当前位置:网站首页>pwm呼吸燈
pwm呼吸燈
2022-07-02 16:43: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
边栏推荐
- Take you ten days to easily complete the go micro service series (I)
- Effectively use keywords to increase Amazon sales
- Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
- Seal Library - installation and introduction
- [error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
- [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
- 学生选课系统(山东农业大学课程设计)
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- [fluent] dart data type string type (string definition | string splicing | string API call)
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
猜你喜欢
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
unity Hub 登录框变得很窄 无法登录
Set the background picture in the idea (ultra detailed)
Vscade set multi line display of tab
What is normal distribution? What is the 28 law?
SSM整合-异常处理器及项目异常处理方案
PCL point cloud image transformation
How to solve the failure of printer driver installation of computer equipment
Remove the underline in router link
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
随机推荐
Ranger (I) preliminary perception
618 deep resumption: Haier Zhijia's winning methodology
⌈ 2022 ⌋ how to use webp gracefully in projects
Remove the underline in router link
Privacy computing technology innovation and industry practice seminar: Learning
MySQL min() finds the minimum value under certain conditions, and there are multiple results
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
Kubernetes three open interfaces first sight
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
LeetCode 3. Longest substring without duplicate characters
(practice C language every day) the sum of the nearest three numbers
Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
Yyds dry inventory executor package (parameter processing function)
Résumé de l'entrevue de Dachang Daquan
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
Analysis of how to prevent virus in industrial computer
LeetCode 6. Z 字形变换 (N字形变换)