Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #include <iom128.h>
  2. #include <inavr.h>
  3.  
  4. //Program configuration
  5. //#define ISR_PROCESSING
  6. #define PTR_ACCESS
  7.  
  8. #define LED_SEG PORTE
  9. #define LED_SEG_DIR DDRE
  10. #define LED_DG PORTB
  11. #define LED_DG_DIR DDRB
  12.  
  13. #define DV_480Hz 15
  14. #define DV_DEMO 40
  15. #define DV_NEXT 13
  16.  
  17. #define DEC_OFF 0x3F
  18. #define DISP_MAX 6
  19. #define LED_BLANK 15
  20.  
  21. typedef union TRQ {
  22. //Access to entire byte
  23. unsigned char c;
  24. struct {
  25. unsigned char f_tck : 1;
  26. //other flags can be defined
  27. unsigned char not_used : 7;
  28. };
  29. } TRQ;
  30.  
  31. //Decoding table based on CD4056 -> '0123456789LHPA- '
  32. __flash unsigned char BCD_2_LED[] = {
  33. 0xDE, 0x82, 0xEC, 0xE6,
  34. 0xB2, 0x76, 0x7E, 0xC2,
  35. 0xFE, 0xF6, 0x1C, 0xBA,
  36. 0xF8, 0xFA, 0x20, 0x00 };
  37.  
  38. __flash unsigned char DEC_1_OF_6[] =
  39. {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF};
  40.  
  41. //Global variables
  42. unsigned char disp[6] = {0,0,0,0,0,0};
  43. unsigned char cur_disp;
  44. unsigned char dv_demo = DV_DEMO;
  45. unsigned char dv_next = DV_NEXT;
  46. __no_init unsigned char idx;
  47. #define UPDATE_DISP_RQ 0x01
  48. volatile TRQ rq;
  49.  
  50. void InitDevices()
  51. {
  52. LED_DG = 0xFE;
  53. LED_DG_DIR = 0xFF;
  54. LED_SEG_DIR = 0xFF;
  55. //Prescaler fclk / 1024 = 7200Hz
  56. //Prescaler for T0 is different than for T1,T2,T3
  57. OCR0 = DV_480Hz - 1;
  58. TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01) | (1 << CS00);
  59. TIMSK = (1 << OCIE0);
  60. __enable_interrupt();
  61. }
  62.  
  63. void DispDemo()
  64. {
  65. unsigned char i;
  66. if(--dv_next)
  67. {
  68. //Johnson counter
  69. for(i=5; i; i--)
  70. disp[i] = (disp[i] & 0xFE) | (disp[i-1] & 0x01);
  71. disp[0] = (disp[0] & 0xFE) | (~disp[5] & 0x01);
  72. return;
  73. }
  74. dv_next = DV_NEXT;
  75. //Shift display content
  76. for(i=5; i; i--)
  77. {
  78. disp[i] = (disp[i] & 0x01) | (disp[i-1] & 0xFE);
  79. }
  80. //New pattern generation
  81. if(idx & 0x10)
  82. {
  83. disp[0] = BCD_2_LED[LED_BLANK];
  84. }
  85. else
  86. {
  87. disp[0] = BCD_2_LED[idx];
  88. }
  89. //Take care for operator precedence
  90. if((++idx) == 0x16) idx = 0;
  91. }
  92.  
  93. #pragma vector = TIMER0_COMP_vect
  94. __interrupt void T0_COMP_ISR()
  95. {
  96. LED_DG |= DEC_OFF;
  97. LED_SEG = disp[cur_disp];
  98. LED_DG = DEC_1_OF_6[cur_disp];
  99. if((++cur_disp) == DISP_MAX) cur_disp = 0;
  100. if(--dv_demo) return;
  101. dv_demo = DV_DEMO;
  102. #ifdef ISR_PROCESSING
  103. DispDemo();
  104. #else
  105. rq.f_tck = 1;
  106. #endif
  107.  
  108. }
  109.  
  110. void main()
  111. {
  112. idx = 0;
  113. cur_disp = 0;
  114. //dv_next = DV_NEXT;
  115. //dv_demo = DV_DEMO;
  116. InitDevices();
  117. while(1)
  118. {
  119. #ifndef ISR_PROCESSING
  120. if(rq.f_tck)
  121. {
  122. rq.f_tck = 0;
  123. DispDemo();
  124. }
  125. #endif
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement