当前位置:网站首页>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
边栏推荐
- 总结|机器视觉中三大坐标系及其相互关系
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
- Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
- PCL least median square method fitting plane
- 由ASP.NET Core根据路径下载文件异常引发的探究
- Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
- 绝对真理和相对真理思考
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
- IDEA中设置背景图片(超详细)
- La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
猜你喜欢
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
TCP server communication process (important)
Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
HMS core machine learning service helps zaful users to shop conveniently
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
How to choose the right kubernetes storage plug-in? (09)
SSM integration exception handler and project exception handling scheme
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
关于mysql安装的一些问题
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
随机推荐
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
忆当年高考|成为程序员的你,后悔了吗?
LeetCode 5. 最长回文子串
总结|机器视觉中三大坐标系及其相互关系
PyC file decompile
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
云原生的 CICD 框架:Tekton
JS learning notes - process control
mysql min() 求某条件下最小的值出现多个结果
Vscade set multi line display of tab
Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 3. 无重复字符的最长子串
去除router-link中的下划线
Written by unity Jason
TCP拥塞控制详解 | 2. 背景
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
电脑管理员权限在哪里可以打开
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time