for (;;)
{
if(InterKey)
{
if (x>79)
{
x=0;
y+=1;
}
PC_DispChar(
x, y, //字符的显示位置
*(char*)pdata,
DISP_BGND_BLACK+DISP_FGND_WHITE
);
PC_DispStr(5,6,s,DISP_BGND_BLACK+DISP_FGND_WHITE);
x += 1;
}
InterKey=FALSE;
OSIntNesting--;
sprintf(w,"%d",OSIntNesting);
OSTimeDlyHMSM(0, 0, 1, 0); //等待1秒
PC_DispStr(x1,y1,w,DISP_BGND_BLACK+DISP_FGND_WHITE);
x1+=3;
PC_DispChar(x1,y1,',',DISP_BGND_BLACK+DISP_FGND_WHITE);
x1++;
if(x1>79)
{
y1+=1;
x1=0;
}
}
}
程序一开始运行结果如下:
1秒后再打印出0,如下:
然后我看Ucos-II源码中关于函数OSTimeDlyHMS的源代码,在文件os_time.c中,源代码如下:
#if OS_TIME_DLY_HMSM_EN > 0u
INT8U OSTimeDlyHMSM (INT8U hours,
INT8U minutes,
INT8U seconds,
INT16U ms)
{
INT32U ticks;
if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
return (OS_ERR_TIME_DLY_ISR);
}
if (OSLockNesting > 0u) { /* See if called with scheduler locked */
return (OS_ERR_SCHED_LOCKED);
}
#if OS_ARG_CHK_EN > 0u
if (hours == 0u) {
if (minutes == 0u) {
if (seconds == 0u) {
if (ms == 0u) {
return (OS_ERR_TIME_ZERO_DLY);
}
}
}
}
if (minutes > 59u) {
return (OS_ERR_TIME_INVALID_MINUTES); /* Validate arguments to be within range */
}
if (seconds > 59u) {
return (OS_ERR_TIME_INVALID_SECONDS);
}
if (ms > 999u) {
return (OS_ERR_TIME_INVALID_MS);
}
#endif
/* Compute the total number of clock ticks required.. */
/* .. (rounded to the nearest tick) */
ticks = ((INT32U)hours * 3600uL + (INT32U)minutes * 60uL + (INT32U)seconds) * OS_TICKS_PER_SEC
+ OS_TICKS_PER_SEC * ((INT32U)ms + 500uL / OS_TICKS_PER_SEC) / 1000uL;
OSTimeDly(ticks);
return (OS_ERR_NONE);
}
#endif
因为OSIntNesting--,OSIntNesting从0变成了255,OSTimeDlyHMS源码中判断OSLockNesting>0,OSTimeDlyHMS函数马上退出,不延时。所以255到1就一次性打印出来了,OSLockNesting变成0时,延时1秒才有效。但是我在CSDN上下载了任哲这本书的例程源码,它改了人家OSTimeDlyHMS的源码,把
if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
return (OS_ERR_TIME_DLY_ISR);
}
if (OSLockNesting > 0u) { /* See if called with scheduler locked */
return (OS_ERR_SCHED_LOCKED);