当前位置:网站首页>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
边栏推荐
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Typescript array out of order output
- PCL least median square method fitting plane
- 618深度复盘:海尔智家的制胜方法论
- Cloud native cicd framework: Tekton
- 虚假的暑假
- Effectively use keywords to increase Amazon sales
- Leetcode -- number of palindromes
- Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
- 忆当年高考|成为程序员的你,后悔了吗?
猜你喜欢

Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification

Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...

Take you ten days to easily complete the go micro service series (I)

How to choose the right kubernetes storage plug-in? (09)

Which software is good for machine vision?

Understand the key technology of AGV -- the difference between laser slam and visual slam

PyC file decompile

隐私计算技术创新及产业实践研讨会:学习

流批一体在京东的探索与实践

The light of ideal never dies
随机推荐
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
Typescript array out of order output
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
Take you ten days to easily complete the go micro service series (I)
大厂面试总结大全
Some problems about MySQL installation
TCP拥塞控制详解 | 2. 背景
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
Aike AI frontier promotion (2.15)
做机器视觉哪个软件好?
忆当年高考|成为程序员的你,后悔了吗?
PCL 最小中值平方法拟合平面
Sim2real environment configuration tutorial
Where can I open computer administrator permissions
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
MySQL min() finds the minimum value under certain conditions, and there are multiple results
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
TCP server communication process (important)
JS learning notes - operators