STM32 board:NUCLEO-F207ZG
https://hackmd.io/uJrNrB3rTgKHdyLhTJY74Q
online IDE操作、ST-Link驅動安裝、Tera Term設置
https://hackmd.io/@Kirashi/SJR-2slKt
在網路搜尋Mbed API,會找到網站
https://os.mbed.com/docs/mbed-os/v6.15/apis/index.html
通常開發函數庫(Library)或板子(Board)的開發者,會提供使用者API,讓使用者不需要了解函數的實作細節,只需要知道函數如何使用,以提高使用者的編程效率。
Mbed API 提供I/O、Serial等周邊設備的控制方法、函數的輸入輸出、函數使用範例等。
API:https://os.mbed.com/docs/mbed-os/v6.15/apis/digitalout.html
DigitalOut (PinName pin)
讓DigitalOut物件能與實際腳位連結,PinName參照Pinout圖。
write (int value)
輸出高準位(1)或低準位(0)。
is_connected ()
確認物件是否與實際腳位有連結了。
DigitalOut& operator= (int value)
例如:testled = 1;
其中testled是DigitalOut物件
Learn More →
#include "mbed.h"
DigitalOut blinkled(LED1);
int main()
{
// Blink LED
while(1) {
blinkled.write(1);
wait(1);
blinkled.write(0);
wait(1);
}
}
#include "mbed.h"
DigitalOut blinkled(LED1);
int main()
{
// Blink LED
while(1) {
blinkled = !blinkled;
wait(1);
}
}
Learn More →
API:https://os.mbed.com/docs/mbed-os/v6.15/apis/digitalin.html
DigitalIn (PinName pin)
讓DigitalIn物件能與實際腳位連結,PinName參照Pinout圖。(設定數位輸入接腳用)
int read ()
讀取pin電壓準位。回傳1或0。(高準位或低準位)。
is_connected ()
確認物件是否與實際腳位有連結了。
mode(PinMode pull)
設定輸入模式。pull={PullUp, PullDown, PullNone, OpenDrain}
operator int()
An operator shorthand for read()
如果button是DigitalIn,led是DigitalOut,下列兩個敘述等價:
led = button;
led.write(button.read());
Learn More →
#include "mbed.h"
DigitalIn button(USER_BUTTON); //the button on your board
DigitalOut led(LED1);
int main()
{
// 檢查物件button是否完成pin連結
if(button.is_connected()) {
printf("button is connected and initialized! \n\r");
}
button.mode(PullNone); // 浮接
while(1) {
led.write(button.read()); // member function expression
wait(0.25);
}
}
#include "mbed.h"
DigitalIn button(USER_BUTTON); //the button on your board
DigitalOut led(LED1);
int main()
{
button.mode(PullNone); // 浮接
while(1) {
led = button; // operator expression
wait(0.25);
}
}
Learn More →
Learn More →
#include "mbed.h"
RawSerial pc(USBTX, USBRX); // tx, rx
int main() {
pc.printf("Hello World!\n\r");
while(1) {
pc.putc(pc.getc()); // echo input back to terminal
}
}
Learn More →
※有字幕
Learn More →
Learn More →
Hint:
※以下為Pointer物件導向宣告方式
#include "mbed.h"
/////Variable Declaration/////
DigitalOut *output;
DigitalOut *X[1];
int main(){
output = new DigitalOut(LED1);
X[0] = output;
while(1){
*X[0] = !*X[0];
wait(0.5);
}
}
start():開始計時。
stop():停止計時。
reset():重新計時。
read():讀取時間,浮點數型態輸出,單位為秒。
read_ms():讀取時間,整數型態輸出,單位為毫秒。
#include "mbed.h"
Timer timer;
float t1;
int t2;
int main()
{
timer.start(); // 計時開始
while(1) {
t1 = timer.read(); // 讀取時間 浮點表示
t2 = timer.read_ms(); // 讀取時間 整數表示
printf("%f seconds \r\n",t1);
printf("%d miliseconds \r\n",t2);
printf("\r\n");
if(t2 > 10000)
timer.stop(); //計時停止
wait(0.5);
}
}
Hint: 使用timer時間差決定按下和放開的情況
Learn More →
除了能觸發中斷的Timer外,Mbed也提供了另外三種Interupt Class API,給開發者利用。
InteruptIn:讀取腳位信號,上、下緣時引發中斷。
API:https://os.mbed.com/docs/mbed-os/v5.15/apis/interruptin.html
Ticker:每經過一段時間就引發中斷。
API:https://os.mbed.com/docs/mbed-os/v5.15/apis/ticker.html
Timeout:延遲一段時間後引發中斷。
API:https://os.mbed.com/docs/mbed-os/v5.15/apis/timeout.html
在使用Interupt Class時需要注意,盡量避免在ISR中使用wait()、無限迴圈(infinite while loop)、blocking calls(除非指派的任務完成否則會持續等待的函數,例如:cin函數)。也不要使用printf、new、malloc函數。詳細參照API中Warnings and notes條目。
Learn More →
#include "mbed.h"
InterruptIn button(USER_BUTTON);
DigitalOut led(LED1);
DigitalOut blinkled(LED3);
void change() {
led = !led;
}
int main() {
button.rise(&change); // 上緣觸發執行flip()
// LED3 每2秒亮暗一次
while(1) {
blinkled = !blinkled;
wait(1);
}
}
Learn More →
#include "mbed.h"
Ticker tick[2];
DigitalOut blinkled[2] = {LED1, LED3};
void change0() {
blinkled[0] = !blinkled[0];
}
void change1() {
blinkled[1] = !blinkled[1];
}
int main() {
tick[0].attach(&change0,0.5);
tick[1].attach(&change1,0.8);
while(1){ }
}
Learn More →
Learn More →
課後問題: