Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #define SEG_PORT PORTB
- #define SEG_DIR DDRB
- #define SEG_A 4
- #define SEG_B 3
- #define SEG_C 1
- #define SEG_D 6
- #define SEG_E 2
- #define SEG_F 5
- #define SEG_G 0
- #define SEG_H 7
- #define DIG_PORT PORTC
- #define DIG_DIR DDRC
- #define DIG_1 0
- #define DIG_2 1
- #define MUX_TIME 1
- char tab_seg[] =
- {
- _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_C) | _BV(SEG_D) | _BV(SEG_E) | _BV(SEG_F), // 0
- _BV(SEG_B) | _BV(SEG_C), // 1
- _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_G) | _BV(SEG_D) | _BV(SEG_E), // 2
- _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_C) | _BV(SEG_D) | _BV(SEG_G), // 3
- _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_B) | _BV(SEG_C), // 4
- _BV(SEG_A) | _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_C) | _BV(SEG_D), // 5
- _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_E) | _BV(SEG_A) | _BV(SEG_C) | _BV(SEG_D), // 6
- _BV(SEG_A) | _BV(SEG_B) | _BV(SEG_C), // 7
- _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_E) | _BV(SEG_D) | _BV(SEG_C) | _BV(SEG_B) | _BV(SEG_A), // 8
- _BV(SEG_F) | _BV(SEG_G) | _BV(SEG_B) | _BV(SEG_A) | _BV(SEG_C) | _BV(SEG_D), // 9
- };
- char dot, i;
- volatile int tim_multiplex = -1;
- volatile int tim_digit = -1;
- int dig1, dig2;
- void set_7seg(){
- static char current_dig = 1;
- if (current_dig == 1 && tim_multiplex >= MUX_TIME) {
- DIG_PORT = _BV(DIG_1);
- SEG_PORT = tab_seg[dig1];
- tim_multiplex = 0;
- current_dig = 2;
- }
- if (current_dig == 2 && tim_multiplex >= MUX_TIME) {
- DIG_PORT = _BV(DIG_2);
- SEG_PORT = tab_seg[dig2];
- tim_multiplex = 0;
- current_dig = 1;
- }
- }
- int main(void)
- {
- // 8-bit TimerX config
- TCCR0B |= (1<<CS00) | (1<<CS01); // set prescaler = 64 ---> please check proper values in PDF
- TCCR0A |= (1<<WGM01); // set CTC mode ---> please check proper values in PDF
- OCR0A = 124; // every [ 1 ms ]
- TIMSK0 |= (1<<OCIE0A); // enable CompareX interrupt
- sei();
- SEG_PORT = 0xFF;
- SEG_DIR = 0xFF;
- DIG_PORT = 0x00;
- DIG_DIR = (1 << DIG_1) | (1 << DIG_2);
- dig1 = 0;
- dig2 = 0;
- dot = 0;
- // Start multiplexing
- tim_multiplex = 0;
- // Start counting
- i = 0;
- tim_digit = 0;
- while(1)
- {
- if (tim_digit >= 124)
- {
- tim_digit = 0;
- i++;
- if (i > 9) {
- i = 0;
- dig1++;
- }
- if(dig1 > 9) dig1 = 0;
- dig2 = i;
- }
- set_7seg();
- }
- }
- // Timer0 compareA interrupt
- ISR( TIMER0_COMPA_vect ) {
- if (tim_multiplex >= 0) ++tim_multiplex;
- if (tim_digit >= 0) ++tim_digit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement