当前位置:网站首页>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
边栏推荐
- Thinking about absolute truth and relative truth
- Everyone Xinfu builds: a one-stop intelligent business credit service platform
- SQL solves the problem of continuous login deformation holiday filtering
- unity Hub 登录框变得很窄 无法登录
- Practice of traffic recording and playback in vivo
- Analysis of how to prevent virus in industrial computer
- LeetCode 2. 两数相加
- Kubernetes three open interfaces first sight
- Remove the underline in router link
- 数学分析_笔记_第5章:一元微分学
猜你喜欢
Yyds dry inventory method of deleting expired documents in batch
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Effectively use keywords to increase Amazon sales
Where can I open computer administrator permissions
关于mysql安装的一些问题
Some problems about MySQL installation
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
⌈ 2022 ⌋ how to use webp gracefully in projects
Sim2real environment configuration tutorial
随机推荐
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
Where can I open computer administrator permissions
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Seal Library - installation and introduction
ROW_NUMBER()、RANK()、DENSE_RANK区别
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
Set the background picture in the idea (ultra detailed)
大廠面試總結大全
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
Exploration and practice of integration of streaming and wholesale in jd.com
PCL least median square method fitting plane
请问怎么在oracle视图中使用stustr函数
七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
Data security industry series Salon (III) | data security industry standard system construction theme Salon
What is Amazon keyword index? The consequences of not indexing are serious
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
PCL 最小中值平方法拟合平面
dried food! Understand the structural vulnerability of graph convolution networks