このプログラムでは、タイマー割り込みを使用してLEDを循環しています。誰かがスイッチを押すと、最初の割り込みを停止し、押されたスイッチに応じてLEDを点灯させる2番目の割り込みをトリガーする必要があります。ここで、どの割り込みが呼び出されているか少し混乱しています。ピン変更割り込みについていくつかの本を参照し、PCMSK2を設定するための数行を書きました。取得している出力は、「スイッチが押されると、最初はすべてのLEDが循環しています... LEDの循環が停止して最初からやり直します(つまり、プログラムが入力を読み取っており、2番目の割り込みをトリガーしていません)。停止しません。または一時停止して、後続のLEDが点灯しません。」誰か助けてもらえますか?
#include <avr/io.h>
#include <avr/interrupt.h>
#define PINK_MASK \
((1<<PINK0)|(1<<PINK1)|(1<<PINK2)|(1<<PINK3)|(1<<PINK4)|(1<<PINK5)|(1<<PINK6)|(1<<PINK7))
volatile unsigned int intrs, i=1;
void enable_ports(void);
void delay(void);
extern void __vector_23 (void) __attribute__ ((interrupt));
extern void __vector_25 (void) __attribute__ ((signal));
void enable_ports()
{
DDRB = 0xff; //PORTB as output for leds
PORTB = 0xff;
DDRK = 0x00; //PORTK as input from switches
PORTK |= PINK_MASK;
PCMSK2 = PINK_MASK; //ENABLE PCMSK2, Setting interrupts
PCICR = 0x04;
PCIFR = 0x04;
TCCR0B = 0x03; //Setting TIMER
TIMSK0 = 0x01;
TCNT0 = 0x00;
intrs = 0;
}
void __vector_23 (void)
{
intrs++;
if(intrs > 60)
{
intrs = 0;
PORTB = (0xff<<i);
i++ ;
if(i == 10 )
{
PORTB = 0xff;
i = 1 ;
}
}
}
void __vector_25 (void)
{
unsigned char switches;
switches = ((~PINK) & (PINK_MASK)); //Reading from switches
if(switches & (1<<PINK0))
PORTB = (PORTB<<PINK0);
else if (switches & (1<<PINK1))
PORTB = (PORTB<<PINK1);
else if (switches & (1<<PINK2))
PORTB = (PORTB<<PINK2);
else if (switches & (1<<PINK3))
PORTB = (PORTB<<PINK3);
else if (switches & (1<<PINK4))
PORTB = (PORTB<<PINK4);
else if (switches & (1<<PINK5))
PORTB = (PORTB<<PINK5);
else if (switches & (1<<PINK6))
PORTB = (PORTB<<PINK6);
else if (switches & (1<<PINK7))
PORTB = (PORTB<<PINK7);
}
int main(void)
{
enable_ports();
sei();
while(1)
{
}
}
たくさんのご支援ありがとうございます。