所以,我正在使用带有ATMega168的NerdKits套件,并将LCD连接到其中,使用两个中断INT0和INT1。 我想连接一个Grove心率监测器,我已将其连接到PCINT1并尝试启用和使用。
void hrm_init()
{
//clear pin
DDRB &= ~(1<<DDB0);
PORTB |= (1<<PORTB0);
//turn on pull up
PCICR |= (1<<PCIE0); //enable pcint 1
PCMSK0 |= (1<<PCINT1); //Trigger on change of PCINT1 (PB1)
sei();
}
ISR(PCINT0_vect)
{
uint8_t changedBits;
changedBits = PINB ^ porthistory;
porthistory = PINB;
//pin has changed
if (changedBits & (1<<PB1))
{
beats += ((PINB & _BV(PB1)) ? 1 : 0); //add a beat on the rising edge
}
}
这段节拍计数代码是从网上借鉴的,因此使用了宏。
这个代码大部分时间都能正常工作,但有时会增加一些额外的节拍,我曾经见过多达10个节拍,同时我还有一个定时器中断(我将用它来计算每分钟的节拍数)。
void clock_init()
{
TCCR0A |= (1<<WGM01);
TCCR0B |= (1<<CS02) | (1<<CS00);
OCR0A = 143;
TIMSK0 |= (1<<OCIE0A);
}
SIGNAL(SIG_OUTPUT_COMPARE0A) {
the_time++;
if (the_time > 500)
{
bpm = beats;
bpm *= 12; //60 seconds divided by 5
the_time = 0;
beats = 0;
}
}