打印

stm32f051产生可调占空比的pwm波

[复制链接]
6893|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wago2008|  楼主 | 2013-5-21 09:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
产生pwm波是比较容易的,调占空比也比较容易,老大提要求要用Tim1产生pwm波的同时还要产生中断,在中断中对pwm波的占空比进行改变,然后说很多单片机都可以实现,是小弟愚昧还是老大说的不对,请各位老师帮忙讨论下。
沙发
s_f_tan| | 2013-5-21 10:56 | 只看该作者
在T1 的update 中断时修改pwm值不就行了

使用特权

评论回复
板凳
cqmiao| | 2013-5-21 11:48 | 只看该作者
貌似是这样

使用特权

评论回复
地板
捡漏王子| | 2013-5-21 12:00 | 只看该作者
最近正在弄PWM

使用特权

评论回复
5
wago2008|  楼主 | 2013-5-22 10:07 | 只看该作者
原来是没有给tim1加中断优先级配置,但是随后问题又出来了,如果要产生互补的pwm波,不改变占空比前是正常互补的,在中断中改变了通道1的占空比后,互补通道没有输出了?


/**********************************
* [url=home.php?mod=space&uid=247401]@brief[/url] TIM_PWM_Config
* @param None
* @retval 配置pwm 参数
**********************************/
void TIM1_PWM_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel=TIM1_CC_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);

//预定标的值TIM1_Period = (time定时器频率 / pwm的频率) - 1
TimerPeriod = (SystemCoreClock / 50000 ) - 1;
//Channel1Pulse的值可以按照下面的公式进行计算:
//ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
//DutyCycle/100为占空比的值

//计算CCR1 跳转值 在占空比为50%时
/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

//Time 定时基础设置
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//Time 定时设置为上升沿计算模式
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Regular);
//频道1的PWM 模式设置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//选择输出比较状态
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//选择互补输出比较状态
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//设置互补输出极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
//装载比较寄存器,判断什么时候发生PWM翻转
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//使能频道1配置
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
//使能TIM1 CCR2寄存器上的 载入
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);

//装载比较寄存器,判断什么时候发生PWM翻转
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;//使能频道2配置
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
//使能TIM1 CCR2寄存器上的 载入
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
//使能ARR寄存器 加载
TIM_ARRPreloadConfig(TIM1,ENABLE);
//死区设置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //运行模式下输出
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //空闲模式下输出选择
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //锁定设置,锁定级别1
TIM_BDTRInitStructure.TIM_DeadTime = 0x0F; //死区时间
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //刹车功能使能
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low; //刹车输入极性,即刹车控制引脚接GND时,PWM停止
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //自动输出使能

TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1,TIM_IT_Update ,ENABLE);
TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE);
//TIM1 主输出使能
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

中断部分


void TIM1_CC_IRQHandler(void)

{
TIM_OCInitTypeDef TIM_OCInitStructure;
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
if(Num_tim1==500000)
{
Num_tim1=0;
if(PWM_num==5)
{
PWM_num=1;
SETPwmPulse(PWM_num);
//TIM_SetCompare1();
}
else
{
SETPwmPulse(PWM_num);
PWM_num++;
}
}
else
{
Num_tim1++;
}
}
else if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
}



使用特权

评论回复
6
wago2008|  楼主 | 2013-5-22 10:08 | 只看该作者
/*******************************************************
  * [url=home.php?mod=space&uid=95362]@func[/url] SETPwmPulse()
  * @brief
  * @param
  * @

********************************************************/
void SETPwmPulse(char num)
{
        switch(num)
                {
                        case 1:
                                //50%
                                Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
                                Channel2Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
                                break;
                        case 2:
                                //37.5%
                                Channel1Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
                                Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
                                break;
                        case 3:
                                //25%
                                Channel1Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
                                Channel2Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
                                break;
                        case 4:
                                //12.5%
                                Channel1Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod - 1)) / 1000);
                                Channel2Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod - 1)) / 1000);
                                break;
                        default:
                                //77.5%
                                Channel1Pulse = (uint16_t) (((uint32_t) 775 * (TimerPeriod - 1)) / 1000);
                                Channel2Pulse = (uint16_t) (((uint32_t) 775 * (TimerPeriod - 1)) / 1000);
        }
       
         TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//使能频道1配置
          TIM_OC1Init(TIM1, &TIM_OCInitStructure);
          //TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;//使能频道1配置
          //TIM_OC2Init(TIM1, &TIM_OCInitStructure);
}

使用特权

评论回复
7
我是老罗| | 2015-11-10 13:40 | 只看该作者
你好,我最近再做pwm输出脉冲,请问怎样设置占空比可调?

使用特权

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

本版积分规则

5

主题

81

帖子

2

粉丝