新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 關于動態(tài)改變STM32 PWM占空比的問題

關于動態(tài)改變STM32 PWM占空比的問題

作者: 時間:2016-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);

}
然后在主循環(huán)里,更改值
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) //無限循環(huán)
{
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更新時間觸發(fā)DMA傳輸(循環(huán)模式),豈不是更簡單?

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



評論


技術專區(qū)

關閉