RT-thread studio 关于硬件定时器计数的问题
[复制链接]
环境:RTT中配置了硬件时钟,计数频率是1MHZ,向上计数,最大计数为0xFFFF
测试步骤:
1.定时器触发模式为周期触发,定时器时间为100US,当定时器超时,进入回调中,读取计数器的值
A.
/* 设置模式为周期性定时器 */
mode = HWTIMER_MODE_PERIOD;
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
if (ret != RT_EOK)
{
rt_kprintf("set mode failed! ret is :%d\n", ret);
return ret;
}
B.
/* 定时器超时回调函数 */
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_hwtimerval_t timeout_s; /* 定时器超时值 */
/* 读取定时器当前值 */
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
rt_kprintf("Read: Usec = %d\n", timeout_s.usec);
return 0;
}
打印结果为:
Read: Usec = 536656
Read: Usec = 536719
Read: Usec = 536881
Read: Usec = 536942
2.
定时器触发模式为周期触发,定时器时间为100US,当定时器超时,进入回调中,读取计数器的值并重新设置定时器的超时时间为100US
A.
/* 设置模式为周期性定时器 */
mode = HWTIMER_MODE_PERIOD;
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
if (ret != RT_EOK)
{
rt_kprintf("set mode failed! ret is :%d\n", ret);
return ret;
}
B.
/* 定时器超时回调函数 */
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_hwtimerval_t timeout_s; /* 定时器超时值 */
/* 读取定时器当前值 */
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
rt_kprintf("Read: Usec = %d\n", timeout_s.usec);
/* 设置定时器超时值为100us并启动定时器 */
timeout_s.sec = 0; /* 秒 */
timeout_s.usec = 100; /* 微秒 */
rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s));
return 0;
}
打印结果为:
Read: Usec = 103
Read: Usec = 103
Read: Usec = 103
Read: Usec = 103
3.
定时器触发模式单次触发,定时器时间为100US,当定时器超时,进入回调中,读取计数器的值并重新设置定时器的超时时间为100US
A.
/* 设置模式为单次定时器 */
mode = HWTIMER_MODE_ONESHOT;
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
if (ret != RT_EOK)
{
rt_kprintf("set mode failed! ret is :%d\n", ret);
return ret;
}
B.
/* 定时器超时回调函数 */
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_hwtimerval_t timeout_s; /* 定时器超时值 */
/* 读取定时器当前值 */
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
rt_kprintf("Read: Usec = %d\n", timeout_s.usec);
/* 设置定时器超时值为100us并启动定时器 */
timeout_s.sec = 0; /* 秒 */
timeout_s.usec = 100; /* 微秒 */
rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s));
return 0;
}
打印结果为:
Read: Usec = 100
Read: Usec = 100
Read: Usec = 100
Read: Usec = 100
问:以上三次的测试步骤,导致的结果不同的原因是?
注:希望可以详细的解释清楚并给我合理的代码建议
|