- 打卡等级:以坛为家II
- 打卡总天数:556
- 最近打卡:2025-08-10 11:00:24
金牌会员
- 积分
- 1619
|
最近在学习冲哥的“定时器周期性调度任务”程序,可以解决部分非阻塞执行问题。但感觉执行时间有问题,还请冲哥或其他老师有空帮忙解答一下,谢谢!
主要有两个问题:
第一,任务调度程序中的每一个模块执行程序时间不是设定的时间,而且相差很大。
static TASK_COMPONENTS Task_Comps[]=
{
//状态 计数 周期 函数
{0, 1, 1, Sample_Display}, /* task 1 Period: 1ms */
{0, 10, 10, Sample_MatrixKey}, /* task 2 Period: 10ms */
{0, 10, 10, Sample_adcKey}, /* task 3 Period: 10ms */
{0, 300, 300, Sample_NTC}, /* task 4 Period: 300ms */
{0, 500, 500, Sample_RTC}, /* task 5 Period: 500ms */
/* Add new task here */
};
比如Sample_RTC模块中设定的是500ms。为观察RTC实时时钟的变化情况,在模块的函数中加入printf_usb打印观察,结果发现时间远大于设定的500ms,也不是(500+500=1S)。
void Sample_RTC(void)
{
if(++msecond & 1) //1秒到
{
RTC();
printf_usb("d% %d %d\r\n",usrHour,usrMinute,usrSecond);
}
// DisplayRTC();
}
为进一步验证时间准确性,加入灯闪烁程序:
static TASK_COMPONENTS Task_Comps[]=
{
//状态 计数 周期 函数
{0, 1, 1, Sample_Display}, /* task 1 Period: 1ms */
{0, 10, 10, Sample_MatrixKey}, /* task 2 Period: 10ms */
{0, 10, 10, Sample_adcKey}, /* task 3 Period: 10ms */
{0, 300, 300, Sample_NTC}, /* task 4 Period: 300ms */
{0, 500, 500, Sample_RTC}, /* task 5 Period: 500ms */
{0, 500, 500, led1_blink}, //P20=!P20
{0, 1000, 1000, led2_blink}, //P21=!P21
/* Add new task here */
};
用示波器查看P20/P21时间间隔,差别太大。P20/P21并不是按照设定的500ms/1000ms闪烁(不好意思未截图)
第二,这个RTC时钟模块以什么时钟为节拍进行计时的?在程序中找不到关联的传递参数。既没有设置内/外部晶振,也没有设置RTC相关寄存器,但它确实在走时?!是不是用的定时器0的节拍进行走时的?可是定时器是1ms定时,设置的参数不对呀?在config.h程序中修改:
#define Timer0_Reload (MAIN_Fosc / 1000) //Timer 0 中断频率, 1000次/秒。倒是可以改变走时速度,但时间走时不对。看程序他们并没有什么关联。唯一的传递变量msecond也是在局部程序中定义的,这个if(++msecond & 1) //1秒到。怎么实现1S时间的?
附上官方源程序,请老师们指导!
|
|