stm32f103呼吸灯
时间: 2023-08-05 17:08:28 浏览: 121
stm32f103单片机呼吸灯实验
stm32f103呼吸灯是通过PWM脉冲宽度调制实现的。在硬件方面,需要进行GPIO的初始化和定时器的配置。在编程方面,需要添加头文件pwm.h,并在其中定义PWM_Init函数。在主函数中,通过设置pwmValue的值和dir的值来控制呼吸灯的亮度变化。具体代码如下:
```c
#include "stm32f10x.h"
#include "delay.h"
#include "led.h"
#include "tim.h"
#include "key.h"
#include "pwm.h"
int main(void) {
u16 pwmValue = 0;
u8 dir = 0;
delay_init();
PWM_Init(999, 719);
while(1) {
if(dir) {
if(pwmValue > 550)
TIM_SetCompare2(TIM3, --pwmValue);
else
dir = 0;
} else {
if(pwmValue < 990)
TIM_SetCompare2(TIM3, ++pwmValue);
else
dir = 1;
}
delay_ms(3);
}
}
```
这段代码中,通过不断改变pwmValue的值来控制TIM3的比较值,从而实现呼吸灯的效果。当dir为1时,pwmValue递减,灯光逐渐变暗;当dir为0时,pwmValue递增,灯光逐渐变亮。通过delay_ms函数来控制呼吸灯的变化速度。
#### 引用[.reference_title]
- *1* [stm32f103呼吸灯(PWM脉冲宽度调制)](https://blog.csdn.net/zmhDD/article/details/111942507)[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* [STM32F103入门 | 12.PWM实现呼吸灯](https://blog.csdn.net/Chen_rr/article/details/89735419)[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 ]
阅读全文