微控制器:Arduino Uno的ATmega328P 时钟频率:16MHz
void timeDelay_CTC(float sec, unsigned char times) //0.1 <= sec <= 4
{
OCR1A = (sec / 0.000064f) - 1;
TCCR1A = 0b00000000;
TCCR1B = 0b00001101;
for( unsigned char i = 1; i <= times; i++ )
{
while( (TIFR1 & (1<<OCF1A)) == 0 );
TIFR1 |= (1<<OCF1A);
}
TCCR1A = 0;
TCCR1B = 0;
}
上述功能用于计算延时周期数,然后在CTC模式下实现。它运作良好。现在,我想在正常模式下编写类似的功能。下面是代码。
void timeDelay_NORM(float sec, unsigned char times)
{
unsigned int cycle = (sec / 0.000064f);
TCNT1 = 65534 - cycle;
TCNT1 = 49910;
TCCR1A = 0b00000000;
TCCR1B = 0b00000101;
for( unsigned char x = 1; x <= 2; x++ )
{
while( (TIFR1 & (1<<TOV1)) == 0 );
TIFR1 |= (1<<TOV1);
}
TCCR1A = 0;
TCCR1B = 0;
}
然而,具有参数“times”的正常模式函数&gt; 1,延时将比预期的要长很多。所以,我尝试了以下代码。
void timeDelay_NORM(float sec, unsigned char times)
{
//unsigned int cycle = (sec / 0.000064f);
//TCNT1 = 65534 - cycle;
TCNT1 = 49910; //Cycles for 0.5sec
TCCR1A = 0b00000000;
TCCR1B = 0b00000101;
//for( unsigned char x = 1; x <= 2; x++ )
//{
while( (TIFR1 & (1<<TOV1)) == 0 ); //Run 0.5sec two times to delay 1sec
TIFR1 |= (1<<TOV1);
while( (TIFR1 & (1<<TOV1)) == 0 );
TIFR1 |= (1<<TOV1);
//}
TCCR1A = 0;
TCCR1B = 0;
}
我发现当它运行以下指令2次时,时间延迟将比预期的要长得多。它延迟大约5s而不是1s。
while( (TIFR1 & (1<<TOV1)) == 0 );
TIFR1 |= (1<<TOV1);
你能教我如何使它发挥作用吗?或者给我一些提示。
感谢您的帮助!
答案 0 :(得分:1)
您不会在循环迭代之间重置TCNT1。
在第一个循环中,它将计数(UINT16_MAX - 49910)个循环。设置TOV1后,TCNT1将翻转为0(溢出)并一直向上计数到UINT16_MAX,这会导致更长的延迟。