【普中Hi3861开发攻略--基于鸿蒙OS】-- 第 19 章 定时器中断实验

 (1)实验平台:

普中Hi3861鸿蒙物联网WIFI套件https://item.taobao.com/item.htm?id=829136021914(2)资料下载:普中科技-各型号产品资料下载链接


        前面章节我们介绍了 Hi3861 的外部中断使用,本章继续介绍定时器功能。本章分为如下几部分内容:

 

19.1 实验介绍

19.1.1 实验简介

19.1.2 实验目的

19.1.3 定时器函数使用

19.1.3.1 hi_timer_create()函数

19.1.3.2 hi_timer_start()函数

19.1.3.3 hi_timer_stop()函数

19.1.3.4 hi_timer_delete()函数

19.2 硬件设计

19.3 软件设计

19.4 实验现象


19.1 实验介绍

19.1.1 实验简介

        定时器,顾名思义就是用来计时的,我们常常会设定计时或闹钟,然后时间到了就告诉我们要做什么。Hi3861 也是这样,通过定时器可以完成各种预设好的任务。定时器到达指定时间后也会产生中断,然后在回调函数内执行所需功能,这个和外部中断类似。

19.1.2 实验目的

        通过定时器让 LED 周期性每秒闪烁 1 次。

19.1.3 定时器函数使用

19.1.3.1 hi_timer_create()函数

        创建系统定时器功能。函数原型:

hi_u32 hi_timer_create(hi_u32 *timer_handle);

        参数:

        timer_handle:获取创建好的定时器句柄

        返回值:0 成功,1 失败;

19.1.3.2 hi_timer_start()函数

        启动系统定时器。函数原型:

hi_u32 hi_timer_start(hi_u32 timer_handle, hi_timer_type type, hi_u32 expire, hi_timer_callback_f timer_func, hi_u32 data);

        参数:

        timer_handle:定时器句柄

        Type:用于设置所创建的定时器类型

        expire:定时时间,单位 ms。如果参数设置值为0,则默认为10ms。

        timer_func:定时器定时时间到达,回调函数。

        data:回调函数参数。不传递参数则为 NULL。

        返回值:0 成功,1 失败;

19.1.3.3 hi_timer_stop()函数

        停止系统定时器。函数原型:

hi_u32 hi_timer_stop(hi_u32 timer_handle);

        参数:

        timer_handle:定时器句柄

        返回值:0 成功,1 失败;

19.1.3.4 hi_timer_delete()函数

        删除系统定时器。如果定时器未到期,则先停止该定时器再删除。函数原型:

hi_u32 hi_timer_delete(hi_u32 timer_handle);

        参数:

        timer_handle:定时器句柄

        返回值:0 成功,1 失败;

19.2 硬件设计

        本实验使用到硬件资源如下:

        (1)LED 模块

        (2)Hi3861 GPIO

        相关电路在前面章节已经介绍过,此处省略。

19.3 软件设计

        将前面章节创建好的工程模板,复制一份,重命名为15_time,如下所示:

        然后在如下路径下新建 bsp_time.c 和 bsp_time.h 文件:

        (1)修改 demo 文件夹下的 BUILD.gn 文件,如下所示:

        (2)添加工程编译文件路径,如下所示

        (3)修改 bsp_time.c 文件,代码如下:

/**
 ****************************************************************************************************
 * @file        bsp_time.c
 * @author      普中科技
 * @version     V1.0
 * @date        2024-06-05
 * @brief       定时器中断实验
 * @license     Copyright (c) 2024-2034, 深圳市普中科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:普中-Hi3861
 * 在线视频:https://space.bilibili.com/2146492485
 * 公司网址:www.prechin.cn
 * 购买地址:
 *
 */

#include "bsp_time.h"
#include <unistd.h>
#include "bsp_led.h"
#include "hi_timer.h"


hi_u32 timer_handle;
uint16_t g_times=0;

