This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TI自带的延时函数可以斌变量吗

TI的延时函数

#include <intrinsics.h>       //延时函数包含的头文件

#define   mcu_xtal   8   //可定义为你所用的晶振频率Mhz  

#define   delay_us(x)   __delay_cycles (x * mcu_xtal)  

#define   delay_ms(x)   __delay_cycles ((unsigned long)x * mcu_xtal*1000)  

#define   delay_s(x)    __delay_cycles ((unsigned long)x * mcu_xtal*1000000)

我在调用时想采用变量来控制其延时的时间

uint  t=1;

delay_us(t);            //这样调用时会出现错误

delay_us(3);       //里面放数字时没有错误

Error[Ta003]: Intrinsic function parameter must be literal

Error while running C/C++ compiler

  • 这样的延时是不能用变量去控制的,__delay_cycles()这个本征函数为了追求延时的准确度,并不是用简单的for循环去控制延时,而回考虑很多判断代码的运行时间,这是需要在程序编译期就确定的。用变量的话,就无法做到准确的延时了。若楼主的延时不需要非常准确的话,可以通过for循环反复执行__delay_cycles()来实现,或者简单的直接用for来完成。