当前位置:网站首页>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
边栏推荐
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
- Unity Json 编写
- unity Hub 登录框变得很窄 无法登录
- Recalling the college entrance examination and becoming a programmer, do you regret it?
- Seal Library - installation and introduction
- [Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
- Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
- 路由模式:hash和history模式
- TCP server communication process (important)
- ROW_NUMBER()、RANK()、DENSE_RANK区别
猜你喜欢
unity Hub 登录框变得很窄 无法登录
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
LeetCode 2. 两数相加
大厂面试总结大全
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
Data security industry series Salon (III) | data security industry standard system construction theme Salon
LeetCode 2. Add two numbers
unity Hub 登錄框變得很窄 無法登錄
Résumé de l'entrevue de Dachang Daquan
Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
随机推荐
Recalling the college entrance examination and becoming a programmer, do you regret it?
Multi task prompt learning: how to train a large language model?
HMS core machine learning service helps zaful users to shop conveniently
Typescript array out of order output
TypeScript数组乱序输出
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
What is Amazon keyword index? The consequences of not indexing are serious
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
绝对真理和相对真理思考
TCP拥塞控制详解 | 2. 背景
Interview summary of large factories
mysql数据库mysqldump为啥没有创建数据库的语句
LeetCode 3. 无重复字符的最长子串
路由模式:hash和history模式
大厂面试总结大全
unity Hub 登录框变得很窄 无法登录
LeetCode 4. Find the median (hard) of two positive arrays
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
TCP server communication process (important)
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi