本帖最后由 xiaoqi976633690 于 2023-9-16 20:18 编辑
1、开发环境
开发板:CH32X035C-R0-1V2
IDE:MounRiver Studio
仿真器:WCHLINKE
例程参考自官方example TIM-> PWM_Output:PWM输出例程
2、硬件配置
PB9----LED1
3、代码实现
PWM 频率和脉宽关系
a.ARR(TIM_Period) 是计数值;
b.PSC(TIM_Prescaler) 是预分频值。
计算公式:Fpwm = 主频 / ((ARR+1)*(PSC+1))(单位:Hz)
/*********************************************************************
* @fn TIM1_OutCompare_Init
*
* @brief Initializes TIM1 output compare.
*
* @param arr - the period value.
* psc - the prescaler value.
* ccp - the pulse value.
*
* @return none
*/
void TIM1_PWMOut_Init(u16 arr, u16 psc, u16 ccp)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
TIM_OCInitTypeDef TIM_OCInitStructure={0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_TIM1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOB, &GPIO_InitStructure );
TIM_TimeBaseInitStructure.TIM_Period = arr;//ARR 自动重装器的值
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
#if (PWM_MODE == PWM_MODE1)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
#elif (PWM_MODE == PWM_MODE2)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
#endif
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ccp;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init( TIM1, &TIM_OCInitStructure );
#if (PWM_SPE_MODE == PWM_SPE_MODE_ENABLE)
TIM_OC12_SupersedeModeCmd( TIM1, TIM_Supersede_Mode_OC1_H, TIM_Supersede_Mode_OC2_H, ENABLE);
#endif
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_Cmd( TIM1, ENABLE );
}
pwm脉宽-计算公式:duty circle = TIMx->CH1CVR / ARR(单位:%)
若是我传入的 pwm_pulse= 20,则duty circle = 20 / 100 = 20%
/*********************************************************************
* @fn TIM_SetCompare1
*
* [url=home.php?mod=space&uid=247401]@brief[/url] Sets the TIMx Capture Compare1 Register value.
*
* @param TIMx - where x can be 1 to 3 select the TIM peripheral.
* Compare1 - specifies the Capture Compare1 register new value.
*
* [url=home.php?mod=space&uid=266161]@return[/url] none
*/
void TIM_SetCompare1(TIM_TypeDef *TIMx, uint16_t Compare1)
{
TIMx->CH1CVR = Compare1;
}
void PWM_Change(u8 pwm_pulse,u_int32_t arr)
{
TIM_SetCompare1(TIM1,pwm_pulse);
TIM_SetAutoreload(TIM1,arr);
}
main函数
int main(void)
{
u8 i=0,stat=0;
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n",SystemCoreClock);
printf("ChipID:%08x\r\n", DBGMCU_GetCHIPID());
TIM1_PWMOut_Init( 99, 99, 99);
while(1)
{
switch(stat)
{
case 0:
i+=2;
if(i>100) stat=1;
PWM_Change(i,99);
break;
case 1:
i-=2;
if(i<1) stat=0;
PWM_Change(i,99);
break;
}
if(i>75) Delay_Ms(50);
if(i>50&&i<75) Delay_Ms(30);
if(i<20) Delay_Ms(10);
if(i>20&&i<50) Delay_Ms(20);
}
}
4、实验现象
源码附件
PWM_Output.zip
(386.66 KB)
https://bbs.21ic.com/forum.php?mod=attachment&aid=MjE2MDA1MHwyMmU0MDczZjE5MjU4MTgxMjg4ZDY2MjcyNmU0OGZlNnwxNzMxMDk4Mzc5&request=yes&_f=.zip
|