当前位置:网站首页>pwm呼吸灯
pwm呼吸灯
2022-07-02 13:22:00 【让一切都燃烧】
任务:
- 学习定时器输出PWM,实现PWM呼吸灯
- 理解PWM呼吸灯的原理
与pwm控制呼吸灯的有关知识点
之前的是使用延时函数控制舵机,延时函数会占用单片机cup,效率低下
pwm脉冲宽度调制器(占空比):控制高低电平
利用微处理器的数字输出对模拟电路进行控制
高电平LED点亮,低电平LED熄灭
高电平的长度决定LED的亮度
要有一个完整的周期,高低电平长度不断变化,周期长度(频率)不变,一个周期循环往复出现
pwm可由stm32中的定时器产生,高级定时器TIM1,普通定时器TIM2,TIM3,TIM4




ccrx表示在一个周期中高低电平的变化位置
ccrx向上调整高电平比列减小,向下调整高电平比例增大

可以实现分别使LED1或LED2亮,但两个不能同时亮
完整代码:
main.c
int main(void)
{
u16 t = 500;
u8 dir = 1;
//delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//uart_init(115200);
LED_Init();
TIM3_PWM_Init(59999,23);
while(1)
{
delay_ms(10);
if(dir)t=t+400;
else t=t-400;
if(t>60000)dir = 0;
if(t==500)dir = 1;
// TIM_SetCompare3(TIM3,t);
TIM_SetCompare4(TIM3,t);
pwm.c
void TIM3_PWM_Init(u16 arr,u16 psc){
//TIM3 PWM初始化 arr重装载值 psc预分频系数
GPIO_InitTypeDef GPIO_InitStrue;
TIM_OCInitTypeDef TIM_OCInitStrue;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能TIM3和相关GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB时钟(LED在PB0引脚)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能AFIO时钟(定时器3通道3需要重映射到BP5引脚)
// GPIO_InitStrue.GPIO_Pin=GPIO_Pin_0; // TIM_CH3
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_1; // TIM_CH4
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP; // 复用推挽
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz; //设置最大输出速度
GPIO_Init(GPIOB,&GPIO_InitStrue); //GPIO端口初始化设置
// GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE); //映射,重映射只用于64、100、144脚单片机
//当没有重映射时,TIM3的四个通道CH1,CH2,CH3,CH4分别对应PA6,PA7,PB0,PB1
//当部分重映射时,TIM3的四个通道CH1,CH2,CH3,CH4分别对应PB4,PB5,PB0,PB1 (GPIO_PartialRemap_TIM3)
//当完全重映射时,TIM3的四个通道CH1,CH2,CH3,CH4分别对应PC6,PC7,PC8,PC9 (GPIO_FullRemap_TIM3)
TIM_TimeBaseInitStrue.TIM_Period=arr; //设置自动重装载值
TIM_TimeBaseInitStrue.TIM_Prescaler=psc; //预分频系数
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up; //计数器向上溢出
TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1; //时钟的分频因子,起到了一点点的延时作用,一般设为TIM_CKD_DIV1
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue); //TIM3初始化设置(设置PWM的周期)
TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM1; // PWM模式1:CNT < CCR时输出有效电平
//TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStrue.TIM_OCPolarity=TIM_OCPolarity_High;// 设置极性-有效电平为:高电平
TIM_OCInitStrue.TIM_OutputState=TIM_OutputState_Enable;// 输出使能
//
//TIM_OC3Init(TIM3,&TIM_OCInitStrue); //TIM3的通道3 PWM 模式设置
//
// TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_OC4Init(TIM3,&TIM_OCInitStrue); //TIM3的通道4 PWM 模式设置
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能预装载寄存器
TIM_Cmd(TIM3,ENABLE); //使能TIM3
}
注意:
PWM时钟频率=72000000/(59999+1)*(23+1) = 50HZ (20ms),
设置自动装载值60000,预分频系数24
边栏推荐
- Sim2real environment configuration tutorial
- Yyds dry inventory company stipulates that all interfaces use post requests. Why?
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
- Yyds dry inventory executor package (parameter processing function)
- (practice C language every day) the sum of the nearest three numbers
- Data security industry series Salon (III) | data security industry standard system construction theme Salon
- 忆当年高考|成为程序员的你,后悔了吗?
- ROW_NUMBER()、RANK()、DENSE_RANK区别
- 曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
- 大厂面试总结大全
猜你喜欢

台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...

The light of ideal never dies

Maui learning road (III) -- in depth discussion of winui3

TCP拥塞控制详解 | 2. 背景

Route service grid traffic through two-level gateway design

Typescript array out of order output

SQL solves the problem of continuous login deformation holiday filtering

一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别

Kubernetes family container housekeeper pod online Q & A?

Which software is good for machine vision?
随机推荐
Unity Json 编写
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
HMS core machine learning service helps zaful users to shop conveniently
Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
Vscode设置标签页多行显示
dried food! Understand the structural vulnerability of graph convolution networks
618深度複盤:海爾智家的制勝方法論
Sqlserver queries which indexes are underutilized
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
Everyone Xinfu builds: a one-stop intelligent business credit service platform
JS learning notes - first acquaintance
LeetCode 6. Z 字形变换 (N字形变换)
SSM integration exception handler and project exception handling scheme
大厂面试总结大全
月报总结|Moonbeam6月份大事一览
Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
通过两级网关设计来路由服务网格流量