Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #include <hidef.h> /* common defines and macros */
  2. #include "derivative.h" /* derivative-specific definitions */
  3. #include "delay.h"
  4.  
  5. //function prototypes
  6. void update_seven_segment(void);
  7. void setClk(void);
  8.  
  9. //initialize variables as unsigned chars (or unsigned bytes)
  10. unsigned char mill = 0;
  11. unsigned char tenths = 0;
  12. unsigned char secs = 0;
  13. unsigned char tenSecs = 0;
  14. unsigned char minutes = 0;
  15. unsigned char select = 0;
  16. unsigned char disp_data[4];
  17. unsigned char disptn[4];
  18.  
  19.  
  20. unsigned char lastbuttons = 0;
  21. unsigned char repeat = 0;
  22. unsigned char runflag = 0; //only update clock if set
  23.  
  24. //array for seven segment pattern
  25. const unsigned char segm_ptrn [38] = {
  26. 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
  27. 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,
  28. 0x3d,0x76,0x74,0x1e,0x38,0x54,0x63,0x5c,
  29. 0x73,0x50,0x78,0x3e,0x1c,0x6e,0x08,0x40,
  30. 0x00,0x01,0x48,0x41,0x09,0x49
  31. };
  32.  
  33.  
  34. void main(void) {
  35. //initialize Dragon12 board ports
  36. setClk();
  37. PTP = 0xFF; //turn off 7-segment display
  38. DDRB = 0xFF; //portb = output
  39. DDRP = 0xFF; //portp = output
  40. DDRJ = 0xFF; //make port J an output port
  41. PTJ = 0xFF; //make PJ1 high to disable LEDs
  42. DDRH = 0x00; //initialize buttons
  43.  
  44.  
  45.  
  46. EnableInterrupts;
  47.  
  48.  
  49. while(1) {
  50. /**** update clock using your own code here ****/
  51. delayby50us(4);
  52. mill++;
  53. if(mill==100) {
  54. tenths++;
  55. mill=0;
  56. if(tenths==10){
  57. secs++;
  58. tenths=0;
  59. if(tenSecs==6){
  60. minutes++;
  61. tenSecs=0;
  62. secs=0;
  63. }else if(secs%10 == 0){
  64. tenSecs++;
  65. secs=0;
  66. }
  67. }
  68. }
  69.  
  70.  
  71.  
  72. disp_data[0]= minutes;
  73. disp_data[1]= tenSecs;
  74. disp_data[2]= secs;
  75. disp_data[3]= tenths;
  76.  
  77.  
  78. /**** update the numbers on seven segment ****/
  79.  
  80.  
  81. update_seven_segment(); //updates what is shown on the seven segment
  82. }
  83. }
  84.  
  85. void update_seven_segment(void) {
  86. int i = 0;
  87.  
  88. //get segment pattern for each number
  89. for(i=0;i<4;i++)
  90. disptn[i] = segm_ptrn[disp_data[i]];
  91.  
  92. //multiplexing display one digit at a time
  93. //digit0:
  94. PORTB = disptn[3];
  95. PTP &= ~0x08;
  96. PTP |= 0x07;
  97. delayby50us(4);
  98.  
  99. //digit1:
  100. PORTB = disptn[2];
  101. PTP &= ~0x04;
  102. PTP |= 0x0B;
  103. delayby50us(4);
  104.  
  105. //digit2:
  106. PORTB = disptn[1];
  107. PTP &= ~0x02;
  108. PTP |= 0x0D;
  109. delayby50us(4);
  110.  
  111. //digit3:
  112. PORTB = disptn[0];
  113. PTP &= ~0x01;
  114. PTP |= 0x0E;
  115. delayby50us(4);
  116. }
  117.  
  118. //this function must be called once to set up clock with correct speed
  119. void setClk(void){
  120. REFDV = 0x00;
  121. SYNR = 0x02;
  122. PLLCTL=0x60;
  123. while(CRGFLG&0x08 == 0x00){ //busy wait
  124. }
  125.  
  126. CLKSEL|=0x80;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement