2025 成大嵌入式作業系統分析與實作 Lab1
github
題目說明
- Must using MultiTask, with Inter-Task Communication (ITC) mechanism
- Two Tasks: LED-task and Button-task
- LED-task will have two states (S1, S2)
- S1: First, the Red LED lights up for 1 second, followed by the Orange LED
lighting up for 1 second (with the Red LED turned off), then the Green LED
lights up for 1 second (with both the Red and Orange LEDs turned off). This
sequence repeats, cycling through the Red, Orange, and Green LEDs.
- S2: Only Orange LED is blinking (2 second ON, 2 second OFF, …)
- Button-task: If the button is pressed, the LED-task will switch to another state
(S1→S2).
- Debounce handling
- Edge detection handling
bounce and debounce
bounce: 當機械按鈕被按下時,內部的機械接觸可能會導致多次開關信號,這稱為「彈跳效應」。
debounce 是用來限制函數在短時間內多次執行的機制,當偵測到按鈕按下時,先忽略短時間內的變化,直到訊號穩定,才確認一次按鍵輸入。

使用 vTaskDelay()
讓程式延遲一段時間再讀取穩定後的狀態。

實作
FreeRTOS設定
STM32 Port 設定 (修改 .ioc檔
)


將PD12、PD13、PD14、PA0做對應設定
左鍵: 選擇模式
右鍵: 重新命名
程式碼
- include RTOS相關程式碼 (順序不能調換,會報錯)
- 因為要使用 message queue,所以也要 include
- 宣告 message queue
- 藍色按鈕控制函式
vTaskBlueButtonControl()
透過訊息佇列 xQueueSend()
通知另一個 LED 控制任務 (vTaskLEDControl
) 來切換 LED 狀態
- LED 狀態函式
- led_state == 1 時,LED 會紅→橙→綠 依序閃爍,每顆 LED 亮 1 秒
- led_state == 0 時,橙色 LED 會 閃爍 2 秒開、2 秒關