//定时器回调函数
void time_isr_fun(void)
{
    static uint8_t i=0;
    i=!i; 
    LED(i);
}

//定时器初始化
//times:定时时间,ms
void time_init(uint16_t times)
{
    hi_u32 ret;
    g_times=times; 
    ret = hi_timer_create(&timer_handle);
    if(ret!=HI_ERR_SUCCESS)
    {
        printf("Timer_Create failed\r\n");
    }
    hi_timer_start(timer_handle,HI_TIMER_TYPE_PERIOD,times,(hi_timer_callback_f)time_isr_fun,NULL);
}

//定时器开关
void time_onoff(uint8_t sta)
{
    if(sta==0)hi_timer_stop(timer_handle);
    else hi_timer_start(timer_handle,HI_TIMER_TYPE_PERIOD,g_times,(hi_timer_callback_f)time_isr_fun,NULL);
}

        (4)修改 bsp_time.h 文件,代码如下:

/**
 ****************************************************************************************************
 * @file        bsp_time.h
 * @author      普中科技
 * @version     V1.0
 * @date        2024-06-05
 * @brief       定时器中断实验
 * @license     Copyright (c) 2024-2034, 深圳市普中科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:普中-Hi3861
 * 在线视频:https://space.bilibili.com/2146492485
 * 公司网址:www.prechin.cn
 * 购买地址:
 *
 */

#ifndef BSP_TIME_H
#define BSP_TIME_H

#include "cmsis_os2.h"
#include "hi_io.h"
#include "hi_gpio.h"



//函数声明
void time_init(uint16_t times);
void time_onoff(uint8_t sta);

#endif

        (5)修改 template.c 文件,代码如下:

/**
 ****************************************************************************************************
 * @file        template.c
 * @author      普中科技
 * @version     V1.0
 * @date        2024-06-05
 * @brief       定时器中断实验
 * @license     Copyright (c) 2024-2034, 深圳市普中科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:普中-Hi3861
 * 在线视频:https://space.bilibili.com/2146492485
 * 公司网址:www.prechin.cn
 * 购买地址:
 *
 ****************************************************************************************************
 * 实验现象:间隔1S,LED指示灯亮灭
 *
 ****************************************************************************************************
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "bsp_time.h"
#include "bsp_led.h"



//控制任务
osThreadId_t TIME_Task_ID; //任务ID

void TIME_Task(void)
{
    led_init();//LED初始化
    time_init(500);//定时器初始化,定时时间500ms
    
    while (1) 
    {
        usleep(10*1000);
    }
}
//任务创建
void time_task_create(void)
{
    osThreadAttr_t taskOptions;
    taskOptions.name = "timeTask";       // 任务的名字
    taskOptions.attr_bits = 0;               // 属性位
    taskOptions.cb_mem = NULL;               // 堆空间地址
    taskOptions.cb_size = 0;                 // 堆空间大小
    taskOptions.stack_mem = NULL;            // 栈空间地址
    taskOptions.stack_size = 1024;           // 栈空间大小 单位:字节
    taskOptions.priority = osPriorityNormal; // 任务的优先级

    TIME_Task_ID = osThreadNew((osThreadFunc_t)TIME_Task, NULL, &taskOptions); // 创建任务
    if (TIME_Task_ID != NULL)
    {
        printf("ID = %d, Task Create OK!\n", TIME_Task_ID);
    }
}

/**
 * @description: 初始化并创建任务
 * @param {*}
 * @return {*}
 */
static void template_demo(void)
{
    printf("普中-Hi3861开发板--定时器中断实验\r\n");
    time_task_create();//任务创建
}
SYS_RUN(template_demo);

19.4 实验现象

        将程序下载到开发板内(可参考“2.2.5 程序下载运行”章节),打开串口调试助手“\5--开发工具\4-串口调试助手\UartAssist.exe”,波特率设置为115200,实验现象: LED 周期性每秒闪烁 1 次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值