微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 关于动态改变STM32 PWM占空比的问题

关于动态改变STM32 PWM占空比的问题

时间:11-25 来源:互联网 点击:

while (1)
{
Delay(500);
if(CCR4_Val>1)
{
CCR4_Val--;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC4Init(TIM3, &TIM_OCInitStructure);
}
else
{
CCR4_Val=990;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC4Init(TIM3, &TIM_OCInitStructure);
}
}
}
在主程序里改变对比值来改变PWM占空比,上面的程序是不是太笨了,有没有其他方法,请高手指点

MCU2008 (2008-9-12 10:43:53)

是太笨了...

sunke9 (2008-9-12 10:47:56)

我的用法和你一样
while (1)
{
Delay(0x0000FFFF);
CCR1_Val = ADC_ConvertedValue*4;
CCR2_Val = 16000 - ADC_ConvertedValue*4;

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC1Init(TIM2, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);

}

nomoneyiv (2008-9-12 11:29:08)

在中断里搞!

nomoneyiv (2008-9-12 11:33:22)

比如:
void TIM1_UP_IRQHandler(void)
{
TIM1_SetCompare1(pwm_val_c1);
TIM1_SetCompare3(pwm_val_c3);
TIM1_SetCompare2(pwm_val_c2);
TIM1_ClearITPendingBit(TIM1_IT_Update);

}
然后在主循环里,更改值

pwm_val_c1++; 之类的。

020411 (2008-9-12 15:21:42)

谢谢STM32青菜

yemingxp (2008-9-12 20:02:26)

aleyn (2008-9-15 14:05:15)

此贴最终解惑者是谁?

sw笨笨 (2009-2-24 07:31:01)

while (1) //无限循环
{
CCR4_Val=CCR4_Val+0x1000; //变量递增
if(CCR4_Val >= 0xffff) CCR4_Val=0; //变量归零

TIM_SetCompare4(TIM3, CCR4_Val); //变更长度
Delay(0xffff); //延时

}

selina (2009-12-12 15:28:56)

好像只能在中断内做吧

wslaurora (2010-1-26 15:48:33)

通过tim更新时间触发DMA传输(循环模式),岂不是更简单?

TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_CCR1_Address; //TIM1通道1外设捕获/比较寄存器首地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SRC_Buffer; //内存数据缓存首地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //外设作为数据传输的目的
DMA_InitStructure.DMA_BufferSize = 256; //DMA缓存大小为256


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top