stm32f1定时器8输出pwm
时间: 2023-09-15 12:22:47 浏览: 100
stm32输出pwm
定时器8输出PWM的代码如下所示:
```c
#include "PWM.h"
int main() {
u16 led0pwmval = 0;
u8 dir = 1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // NVIC初始化分组
LED_Init(); // LED初始化
delay_init(); // 延时初始化
TIM_PWM_Init(899, 0); // 不分频。PWM频率=72000000/900=80Khz
while(1) {
delay_ms(10);
if(dir) {
led0pwmval++;
} else {
led0pwmval--;
}
if(led0pwmval > 300) {
dir = 0;
}
if(led0pwmval == 0) {
dir = 1;
}
// 往CCR2寄存器写入不断变化的led0pwmval值,调整比较值,就能输出不断变化的占空比了
TIM_SetCompare2(TIM8, led0pwmval);
}
}
```
在这段代码中,我们使用了定时器8来输出PWM信号。首先,我们需要进行一些初始化工作,包括设置NVIC分组、LED初始化和延时初始化。然后,我们调用TIM_PWM_Init函数来初始化定时器8,设置不分频,使得PWM频率为80KHz。接下来,我们进入一个循环,每隔10毫秒调整led0pwmval的值,从而改变占空比。最后,我们使用TIM_SetCompare2函数将led0pwmval的值写入CCR2寄存器,实现PWM输出。
#### 引用[.reference_title]
- *1* [STM32F103C8T6---定时器1&定时器8输出PWM信号注意事项](https://blog.csdn.net/weixin_51134981/article/details/129971600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [STM32F1定时器-PWM输出](https://blog.csdn.net/weixin_46251230/article/details/126796005)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文