我猜,我的CODE中的溢出中断(用于将 8 位定时器的分辨率从 16µs/步提高到 1µs/步)有一些问题。当程序在我的主循环中的 if 语句中时,似乎溢出中断触发,从而搞砸了!
if(pInt == 1) //PCNINT-if-statment
{
pulse16 = (tot_overflow << 8) | TCNT1; //adds tot_overflow and TCNT1 to be able to set if-statements in PCINT-while-loop with µs
if(PINB & (1 << PINB3)) //if PB3 is HIGH
{
TCNT1 = 0; //resets Timer/Counter1
tot_overflow = 0; //resets tot_overflow variable
}
else
{
if (pulse16 >1555) //when stick 1 travels from 1555 µs towards 2006 µs
{
PORTB &= ~(1 << relayPin); //relay pole switch, + & - on motor
PORTB |= (1 << greenLED); //LED green indicates forward motion
PORTB &= ~(1 << redLED); //turn off red LED
}
else if (pulse16 <1490) //when stick 1 travels from 1490 ms towards 920 µs
{
PORTB |= (1 << relayPin); //relay pole switch, - & + on motor
PORTB &= ~(1 << greenLED); //turn off green LED
PORTB |= (1 << redLED); //LED red indicates backward motion
}
else //if µs is 1490> or <1555 - dead-span to prevent gliteches on relay when stick is in centre position
{
}
}
pInt = 0; //resets pInt to exit PCNINT-if-statment
}
pInt是一个“标志变量”,表示 PCINT 已被触发。每次触发溢出中断时,tot_overflow 变量都会递增。
我使用 ATtiny85 作为 RC 开关,当来自接收器的 µs 高于 1555 时,它应该在 PB2 引脚上变为低电平,当 µs 低于 1490 时变为高电平。
会发生以下情况:当检查 µs 是否高于 1555 或低于 1490并且使用溢出中断它有时会在 1490-1555 的“死区”中将 PB2 引脚高/低,而不应该在“死区”之外,有时会在“死区”之外!这是关于故障的视频。请注意,在我的代码中,绿色 LED 是redLED,黄色 LED 是greenLED 。
我对此很陌生,我不确定为什么会这样,我不明白为什么代码不起作用。我已经查看了 CTC 功能,但我看不出它会有所帮助,因为我仍然必须使用定时器溢出中断来获得我想要的 1µs/步的分辨率。
编辑
我尝试了几种变体(@yann-vernier 的建议),但仍然无法正常工作,这是最接近工作代码的方法:
while(1) //leave and/or put your own code here
{
static uint8_t tot_overflow; //variable to count the number of Timer/Counter1 overflows
if (TIFR & (1 << TOV1) )
{
TIFR |= (1 << TOV1); // clear timer-overflow-flag
tot_overflow ++;
}
if(GIFR & (1 << PCIF) ) //PCINT-flag idicates PCINT
{
uint16_t pulse; //variable to make a 16 bit integer from tot_overflow and TCNT1
// PORTB |= (1 << debugPin); //pin is HIGH on when interrupt is intialized
pulse = (tot_overflow << 8) | TCNT1; //adds tot_overflow and TCNT1 to be able to set if-statements in PCINT-while-loop with µs
这部分我没有开始工作:
if ( ((TIFR & (1 << TOV1)) && ((pulse & 0xff))) < 0x80)
{
pulse += 0x100; // Overflow had not been counted
}
我不确定我明白上面发生了什么,我唯一知道的是我可能做错了!当我评论上述部分时,它的工作方式与 mu 旧代码相同!
else
{
if(PINB & (1 << PINB3)) //if PB3 is HIGH
{
TCNT1 = 0; //resets Timer/Counter1
tot_overflow = 0; //resets tot_overflow variable
}
else
{
if (pulse > 1555) //when stick 1 travels from 1555 µs towards 2006 µs
{
PORTB &= ~(1 << relayPin); //relay pole switch, + & - on motor
PORTB |= (1 << greenLED); //LED green indicates forward motion
PORTB &= ~(1 << redLED); //turn off red LED
}
else if (pulse < 1490) //when stick 1 travels from 1490 ms towards 920 µs
{
PORTB |= (1 << relayPin); //relay pole switch, - & + on motor
PORTB &= ~(1 << greenLED); //turn off green LED
PORTB |= (1 << redLED); //LED red indicates backward motion
}
else //if µs is 1490> or <1555 - dead-span to prevent gliteches on relay when stick is in centre position
{
// PORTB |= (1 << greenLED); //for debug to indicate dead-span
// PORTB |= (1 << redLED); //for debug to indicate dead-span
}
}
}
GIFR |= (1 << PCIF); //clear PCINT-flag
}
else
{
}
}
}
ISR(TIMER1_OVF_vect) //when Counter/Timer1 overflows
{
}
ISR(PCINT0_vect) //when pin-level changes on PB3
{
}
它是接近还是我仍然在蓝色?