1. 程式人生 > 其它 >基於小熊派Hi3861鴻蒙開發的IoT物聯網學習【三】

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

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

定時器執行機制:

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;//typedefunsignedintuint32_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);

hpmdist編譯----------->HiBurn燒錄--------------------- >執行看log

心有猛虎,細嗅薔薇