Hôm nay mình xin chia sẻ 1 bài hướng dẫn lập trình STM32F1 trên nền tảng IDE của Arduino.
1. Chuẩn bị
Phần cứng:
- KIT hoặc mạch STM32F1
- Mạch nạp ST-Link (Nếu KIT bạn có tích hợp sẵn thì không cần)
Phần mềm:
- IDE Arduino
- Bộ cài đặt và thư viện STM32
Linkdownload: https://drive.google.com/open?id=1oGK8hB1SRIEztYicgIjw5mRaw-kyonE9
2. Tiến hành cài đặt
Bước 1: Cài đặt IDE Arduino, nếu máy bạn đã cài rồi thì bỏ qua bước này.
Mặc định đường dẫn cài đặt sẽ là: C:\Program Files (x86)\Arduino
Bước 2: Giải nén và copy thư mục STM32 vào trong thư mục hardware của Arduino.
![](https://dientuagv.com/wp-content/uploads/2021/09/b2.png)
Bước 3: Kết nối ST-Link với Kit STM32F1 của bạn.
Bước 4: Cài đặt driver:
Chạy 2 file install_drivers.bat và install_STM_COM_drivers.bat rồi đợi đến khi có thông báo Press any key to continue là xong.
![](https://dientuagv.com/wp-content/uploads/2021/09/b4.png)
Bước 5: Vào thư mục C:\Program Files (x86)\Arduino\hardware\STM32\tools\win để chạy 3 file: maple_upload.bat – serial_upload.bat – stlink_upload.bat
![](https://dientuagv.com/wp-content/uploads/2021/09/b5.png)
Bước 6: Copy file rxtxSerial.dll vào thư mục C:\Program Files (x86)\Arduino
![](https://dientuagv.com/wp-content/uploads/2021/09/b6.png)
Bước 7: Tạo 1 thư mục Arduino ở ổ D để chứa file build ra từ Arduino.
Đường dẫn sẽ là D:\Arduino
Bước 8: Mở phần mềm IDE Arduino
Vào File ->Preferences, chọn phần có preferences.txt để vào thư mục chứa file, sau đó mở file này để chỉnh sửa (Nhớ tắt IDE Arduino trước khi chỉnh sửa file và lưu nhé).
![](https://dientuagv.com/wp-content/uploads/2021/09/b8.png)
Thêm dòng build.path = D:\Arduino vào dòng cuối cùng trong file preferences.txt.
![](https://dientuagv.com/wp-content/uploads/2021/09/b8-1.png)
Bước 9: Mở IDE Arduino, vào Tool->Board->Board manager, tìm Arduino SAM Board rồi install để cài.
![](https://dientuagv.com/wp-content/uploads/2021/09/b9.png)
Bước 10: Cấu hình Board và nạp code
Chọn loại Board tương ứng với Chip bạn đang dùng.
![](https://dientuagv.com/wp-content/uploads/2021/09/b10.png)
Chọn Update method là ST-Link.
![](https://dientuagv.com/wp-content/uploads/2021/09/b10-1.png)
Bước 11: Chạy thử chương trình nháy LED. Board mình sử dụng LED ở chân PC13 nên mình sẽ tùy chỉnh chân LED lại tí trong code.
define LED PC13 //Tuy chinh theo KIT
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}