当前位置:网站首页>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
边栏推荐
- 电脑设备打印机驱动安装失败如何解决
- LeetCode 3. 无重复字符的最长子串
- Data security industry series Salon (III) | data security industry standard system construction theme Salon
- LeetCode 6. Zigzag transformation (n-shaped transformation)
- Interview summary of large factories
- 历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
- Student course selection system (curriculum design of Shandong Agricultural University)
- Vscade set multi line display of tab
- Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
- Cloud native cicd framework: Tekton
猜你喜欢

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...

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

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

La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter

Some problems about MySQL installation

A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits

What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?

Summary | three coordinate systems in machine vision and their relationships

dried food! Understand the structural vulnerability of graph convolution networks

路由模式:hash和history模式
随机推荐
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
Library management system (Shandong Agricultural University Curriculum Design)
Aike AI frontier promotion (2.15)
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
LeetCode 1. Sum of two numbers
Kubernetes three open interfaces first sight
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
Analysis of how to prevent virus in industrial computer
LeetCode 5. Longest Palindromic Substring
流批一体在京东的探索与实践
mysql min() 求某条件下最小的值出现多个结果
虚假的暑假
大廠面試總結大全
Unity Json 编写
机器学习-感知机模型
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
618 deep resumption: Haier Zhijia's winning methodology
Original God 2.6 server download and installation tutorial
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