一、RTX的定时器组的API函数总共有3个,如下图
下面就解析这3个函数的及其使用方法
1、os_tmr_create函数原型
OS_ID os_tmr_create ( U16 tcnt, /* Length of the timer. */ U16 info ); /* Argument to the callback function. */函数描述:
该函数用于创建定时器组并启动定时器,定时时间到后就调用回调函数os_tmr_call
第一个参数用来设置定时的时间
第二个参数表示是那个定时器,主要用于回调函数区分不同的定时器
返回值 创建成功的话会返回定时器的ID,失败返回NULL
使用举例:
__task void task1 (void) { .. tmr1 = os_tmr_create (10, 1); if (tmr1 == NULL) { printf ("Failed to create user timer.\n"); } .. }2、os_tmr_call函数原型
void os_tmr_call ( U16 info ); /* Identification of an expired timer. */函数描述:
当函数os_tmr_create设置的定时时间到后调用回调函数os_tmr_call,
第一个参数用于区分不同定时器
使用举例:
void os_tmr_call (U16 info) { switch (info) { case 1: /* The supervised task is locked, */ /* recovery actions required. */ break; case 2: /* The second task is locked. */ break; .. } }3、os_tmr_kill函数原型
OS_ID os_tmr_kill ( OS_ID timer ); /* ID of the timer to kill */函数描述:
该函数用于终止指定的定时器
第一个参数是指定定时器的ID
使用举例:
OS_TID tsk1; OS_ID tmr1; __task void task1 (void) { .. if (os_tmr_kill (tmr1) != NULL) { printf ("\nThis timer is not on the list."); } else { printf ("\nTimer killed."); } .. }二、使用注意事项
1、一定要配置软定时器,如下红圈,默认的时候是 0,这里我设置的是5个软定时器
2、RTX的定时器仅支持单次,不支持周期性执行,如果需要周期执行,需要重复创建
3、定时器回调函数os_tmr_call中仅支持isr_开头的系统函数,不支持os_开头的函数,因为回调函数是在滴答定时器中断中执行的
三、实验目的
学习RTX定时器
四、实验内容
1、按键k1按下,创建定时器1,在回调函数里面点亮LED
2、按键k2按下,创建定时器2,在回调函数里面熄灭LED
3、按键k3按键,用于停止定时器2
五、实验完整代码
#include "bsp.h" /* 底层硬件驱动 */
#include <RTL.h>
static uint64_t AppTaskLEDStk[256/8];/*任务栈*/
static uint64_t AppTaskStartStk[512/8];/*任务栈*/
static uint64_t AppTaskUserKeyStk[512/8];/*任务栈*/
/*任务句柄*/
OS_TID HandleTaskLED = NULL;
OS_TID HandleTaskKey = NULL;
/*函数声明*/
static void AppTaskCreate(void);
__task void AppTaskLED(void);
__task void AppTaskStart(void);
__task void AppTaskUerKey(void);
/*定时器的ID*/
OS_ID Timer1;
OS_ID Timer2;
/*
*********************************************************************************************************
* 函 数 名: main
* 功能说明: c程序入口
* 形 参:无
* 返 回 值: 错误代码(无需处理)
*********************************************************************************************************
*/
int main(void)
{
/*
ST固件库中的启动文件已经执行了 SystemInit() 函数,该函数在 system_stm32f4xx.c 文件,主要功能是
配置CPU系统的时钟,内部Flash访问时序,配置FSMC用于外部SRAM
*/
bsp_Init();/**/
os_sys_init_user(AppTaskStart, /*任务函数*/
3, /*任务优先级*/
&AppTaskStartStk, /*任务栈*/
sizeof(AppTaskStartStk));/*任务栈大小*/
/* 进入主程序循环体 */
while (1)
{
;
}
}
/*
*********************************************************************************************************
* 函 数 名: AppTaskUserKey
* 功能说明: 执行按键相对应的功能
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
__task void AppTaskUserKey(void)
{
uint8_t ucKeycode =0;
while(1)
{
ucKeycode = bsp_GetKey();
switch(ucKeycode)
{
case KEY_DOWN_K1:
printf("\r\n启动定时器1\r\n");
Timer1 = os_tmr_create(2000,1);
break;
case KEY_DOWN_K2:
printf("\r\n启动定时器2\r\n");
Timer2 = os_tmr_create(4000,2);
break;
case KEY_DOWN_K3:
printf("\r\n停止定时器2\r\n");
os_tmr_kill(Timer2);
break;
default:
break;
}
os_dly_wait(20);
}
}
/*
*********************************************************************************************************
* 函 数 名: AppTaskLED
* 功能说明: LED闪烁的任务
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
__task void AppTaskLED(void)
{
static uint8_t i = 0;
while(1)
{
if(i % 2 == 0)
{
GPIO_ResetBits(GPIOI,GPIO_Pin_10);/*点亮LED*/
}
else
{
GPIO_SetBits(GPIOI,GPIO_Pin_10);/*熄灭LED*/
}
i++;
os_dly_wait(800);/*系统延时函数 因为时钟节拍为1000 所以这里是延时800ms,也就是使AppTaskLED任务挂起800MS*/
}
}
/*
*********************************************************************************************************
* 函 数 名:AppTaskCreate
* 功能说明: 任务创建
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
static void AppTaskCreate(void)
{
HandleTaskLED = os_tsk_create_user(AppTaskLED, /*任务函数*/
1, /*优先级 注意RTX的数字越小,优先级越低*/
&AppTaskLEDStk, /*任务栈起始地址*/
sizeof(AppTaskLEDStk));/*任务栈大小*/
HandleTaskKey = os_tsk_create_user(AppTaskUserKey,
2,
&AppTaskUserKeyStk,
sizeof(AppTaskUserKeyStk));
}
/*
*********************************************************************************************************
* 函 数 名:AppTaskStart
* 功能说明: 开始任务
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
__task void AppTaskStart(void)
{
AppTaskCreate();/*创建AppTaskLED任务*/
while(1)
{
bsp_KeyScan();
os_dly_wait(10);/*系统延时函数*/
}
}
在配置文件RTX_Conf_CM.c文件的定时器回调函数如下
void os_tmr_call (U16 info) {
/* This function is called when the user timer has expired. Parameter */
/* 'info' holds the value, defined when the timer was created. */
/* HERE: include optional user code to be executed on timeout. */
switch(info)
{
case 1:
printf("\r\n定时器1执行成功\r\n");
GPIO_ResetBits(GPIOF,GPIO_Pin_8);
break;
case 2:
printf("\r\n定时器2执行成功\r\n");
GPIO_SetBits(GPIOF,GPIO_Pin_8);
break;
default:
break;
}
}
六、实验现象
按键k1按下点亮LED按键K2按下熄灭LED,同时按键K3可以终止定时器2