0

我想读取 LED 旁边的开关,并将 LED 从 0 循环到按下的任何开关,如果没有按下任何开关,则延迟循环通过所有开关。为此,我使用了 timer0。因为我在 atmega8515 上工作。我用的是INT0。这是我的实现:

#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)
        {
           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
            }   

    }
}
}

但是在 ISR 中使用延迟循环是一种不好的编程习惯。如何使用相同的计时器而不是延迟?

4

1 回答 1

1

我认为在 ISR 中,您应该只更新 LED 状态,在主循环中您可以设置 PORTB 并读取开关值。在您的代码中,它似乎在 ISR 中占用了很多时间。

于 2011-06-02T03:01:58.140 回答