# 乒乓球專題 __ RT-thread
###### tags: `專題`, `110-1`, `2021`, `110-2`, `2022`
## Table Of Content
[TOC]
# rt-thread 使用 PWM
###### [參考 link](https://club.rt-thread.org/ask/article/3229.html), [參考 link](https://blog.csdn.net/qq_25186745/article/details/112885020)
1. 
2. The definition is given in board.h
This place needs to be added, **BSP_USING_PWM1_CH1**, such channel macros, so that the channel can **be opened through the pwm_get_channel() function** in the drv_pwm.c file.
Of course, if there is no drv_pwm.h and drv_pwm.c in the project, it can be copied from the rt thread source file.
- 
- 
3. Add the configuration function generated by cubemx
In the stm32 bare metal configuration peripherals, there are nothing more than three steps
1. Configure the pins
2. Enable the clock (pin clock and peripheral clock)
3. Configure the peripheral
- rt thread通过使用rt device的方法,帮你封装了第三步,配置外设,而前两个步骤就需要自己配置
- 官方给出需要拷贝void HAL_TIM_Base_MspInit(TIM_HandleTypeDef htim_base) 和void HAL_TIM_MspPostInit(TIM_HandleTypeDef htim)这两个函数到board.c中
1. 我的 TIMER 有定義如下(TIMER3)

2. CubeMX




1. 生成代碼
2. 得到的 source Code
3. 复制 **void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)** 和 **void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)** 到board.c文件
1. 從 C:\RT-ThreadStudio\workspace\STM32F429_0103-v2\cubemx\Src\stm32f4xx_hal_msp.c 找到的這兩個 functions

2. 把這兩個函式複製進入 board.c

3. 然后打开stm32f4xx_hal_config.h 取消掉 #define HAL_TIM_MODULE_ENABLED的屏蔽
- 
## 硬體
### power-電源供應器--SS-400ET

- 3.3V
## stm32f429 discovery 開發環境建立
在運行應用前,應該先做以下事情:
1. 安裝首選IDE
- IDE是?
- Integrated Development Environment,整合開發環境
2. 安裝ST-LINK V2驅動
3. 下載STM32F429I-discovery固件
4. 確認如下圖連接STM32F429I-Discovery開發板


