Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4.  
  5. #define SEG_PORT PORTB
  6. #define SEG_DIR DDRB
  7. #define SEG_A 4
  8. #define SEG_B 3
  9. #define SEG_C 1
  10. #define SEG_D 6
  11. #define SEG_E 2
  12. #define SEG_F 5
  13. #define SEG_G 0
  14. #define SEG_H 7
  15.  
  16. #define DIG_PORT PORTC
  17. #define DIG_DIR DDRC
  18. #define DIG_1 0
  19. #define DIG_2 1
  20.  
  21. #define MUX_TIME 1
  22.  
  23. char tab_seg[] =
  24. {
  25.     _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_C) | _BV(SEG_D) | _BV(SEG_E) | _BV(SEG_F),    // 0
  26.     _BV(SEG_B) | _BV(SEG_C),                                                        // 1
  27.     _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_G) | _BV(SEG_D) | _BV(SEG_E),                 // 2
  28.     _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_C) | _BV(SEG_D) | _BV(SEG_G),                 // 3
  29.     _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_B) | _BV(SEG_C),                              // 4
  30.     _BV(SEG_A) | _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_C) | _BV(SEG_D),                             // 5
  31.     _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_E) | _BV(SEG_A) | _BV(SEG_C) | _BV(SEG_D),                                // 6
  32.     _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_C),                               // 7
  33.     _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_E) | _BV(SEG_D) | _BV(SEG_C) | _BV(SEG_B) | _BV(SEG_A),                               // 8
  34.     _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_B) | _BV(SEG_A) | _BV(SEG_C) | _BV(SEG_D),                                // 9
  35.  
  36.  
  37. };
  38.  
  39. char dot, i;
  40. volatile int tim_multiplex = -1;
  41. volatile int tim_digit = -1;
  42. int dig1, dig2;
  43.  
  44. void set_7seg(){
  45.     static char current_dig = 1;
  46.  
  47.     if (current_dig == 1 && tim_multiplex >= MUX_TIME) {
  48.         DIG_PORT = _BV(DIG_1);
  49.         SEG_PORT = tab_seg[dig1];
  50.         tim_multiplex = 0;
  51.         current_dig = 2;
  52.     }
  53.  
  54.     if (current_dig == 2 && tim_multiplex >= MUX_TIME) {
  55.         DIG_PORT = _BV(DIG_2);
  56.         SEG_PORT = tab_seg[dig2];
  57.         tim_multiplex = 0;
  58.         current_dig = 1;
  59.     }
  60. }
  61.  
  62.  
  63. int main(void)
  64. {
  65.  
  66.     // 8-bit TimerX config
  67.     TCCR0B |= (1<<CS00) | (1<<CS01);  // set prescaler = 64 ---> please check proper values in PDF
  68.     TCCR0A |= (1<<WGM01); // set CTC mode ---> please check proper values in PDF
  69.     OCR0A  = 124;  // every  [ 1 ms ]
  70.     TIMSK0  |= (1<<OCIE0A);     // enable CompareX interrupt
  71.  
  72.     sei();
  73.  
  74.  
  75.     SEG_PORT = 0xFF;
  76.     SEG_DIR = 0xFF;
  77.  
  78.     DIG_PORT = 0x00;
  79.     DIG_DIR = (1 << DIG_1) | (1 << DIG_2);
  80.     dig1 = 0;
  81.     dig2 = 0;
  82.     dot = 0;
  83.  
  84.     // Start multiplexing
  85.     tim_multiplex = 0;
  86.  
  87.     // Start counting
  88.     i = 0;
  89.     tim_digit = 0;
  90.     while(1)
  91.     {
  92.  
  93.         if (tim_digit >= 124)
  94.         {
  95.             tim_digit = 0;
  96.             i++;
  97.             if (i > 9) {
  98.                 i = 0;
  99.                 dig1++;
  100.             }
  101.             if(dig1 > 9) dig1 = 0;
  102.             dig2 = i;
  103.         }
  104.         set_7seg();
  105.     }
  106.  
  107. }
  108.  
  109. // Timer0 compareA interrupt
  110. ISR( TIMER0_COMPA_vect ) {
  111.     if (tim_multiplex >= 0) ++tim_multiplex;
  112.     if (tim_digit >= 0) ++tim_digit;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement