天天看點

基于小熊派Hi3861鴻蒙開發的IoT物聯網學習【三】

軟體定時器:是基于系統Tick時鐘中斷且由軟體來模拟的定時器,當經過設定的Tick時鐘計數值後會觸發使用者定義的回調函數。定時精度與系統Tick時鐘的周期有關。

定時器運作機制:

基于小熊派Hi3861鴻蒙開發的IoT物聯網學習【三】

cmsis_os2的API軟體定時器接口:

⚫ 靜态裁剪:能通過宏關閉軟體定時器功能。

⚫ 軟體定時器建立:osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);

⚫ 軟體定時器啟動:osTimerStart (osTimerId_t timer_id, uint32_t ticks);

⚫ 軟體定時器停止:osTimerStop (osTimerId_t timer_id);

⚫ 軟體定時器删除:osTimerDelete (osTimerId_t timer_id);

⚫ 軟體定時器剩餘Tick數擷取:

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

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

uint32_t exec1, exec2;//typedef unsigned int    uint32_t;      
/***** 定時器1 回調函數 *****/
////arg 是一個void的指針變量,指向一個位址
void Timer1_Callback(void *arg)
{
    (void)arg;
    printf("This is BearPi Harmony Timer1_Callback!\r\n");
}

/***** 定時器2 回調函數 *****/
void Timer2_Callback(void *arg)
{
    (void)arg;
    printf("This is BearPi Harmony Timer2_Callback!\r\n");
}

/***** 定時器建立 *****/
static void Timer_example(void)
{
    osTimerId_t id1, id2;
    uint32_t timerDelay; //延遲時間  unsigned int 類型
    osStatus_t status; //狀态是枚舉值

    exec1 = 1U;
    id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
    if (id1 != NULL)
    {
        // Hi3861 1U=10ms,100U=1S
        timerDelay = 100U;//延遲1s

        status = osTimerStart(id1, timerDelay);
        if (status != osOK)
        {
            // Timer could not be started
        }
    }

    exec2 = 1U;
    id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
    if (id2 != NULL)
    {

        // Hi3861 1U=10ms,300U=3S
        timerDelay = 300U;

        status = osTimerStart(id2, timerDelay);
        if (status != osOK)
        {
            // Timer could not be started
        }
    }
}

APP_FEATURE_INIT(Timer_example);      

hpm dist 編譯----------->HiBurn燒錄--------------------- >運作看log

基于小熊派Hi3861鴻蒙開發的IoT物聯網學習【三】
基于小熊派Hi3861鴻蒙開發的IoT物聯網學習【三】