Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- // global variable to count the number of overflows
- volatile uint8_t tot_overflow;
- // initialize timer, interrupt and variable
- volatile bool is_interupt;
- volatile uint8_t counter;
- volatile uint8_t tab_size;
- volatile uint32_t tab[26] = { 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00};
- void timer1_init()
- {
- sei();
- // set up timer with prescaler = 8
- TCCR1B |= (1 << CS10);
- // initialize counter
- TCNT1 = 0;
- // enable overflow interrupt
- TIMSK1 |= (1 << TOIE1);
- // enable global interrupts
- sei();
- // initialize overflow counter variable
- tot_overflow = 0;
- is_interupt = false;
- }
- // TIMER1 overflow interrupt service routine
- // called whenever TCNT1 overflows
- ISR(TIMER1_OVF_vect)
- {
- // keep track of the number of overflows
- tot_overflow++;
- // check for number of overflows here itself
- // 61 overflows = 2 seconds delay (approx.)
- if(tot_overflow >= 61) // NOTE: '>=' used instead of '=='
- {
- is_interupt = true;
- tot_overflow = 0;
- // reset overflow counter
- }
- }
- volatile bool is_on;
- int main(void)
- {
- DDRB=0xFF;
- is_on = false;
- tab_size = 26;
- counter = 0;
- //PORTB = 0x80;
- // initialize timer
- timer1_init();
- // loop forever
- while(1)
- {
- if(is_interupt){
- if(counter >= tab_size){
- counter = 0;
- }
- PORTB = tab[counter];
- counter++;
- is_interupt = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement