当前位置:网站首页>PWM breathing lamp
PWM breathing lamp
2022-07-02 16:43:00 【Let everything burn】
Mission :
- Learn timer output PWM, Realization PWM Breathing lights
- understand PWM Principle of breathing lamp
And pwm Knowledge points about controlling breathing lights
The previous one used the delay function to control the steering gear , The delay function will occupy the MCU cup, inefficiency
pwm Pulse width modulator ( Duty cycle ): Control the high and low level
Use the digital output of microprocessor to control the analog circuit
High level LED Lighten up , Low level LED Extinguish
The length of the high level determines LED The brightness of
There should be a complete cycle , The length of high and low levels changes constantly , Cycle length ( frequency ) unchanged , A cycle appears repeatedly
pwm May by stm32 Timer generation in , Advanced timer TIM1, Ordinary timer TIM2,TIM3,TIM4




ccrx Indicates the change position of high and low levels in a cycle
ccrx Adjust the high level up to reduce the column , Adjust the high-level ratio downward to increase 

It can make LED1 or LED2 bright , But two cannot light at the same time
Complete code :
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 initialization arr Reload value psc The prescaled coefficients
GPIO_InitTypeDef GPIO_InitStrue;
TIM_OCInitTypeDef TIM_OCInitStrue;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);// Can make TIM3 And the related GPIO The clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);// Can make GPIOB The clock (LED stay PB0 Pin )
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);// Can make AFIO The clock ( Timer 3 passageway 3 Need to remap to BP5 Pin )
// 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; // Reuse push pull
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz; // Set the maximum output speed
GPIO_Init(GPIOB,&GPIO_InitStrue); //GPIO Port initialization settings
// GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE); // mapping , Remapping is only used for 64、100、144 Single chip microcomputer foot
// When there is no remapping ,TIM3 The four channels of CH1,CH2,CH3,CH4 They correspond to each other PA6,PA7,PB0,PB1
// When partially remapped ,TIM3 The four channels of CH1,CH2,CH3,CH4 They correspond to each other PB4,PB5,PB0,PB1 (GPIO_PartialRemap_TIM3)
// When completely remapped ,TIM3 The four channels of CH1,CH2,CH3,CH4 They correspond to each other PC6,PC7,PC8,PC9 (GPIO_FullRemap_TIM3)
TIM_TimeBaseInitStrue.TIM_Period=arr; // Set auto reload load value
TIM_TimeBaseInitStrue.TIM_Prescaler=psc; // The prescaled coefficients
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up; // The counter overflows up
TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1; // The division factor of the clock , It plays a little delay role , General set to TIM_CKD_DIV1
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue); //TIM3 Initialize settings ( Set up PWM The cycle of )
TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM1; // PWM Pattern 1:CNT < CCR Output effective level at
//TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStrue.TIM_OCPolarity=TIM_OCPolarity_High;// Set polarity - The effective level is : High level
TIM_OCInitStrue.TIM_OutputState=TIM_OutputState_Enable;// Output enable
//
//TIM_OC3Init(TIM3,&TIM_OCInitStrue); //TIM3 The passage of 3 PWM Mode setting
//
// TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_OC4Init(TIM3,&TIM_OCInitStrue); //TIM3 The passage of 4 PWM Mode setting
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable); // Enable preload register
TIM_Cmd(TIM3,ENABLE); // Can make TIM3
}
Be careful :
PWM clock frequency=72000000/(59999+1)*(23+1) = 50HZ (20ms),
Set upAuto load values60000,prescalecoefficient 24
边栏推荐
- PCL point cloud image transformation
- PCL 最小中值平方法拟合平面
- Machine learning perceptron model
- TCP拥塞控制详解 | 2. 背景
- [North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
- Cloud native cicd framework: Tekton
- [fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
- 串口控制舵机转动
- 图书管理系统(山东农业大学课程设计)
- TCP server communication process (important)
猜你喜欢

July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance

串口控制舵机转动

Win11应用商店无法加载页面怎么办?Win11商店无法加载页面

Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~

Yyds dry inventory company stipulates that all interfaces use post requests. Why?

Seal Library - installation and introduction

LeetCode 1. Sum of two numbers
![[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
![[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)](/img/c7/1949894e106036d2b412bcd6f70245.jpg)
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)

总结|机器视觉中三大坐标系及其相互关系
随机推荐
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
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Yolov5 practice: teach object detection by hand
做机器视觉哪个软件好?
流批一体在京东的探索与实践
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
mysql数据库mysqldump为啥没有创建数据库的语句
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
unity Hub 登录框变得很窄 无法登录
PWM控制舵机
Student course selection system (curriculum design of Shandong Agricultural University)
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
SSM integration exception handler and project exception handling scheme
HMS core machine learning service helps zaful users to shop conveniently
Classic quotations
LeetCode 3. Longest substring without duplicate characters
串口控制舵机转动
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!