- **參考 [STM32F429 Discovery kit](https://hackmd.io/@GoroYeh56/GPIO#STM32F429-Discovery-kit) 與 [成大資工wiki-GPIO介紹](http://wiki.csie.ncku.edu.tw/embedded/GPIO#stm32f4xx-gpio%E7%89%B9%E6%80%A7)**
- ### Hardware Block

- 紅色框框是可以使用的 debugger
- ### Debugger

## PWM控制馬達
- Pulse-Width Modulation ,脈衝寬度調變(脈衝時間調變)。
- 又稱pulse-durarion modulation(PDM)
- 用數字輸出對類比電路進行控制,將脈波轉換為類比訊號的技術。
- 轉換後週期固定(頻率大小不變),而脈波的工作週期會依類比訊號大小而改變,使整理平均電壓上升或下降。
- STM32內部要產生PWM訊號時,需要透過Timer來實現。
### PWM原理
- 由左到右為三個不同的脈衝訊號

- 週期:10ms;頻率:100Hz
- 佔空比:一個脈波週期內,高電平的時間站整理週期的比例
purpose: 為了實際改變週期內的電壓值。
ex.左: 40%;中: 60%;右:80%
(if 為50%,則表示實際輸出的電壓為原電壓的一半)
### PWM控制電機的原理
- 透過調製器給電機提供一個頻率固定、脈衝寬度可調的脈衝電。
- 脈衝寬度↑ → 佔空比↑ → 電機轉速↑
- 電機轉速 ∝ 平均電壓

### Clock control
- 學習Timer前, 先瞭解系統clock是如何產生的, 以及給Timer的clock值。
### micro-controler的PWM控制原理
- 透過定時器(clock)實現。
- 控制轉速
- 調電壓
- ex.

- 斬波
- 非連續工作,而是一下子有能量(工作),一下子沒有能量(停止工作)。
- 工作的時候按額定工作電壓送電的,不會輸出無力。
- 又由於PWM的工作頻率高,肉眼察覺不到,實際上變慢。同理,LED燈的亮度調節和這一樣。
- 有效避免轉速降低,但沒有動力的問題。
- ex.

### STM32F429 - [ref.](https://www.st.com/resource/en/reference_manual/DM00031020-.pdf)
#### other source:
- ART-PI調試紀錄---PWM舵機使用 https://blog.csdn.net/qq_16519885/article/details/112062403
- PWM輸出實驗: https://open.163.com/newview/movie/free?mid=QETIPAT06&pid=AETIOHK7R
- STM32F4_PWM設計: https://blog.csdn.net/qq_33974167/article/details/88854865
- STM32F4輸出PWM波_STM32F4 https://blog.csdn.net/weixin_39750190/article/details/110501944
### 成大資工wiki - [link](http://wiki.csie.ncku.edu.tw/embedded/PWM#introduction)
## RT-thread 文檔中心
### Clock-Manager
- clock節拍的長度可以依據RT_TICK_PER_SECOND 的定義調整,等於1/RT_TICK_PER_SECOND 秒。
### Pin
#### Pin腳常用函數
- 
- 
####
## rt-thread PWM-API
- https://github.com/RT-Thread/rtthread-manual-doc/blob/master/device/pwm/pwm.md
- https://www.rt-thread.org/document/api/group__pwm.html
- example: PWM實現LED燈
- https://www.rt-thread.org/document/api/group__pwm__sample.html
-
## RT-thread LED
```C=1
PLEASE DEFINE the LED0 pin for your board, such as: PA5 */
#define LED0_PIN GET_PIN(A, 5)
int main(void)
{
int count = 1;
/* set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
while (count++)
{
/* set LED0 pin level to high or low */
rt_pin_write(LED0_PIN, count % 2);
LOG_D("Hello RT-Thread!");
rt_thread_mdelay(1000);
}
return RT_EOK;
}
```
## RT-thread PWM
``` C=1
/*
* 程序清單:這是一個PWM 設備使用例程,導出pwm_led_sample 命令到控制終端,命令調用格式:pwm_led_sample。
* 程序功能:通過PWM 設備控制LED 燈的亮度,可以看到LED不停的由暗變到亮,然後又從亮變到暗。
*/
#include <rtthread.h>
#include <rtdevice.h>
#define LED_PIN_NUM 57
#define PWM_DEV_NAME "pwm3"
#define PWM_DEV_CHANNEL 4
struct
static
{
rt_uint32_t
period = 500000;
dir = 1;
pulse = 0;
rt_pin_mode/* 設置LED引腳腳模式為輸出*/
rt_pin_write/* 拉高LED引腳*/
/* 查找設備*/
pwm_dev = (
if{
rt_kprintf
return
}
/* 設置PWM週期和脈衝寬度默認值*/
rt_pwm_set
/* 使能設備*/
rt_pwm_enable
while
{
rt_thread_mdelay
if{pulse += 5000; }
else{pulse -= 5000;}
if{dir = 0;}
if{dir = 1;}
/* 設置PWM週期和脈衝寬度*/
rt_pwm_set
}
}
/* 導出到msh 命令列表中*/
MSH_CMD_EXPORT
```
## 步進馬達(減速馬達)

###### --help -Hardrst
###### vim
###### code
## Arduino
### micro-controller-> MCU
- 可程式化的device(IC)
### Serial communication
### Advanced Arduino programming
### 伺服馬達&可變電阻
###### [Arduino ref](https://sites.google.com/view/rayarduino/%E4%BC%BA%E6%9C%8D%E9%A6%AC%E9%81%94%E5%92%8C%E5%8F%AF%E8%AE%8A%E9%9B%BB%E9%98%BB)
###### [Arduino ref-pdf](http://www.ycvs.ntpc.edu.tw/ezfiles/0/1000/img/112/315568714.pdf)
``` Arduino = 1
#include <Servo.h> //含入伺服馬達程式庫 Servo.h
Servo myservo; //命名伺服馬達為 myservo
int potpin = 0; //可變電阻中間腳接 Arduino A0
int val; // 宣告整數 val
void setup() {
myservo.attach(13); //伺服馬達黃色線接 Arduino pin 13
}
void loop() {
val = analogRead(potpin); //讀取可變電阻的電位值 0-1023
val = map(val, 0, 1023, 0, 180); //轉換電位值 0-1023 至角度0-180 度, 因為伺服馬達的旋轉為 0-180 度
myservo.write(val); //轉動伺服馬達至角度 val
delay(15);
}
```
### DC motor control(直流馬達控制)
### Arduino PWM

## 參考資料
- https://blog.csdn.net/weixin_36036925/article/details/112016184
- https://www.cnblogs.com/wangh0802PositiveANDupward/archive/2012/12/29/2839313.html