🤖
LinkIt 7697 BlocklyDuino 使用指南
  • LinkIt 7697 BlocklyDuino 使用指南
    • 認識 LinkIt 7697
    • 認識 BlocklyDuino
    • BlocklyDuino v2 & v3 比較
    • 安裝開發環境 (v2)
      • 安裝 Arduino 編輯器
      • 安裝驅動並連接開發板
      • 安裝 BlocklyDuino 編輯器
      • 連結兩個編輯器並上傳程式
    • 安裝開發環境 (v3)
    • BlocklyDuino 基礎操作
    • 實作範例教學
      • A01. 內建 LED 閃爍
      • A02. 外接 LED 閃爍
      • A03. 外接 LED 呼吸燈
      • A04. RGB LED 顏色變化
      • A05. 外部按鈕控制開關
      • A06. 蜂鳴器播放聲音
      • A07 .可變電阻控制電壓輸出
      • A08. 伺服馬達控制
      • A21. DHT: 溫濕度感應器
      • A22. HTU21D: 溫溼度感應器
      • A23. PMSX003: PM2.5 感測器
      • A24. HC-SR04P: 超音波測距
      • A25. ADXL345: 加速規(動態)
      • A26. ADXL345: 加速規(手勢)
      • A27. 1602/2004: 液晶顯示模組
      • A28. WS2812: RGB 彩色燈條
      • A29. MFRC522: RFID
      • B00. Grove 相關基礎知識
      • B01. Grove 光感應器
      • B02. Grove 超音波測距感應器
      • B03. Grove 溫溼度感應器
      • B04. Grove 繼電器
      • B05. Grove LED 燈條
      • B06. Grove OLED 顯示器
      • C01. 雲端控制 LED 開關
      • C02. 上傳溫溼度到雲端
      • D01. 手機透過 BLE 讀寫裝置資料
      • E01. 遙控器: 按鈕控制 LED
    • 資源鏈結
  • LinkIt 7697 - Arduino IDE 開發指南
    • 環境設定
      • 設定 Arduino IDE
      • 將 LinkIt 7697 連接到電腦
        • 在 Windows 上安裝 CP2102N 驅動程式
        • 在 macOS 上安裝 CP2102N 驅動程式
      • 執行第一個程式
    • 開發指南
      • GPIO
      • UART
      • ADC
      • EINT (外部中斷 / External Interrupt)
      • I2C
      • SPI
      • EEPROM
      • Timer
      • Flash (索引式儲存空間)
      • RTC (Real-Time Clock)
      • Software Serial
      • 使用 Wi-Fi 函式庫
      • 使用 MCS 函式庫
        • 連接到 MCS
        • 連接到 MCSLite
          • 建立可透過 MCS Lite 控制的 Beacon
        • MCS 函式庫 API 使用手冊
          • MCSDevice
          • MCSLiteDeviceMCSLiteDevice
          • MCSDataChannel 相關類別
      • 使用 BLE 低功耗藍牙
        • 藍牙信標
        • 主控裝置
        • 周邊裝置
        • 已知限制
        • LBLE API 指南
          • LBLE
          • LBLECentral
          • LBLEPeripheral
          • LBLEAdvertisementData
          • LBLEService
          • LBLECharacteristic
          • LBLEAddress
          • LBLEUuid
          • LBLEClient
      • 使用 LinkIt Remote
        • LRemote API
        • LRemote 控制元件 API
      • LinkIt 7697 腳位的初始狀態
      • LinkIt 7697 供電說明
      • 更新 Bootloader 與韌體
      • Flash 燒錄工具
      • FOTA 更新
      • LinkIt 7697 的功能限制
      • 自行編譯 Arduino Package
      • 回報問題
    • 週邊元件連接教學
      • SMD 按鈕
      • RGB 三色 LED
      • 可變電阻
      • 九軸感應器 (MPU-9250)
      • 溫度與濕度感應器 (AM2302)
      • 超音波距離感應器
      • 光感應器 (LM358)
      • 0.96 吋 OLED 顯示模組 (SSD1308Z)
      • 長條型 LED 顯示模組 (MY9221)
      • 繼電器
      • 伺服馬達 (EMAX 9g ES08A Servo)
      • 使用 74HC595 驅動七段顯示器
      • 使用 MAX7219 驅動七段顯示器
      • 使用 MAX7219 驅動 8x8 矩陣式顯示器
      • 使用 OLED 模組 (如 SH1106 / SSD1306... 等模組)
      • 使用 PCF8574 / PCF8574A 驅動 1602 LCD
    • 開發套件與擴充板
      • Grove Starter Kit
      • Robot Shield
        • OTTO97
    • 下載
    • 資源連結
    • BSP 版本更新資訊
  • LinkIt 7697 - Development Guide for Arduino IDE
    • Environment Setup
      • Setup Arduino IDE
      • Connecting LinkIt 7697 to Computer
        • Install CP2102N Driver on Windows
        • Install CP2102N Driver on macOS
      • Run Your First Sketch
    • Developer Guide
      • GPIO
      • UART
      • ADC
      • External Interrupt
      • I2C
      • SPI
      • EEPROM
      • Timer
      • Flash (Key-Pair Storage)
      • RTC (Real-Time Clock)
      • Software Serial
      • Using the Wi-Fi Library
      • Using MCS Library
        • Connecting to MCS
        • Connecting to MCSLite
          • Build a Beacon controlled by MCS Lite
        • MCS Library API Reference
          • MCSDevice
          • MCSLiteDevice
          • MCSDataChannel Classes
      • Using Bluetooth
        • Beacons
        • Central Devices
        • Peripheral Devices
        • Limitations of LBLE library
        • LBLE Library API Guide
          • LBLE
          • LBLECentral
          • LBLEPeripheral
          • LBLEAdvertisementData
          • LBLEService
          • LBLECharacteristic
          • LBLEAddress
          • LBLEUuid
          • LBLEClient
      • Using LinkIt Remote
        • LRemote
        • LRemote Control Classes
      • Initial Pin State of LinkIt 7697
      • Powering the LinkIt 7697
      • Update Bootloader and Firmware
      • Flash Uploading Tool
      • FOTA Update
      • Limitations of LinkIt 7697
      • Source Code of Arduino Package
      • Issue Report
    • Tutorial
      • SMD Buttons
      • RGB LED
      • Potentiometer
      • IMU 9DOF v2.0 (MPU-9250)
      • Temperature and Humidity Sensor Pro (AM2302)
      • Grove Ultrasonic Ranger
      • Light Sensor (LM358)
      • Grove OLED Display 0.96" (SSD1308Z)
      • LED Bar v2.0 (MY9221)
      • Relay
      • Mini Servo (EMAX 9g ES08A Servo)
      • Driving 7-segment Displays with 74HC595
      • Driving 7-segment Displays with MAX7219
      • Driving 8x8 Dot Matrices with MAX7219
      • Using OLED module (SH1106 / SSD1306... etc.)
      • Driving 1602 LCD with PCF8574 / PCF8574A
    • Kits and Shields
      • Grove Starter Kit
      • Robot Shield
        • OTTO97
    • Download
    • Resources
    • BSP Release Notes
