atmega - cycle the LEDS IN ATMEGA8515 -


i want read switches next leds , cycle leds 0 whichever switch pressed if none pressed cycle through of them delay.for have used timer0. since work on atmega8515. used int0. here implementation:

#include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h>  #define bit(n) (1 << n)   volatile uint8_t led, k, switches, , j = 1;             /* uint8_t same unsigned char */ int main(void) {        ddrb = 0xff;                /* use pins on portb output */      led = 1;                    /* init variable representing led state */     portb = 0xff;      cli( );      tccr0|=(1<<cs02) |(1<<cs00);   //enable overflow interrupt enable    timsk|=(1<<toie0);   //initialize counter    tcnt0=0;      gicr = bit(int0);     mcucr = bit(isc01) | bit(isc00);     sei( );      ( ; ;);    }   isr(timer0_ovf_vect) {    if(switches == 0xff)   {     portb = ~led;           /* invert output since 0 means: led on */         led <<= 1;              /* move next led */         if (!led)               /* overflow: start pin b0 again */         {             led = 1;         }   }        else   {     (k = 0; k< 8;k++)     {         j =  switches & (1 << k);          if(j == 0)         {            for(i=1;i<=(k +1);i++)             {                    j = 1;                 portb =  ~j;                 j = 1 << i;                  _delay_ms(100);     //without delay doesnt cycle leds whichever switch pressed             }         } } } 

but using delay loops in isr bad programming practice. how use same timer instead of delay?

i think in isr, should update led status , in main loop set portb , read switches values. in code, seems occupy time in isr.


Comments