【K70EK_T7_MQX例程】016Timer(定时器)

By Mcuzone

调试串口波特率:115200-uart2
基于MDKK70-EK_T7硬件平台
关键词: Freescale 飞思卡尔 Kinetis MQX Cortex-M4 K70 MDKK70 核心板 开发板 例程 demo timer

可以参考MQX手册timer章节
串口输出
Two timers are created, each of a period of 2 seconds,
the second timer offset by 1 second from the first.
Task runs for 6 seconds,
then timers are closed and task finishes.

ON
OFF
ON
OFF
ON
OFF

The task is finished!

3.9.4.1 创建定时器组件
可以通过调用_timer_create_component()创建定时器组件,同时指定优先级和定时器任务堆栈长度,这些都是在定时器组件创建时由MQX创建的。定时器任务管理定时器队列,为通知函数提供场景信息。
93
如果没有创建定时器组件,MQX则会在应用程序第一次开启定时器时利用默认值创建。
参数
默认值
定时器任务的优先级
1
定时器任务的堆栈长度
500
3.9.4.2 开启定时器
任务可调用以下函数之一开启定时器:
_timer_start_oneshot_after(),
_timer_start_oneshot_after_ticks() ·
_timer_start_oneshot_at(),
_timer_start_oneshot_at_ticks() ·
_timer_start_periodic_at(),
_timer_start_periodic_at_ticks() ·
_timer_start_periodic_every(),
_timer_start_periodic_every_ticks() 
当定时器调用任意一个函数时,MQX向定时器队列加入定时器请求。当定时器超时,则通知函数运行。 注意:定时器任务的堆栈空间必须包括通知函数需要的堆栈空间。
3.9.4.3 取消重要定时器请求
任务通过调用_timer_cancel()函数取消重要定时器请求,同时需要向该函数传入_timer_start函数传来的定时器句柄。
3.9.4.4 举例:定时器的使用
每秒钟模拟LED亮暗。一个定时器启动LED,另一个关闭它。
3.9.4.4.1 样例代码
/* main.c */ 
#include <mqx.h> #include <bsp.h> #include <fio.h> #include <timer.h> 
#define TIMER_TASK_PRIORITY 2
#define TIMER_STACK_SIZE 1000 
#define MAIN_TASK 10 extern void main_task(uint_32); 

TASK_TEMPLATE_STRUCT MQX_template_list[] = { {MAIN_TASK, main_task, 2000, 8, "Main", MQX_AUTO_START_TASK, 0L, 0},
{0, 0, 0, 0, 0, 0,0L, 0 } };
/*函数*----------------------------------------------------------* 
* 函数名: LED_on * 返回值 : none * 注释 :该定时器函数打印‖ON‖ *结束*-----------------------------------------------------------*/ 
void LED_on ( _timer_id id, pointer data_ptr, MQX_TICK_STRUCT_PTR tick_ptr )
{ printf("ON\n"); } 
/*函数*----------------------------------------------------------*
* 函数名: LED_off * 返回值 : none * 注释 :该定时器函数打印“OFF” *结束*-----------------------------------------------------------*/
void LED_off ( _timer_id id, pointer data_ptr, MQX_TICK_STRUCT_PTR tick_ptr )
{ printf("OFF\n"); } 
/*任务*----------------------------------------------------------*
* 任务名 : main_task * 注释 :任务创建两个定时器,每个定时器周期为2秒,第二个与第一个定时器定时相差1秒 *结束*-----------------------------------------------------------*/
void main_task( uint_32 initial_data )
{ MQX_TICK_STRUCT ticks; MQX_TICK_STRUCT dticks; _timer_id on_timer; _timer_id off_timer; 
/* ** Create the timer component with more stack than the default *
* in order to handle printf() requirements: */ 
_timer_create_component(TIMER_DEFAULT_TASK_PRIORITY, 1024); 
_time_init_ticks(&dticks, 0); 
_time_add_sec_to_ticks(&dticks, 2); 
_time_get_ticks(&ticks); 
_time_add_sec_to_ticks(&ticks, 1); 
on_timer = _timer_start_periodic_at_ticks(LED_on, 0, TIMER_ELAPSED_TIME_MODE, &ticks, &dticks); 
_time_add_sec_to_ticks(&ticks, 1); 
off_timer = _timer_start_periodic_at_ticks(LED_off, 0, TIMER_ELAPSED_TIME_MODE, &ticks, &dticks); 
_time_delay_ticks(600); 
printf("\nThe task is finished!"); 
_timer_cancel(on_timer); 
_timer_cancel(off_timer); 
_mqx_exit(0); }

串口输出: 

例程及示例:

链接: http://pan.baidu.com/s/1jGwUi4A 密码: aq5e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值