TQ2440 准精确定时方法 非精确

   void delay(U8 time)
/*此处使用timer2
  此处计数频率经过PCLK分频过之后是5MHz,则计数一次是1/5M,计数5次则为1us, val=5 计数为1us
*/

{
 
U32 val = 5;
 rTCFG0 &= ~(0xff<<8);
 rTCFG0 |= 0x4<<8;   //prescaler = 4+1
 rTCFG1 &= ~(0xf<<8);
 rTCFG1 |= 0<<8;  //mux = 1/2

 rTCNTB2 = val;//这个是装载计数值得,关键要在TCON里面设置需不需要自动装载。
 rTCMPB2 = val>>1;  // 50%
 rTCON &= ~(0xf<<12);
 rTCON |= 0xb<<12;  //interval, inv-off, update TCNTB3&TCMPB3, start timer 2
 rTCON &= ~(2<<12);  //clear manual update bit
 while(time--) {
  while(rTCNTO2>=val>>1);
  while(rTCNTO2<val>>1);
 };
}

void Delay(int time)
/*此处使用timer3
  Timer input clock Frequency = PCLK / {prescaler value+1} / {divider value}
  PCLK=50MHz
  实际计时频率为50M/4/2=6.25MHz
  定时时间的计算:计数一次为1/6.25M=1/6.25us 计数数值为TCNTBn中的计数值比如是counter
  那么计数时间为counter*1/6.25us,
  此例当中counter=6250-1,目前尚不清楚为什么减去一
  6250*(1/6.25us)=1000us即1ms
  这样的话 Delay函数的意思就是延迟time毫秒
*/

{
 U32 val = (PCLK>>3)/1000-1;
 
 rTCFG0 &= ~(0xff<<8);
 rTCFG0 |= 3<<8;   //prescaler = 3+1
 rTCFG1 &= ~(0xf<<12);
 rTCFG1 |= 0<<12;  //mux = 1/2

 rTCNTB3 = val;
 rTCMPB3 = val>>1;  // 50%
 rTCON &= ~(0xf<<16);
 rTCON |= 0xb<<16;  //interval, inv-off, update TCNTB3&TCMPB3, start timer 3
 rTCON &= ~(2<<16);  //clear manual update bit
 while(time--) {
  while(rTCNTO3>=val>>1);
  while(rTCNTO3<val>>1);
 };
}

1.可以使用定时器,产生中断,在外围或者在延时程序里面,使用while循环查阅定时器的状态,若查到产生中断,则表明延时到,就可以了



2.使用嵌入c程序汇编实现,使用汇编的定时器其实是比较精确的



你可能感兴趣的:(TQ2440 准精确定时方法 非精确)