开发者

cycle the LEDS IN ATMEGA8515

开发者 https://www.devze.com 2023-03-01 10:52 出处:网络
I want to read the switches next to the LEDS and cycle the LEDS from 0 to whichever switch is pressed

I want to read the switches next to the LEDS and cycle the LEDS from 0 to whichever switch is pressed if none are pressed cycle through all of them with the delay.For this i have used timer0. Since I work on atmega8515. I used INT0. Here is my implementation:

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define BIT(n) (1 << n) 

volatile uint8_t led, k, switches, i , j = 1;
            /* uint8_t is the same as unsigned char */
int main(void)
{



    DDRB = 0xff;                /* use all pins on PortB for output */

    led = 1;                    /* init variable representing the 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( );

    for ( ; ;);



}


ISR(TIMER0_OVF_vect)
{

  if(switches == 0xff)
  {
    PORTB = ~led;           /* invert the output since a zero means: LED on */
        led <<= 1;              /* move to next LED */
        if (!led)               /* overflow: start with Pin B0 again */
        {
            led = 1;
        }
  }     
  else
  {
    for (k = 0; k< 8;k++)
    {
        j =  switches & (1 << k);

        if(j == 0)
        开发者_如何学Python{
           for(i=1;i<=(k +1);i++)
            {   
                j = 1;
                PORTB =  ~j;
                j = 1 << i;

                _delay_ms(100);     //without this delay it doesnt cycle the LEDS from to whichever switch is pressed
            }   

    }
}
}

But using delay loops in ISR is a bad programming practice. How to use the same timer instead of the delay?


I think in the ISR, you should just update the LED status and in the main loop you could set the PORTB and read the switches values. In your code, it seems occupy so much time in ISR.

0

精彩评论

暂无评论...
验证码 换一张
取 消