打印
[应用相关]

PWM 输出与占空比的修改

[复制链接]
574|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
bbapple|  楼主 | 2016-8-27 10:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
TIM_TimeBaseInitStruct.TIM_Prescaler = 179;  // 10/2MHZ=5us
TIM_TimeBaseInitStruct.TIM_Prescaler = 179;  // 10/2MHZ=5us
TIM_TimeBaseInitStruct.TIM_Period = 1999;//10ms  
TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);     
/********以下 4路PWM设置共有**********/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//高优先级
/****PWM1*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM1;//占空比分子1,即计数器计数到TIM3_CCR1_PWM1 发生跳变
TIM_OC1Init(TIM3, &TIM_OCInitStructure);  //初始化
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//预重载使能
/****PWM2*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM2;  //占空比分子2,即计数器计数到TIM3_CCR1_PWM2 发生跳变
TIM_OC2Init(TIM3, &TIM_OCInitStructure);  
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/****PWM3*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM3;//占空比分子3,即计数器计数到TIM3_CCR1_PWM3 发生跳变
TIM_OC3Init(TIM3, &TIM_OCInitStructure);  
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/****PWM4*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM4;//占空比分子4,即计数器计数到TIM3_CCR1_PWM4 发生跳变
TIM_OC4Init(TIM3, &TIM_OCInitStructure);  
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);//此句最好写上 防止意外发生,默认自动装载
TIM_Cmd(TIM3, ENABLE);
若需要调节占空比 则调用以下函数
TIM_SetCompare1(TIM3, TIM3_CCR1_PWM1);//PWM1,调节占空比
TIM_SetCompare2(TIM3, TIM3_CCR1_PWM2);//PWM1,调节占空比
TIM_SetCompare3(TIM3, TIM3_CCR1_PWM3);//PWM1,调节占空比
TIM_SetCompare4(TIM3, TIM3_CCR1_PWM4);//PWM1,调节占空比
沙发
huangcunxiake| | 2016-8-27 11:18 | 只看该作者
PWM是一种对模拟信号电平进行数字编码的方法。通过高分辨率计数器的使用,方波的占空比被调制用来对一个具体模拟信号的电平进行编码

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

1811

帖子

1

粉丝