LiteOS系统的软件定时器

在LiteOS系统中的软件定时器是由一个硬件定时器,每隔10ms逐一检查每个软件定时器是否超时,如已超时则执行该软件定时器的处理函数,并重新计时。

软件定时器的主要操作函数:

//创建软件定时器,指定超时执行的函数,定时器类型,函数参数,属性配置
osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, 
        void *argument, const osTimerAttr_t *attr)

参数:
func参数指定超时后执行的函数,
type参数指定定时器是重复工作(osTimerPeriodic)的还是一次性的(osTimerOnce),
argument参数指定超时函数执行时传递的参数, 
attr参数指定定时的属性配置,一般设为NULL,使用默认配置即可

返回值:
成功返回定时器的ID,通过此ID可以启动或停止定时器

用法:
 void timerFunc(void *arg) //定时器超时函数
 {
    printf("time out\n");
 }

 osTimerId_t timerID = osTimerNew(timerFunc, osTimerPeriodic, NULL, NULL);

 //启动定时器,并指定超时时间.
 osTimerStart(timerID, 100*3); //超时时间为: 300*10ms

通过定时检查按键IO口电平的方式实现按键驱动:


#include <stdio.h>
#include <ohos_init.h> 
#include <hi_io.h>
#include <iot_gpio.h>
#include <iot_errno.h>
#include <unistd.h>
#include <cmsis_os2.h>
#include <hi_timer.h>

#define KEY1_IO     HI_IO_NAME_GPIO_11
#define KEY1_FUNC   HI_IO_FUNC_GPIO_11_GPIO
#define KEY2_IO     HI_IO_NAME_GPIO_12
#define KEY2_FUNC   HI_IO_FUNC_GPIO_12_GPIO

osTimerId_t timerID;//声明定时器ID
int nKey1 = 0, nKey2 = 0; //记录按键按下次数
int fKey1 = 0, fKey2 = 0;//标注按钮是否按下,0表示松手,1表示按下

void scanKey(char *name, int io, int *flag, int *n)
{
    int v;
    //检查KEY
    IoTGpioGetInputVal(io, &v);
    if (v)
    {
        if (*flag)
        {
            printf("%s up:%d\n", name, *n);
            *flag = 0; //表示已松手
        }
    }
    else
    {
        if (!*flag)
        {
            printf("%s down:%d\n", name, (*n)++);       
            *flag = 1; //表示已按下 
        }
    }
}

void timerFunc(void *arg)
{
    scanKey("key1", KEY1_IO, &fKey1, &nKey1);
    scanKey("key2", KEY2_IO, &fKey2, &nKey2);
}

void initKey(int io, int ioFunc)
{
    IoTGpioInit(io);
    hi_io_set_func(io, ioFunc);
    hi_io_set_pull(io, HI_IO_PULL_UP);
    IoTGpioSetDir(io, IOT_GPIO_DIR_IN);
}
void myhello_test()
{
    initKey(KEY1_IO, KEY1_FUNC);
    initKey(KEY2_IO, KEY2_FUNC);    

    //创建定时器,指定超时执行的函数,定时器类型,函数参数,属性配置
    timerID = osTimerNew(timerFunc, osTimerPeriodic, NULL, NULL);
    //启动定时器,100ms扫描
    osTimerStart(timerID, 5);
}

SYS_RUN(myhello_test);

相关推荐

  1. LiteOS系统软件定时器

    2024-07-12 08:34:04       23 阅读
  2. STM32—系统定时器

    2024-07-12 08:34:04       55 阅读
  3. 定时重启指定软件

    2024-07-12 08:34:04       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 08:34:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 08:34:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 08:34:04       57 阅读
  4. Python语言-面向对象

    2024-07-12 08:34:04       68 阅读

热门阅读

  1. Codeforces Round #956 (Div. 2) and ByteRace 2024

    2024-07-12 08:34:04       27 阅读
  2. C++学习笔记

    2024-07-12 08:34:04       28 阅读
  3. 调整视频帧率、分辨率

    2024-07-12 08:34:04       29 阅读
  4. 路由器内部优先级和外部优先级的区别

    2024-07-12 08:34:04       27 阅读
  5. 嵌入式驱动程序100道面试题(6万字长文)

    2024-07-12 08:34:04       23 阅读
  6. Linux中防火墙firewalld

    2024-07-12 08:34:04       24 阅读
  7. 针对不支持AJAX异步查询的虚拟空间做跨站点查询

    2024-07-12 08:34:04       28 阅读
  8. docker 基础命令

    2024-07-12 08:34:04       24 阅读
  9. GPT-4o在Excel的应用

    2024-07-12 08:34:04       28 阅读
  10. Apache Web安全分析与增强

    2024-07-12 08:34:04       24 阅读