Powered by GitBook
On this page
  • 產生 Timer 實體
  • 初始化
  • 啟動 Timer
  • 中止 Timer
  • 釋放資源
  • 範例程式
  1. LinkIt 7697 - Arduino IDE 開發指南
  2. 開發指南

Timer

PreviousEEPROMNextFlash (索引式儲存空間)

Last updated 3 years ago

為了讓 Arduino 開發者也能使用 timer 功能,LinkIt 7697 Arduino BSP 提供 LTimer 類別將原生 LinkIt SDK 的 timer 功能開放出來。實際應用請參考 IDE 中的 File / Examples / LTimer / BlinkLED 範例,該範例透過使用兩個 timer,分別控制板上 LED 與外部 LED (連接至 P6 腳位如下圖),使其各以 500ms 與 250ms 的間隔閃爍。接下來將說明該範例是如何構成、以及各 API 所對應的功能。

產生 Timer 實體

首先加入 LTimer 類別的 header file:

#include "LTimer.h"

開發者在程式裡最多可同時使用兩個 timer,這兩個 timer 對應的 enum ID 為 LTIMER_0 和 LTIMER_1。使用 timer 前要先產生出 timer 的實體,以下程式碼會產生出 LTIMER_0 與 LTIMER_1 的兩個 timer 實體:

LTimer timer0(LTIMER_0);
LTimer timer1(LTIMER_1);

初始化

建立 timer 實體後,接下來要進行初始化的動作。在 Arduino 的 setup() 函式裡,呼叫 timer 的 .begin() 函式來進行初始化:

timer0.begin();
timer1.begin();

啟動 Timer

初始化完成後,就可以呼叫 .start() 函式來啟動 timer。啟動 timer 需要四個參數:

LTimerStatus start(
  uint32_t timeoutMS,
  LTimerMode timerMode,
  ltimer_callback_t callbackFunc,
  void *userData
);

參數的說明如下:

  • [IN] timeoutMS:Timer 倒數的時間,單位為微秒 (millisecond)。

  • [IN] timerMode:運作模式。可設定為僅運作一次 (LTIMER_ONESHOT_MODE) 或是反覆重啟 (LTIMER_REPEAT_MODE)。在運作一次的模式下,當 timer 倒數完後,callback 函式僅會被呼叫一次;而在反覆重啟模式下,callback 函式每 timeoutMS 微秒就會被呼叫一次,直到 timer 被停止或是釋放。

  • [IN] callbackFunc:當 timer 倒數完成後會呼叫的 callback 函式。

  • [IN] userData:當 callback 函式被呼叫時伴隨帶入的資料結構。

Callback 函式的原型宣告如下:

typedef void (*ltimer_callback_t)(void *user_data);

其中的 user_data 輸入參數即是啟動 timer 時帶入 .start() 函式裡的 userData 參數。

中止 Timer

在運作一次的模式下,因為 timer 只會倒數一次,所以不需做額外動作將 timer 停止。不過在反覆重啟的模式下,開發者可能因應用情境會有將 timer 停下來的需求。藉由呼叫 .stop() 函式即可中止 timer 的運作。若之後還要繼續 timer,再呼叫一次 .start() 就可以恢復 timer 運行。

釋放資源

當程式確定不再需要 timer 時,可呼叫 .end() 函式以釋放配置給 timer 的資源。

範例程式

#include "Arduino.h"
#include "LTimer.h"

int led0 = 7;
int led1 = 6;
int val0 = 0;
int val1 = 0;

// instantiation
LTimer timer0(LTIMER_0);
LTimer timer1(LTIMER_1);

// callback function for timer0
void _callback0(void *usr_data)
{
  val0 = !val0;
  digitalWrite(led0, val0);
}

// callback function for timer1
void _callback1(void *usr_data)
{
  val1 = !val1;
  digitalWrite(led1, val1);
}

void setup() {
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  Serial.begin(115200);

  // initialization
  timer0.begin();
  timer1.begin();

  // start the execution
  timer0.start(500, LTIMER_REPEAT_MODE, _callback0, NULL);
  timer1.start(250, LTIMER_REPEAT_MODE, _callback1, NULL);
}

void loop() {
}