ARM 的PWM输出注意点
- APM32 M0的PWM初始化
- STM32 的PWM初始化
ARM 的PWM输出注意点:想改变PWM的输出通道就得改变相应的端口定义、定时器及定时器通道配置,请参考下面的例程:
APM32 M0的PWM初始化
/*!
* @brief TMR1 PWM Output Init
*
* @param None
*
* @retval None
*
* @note
*/
void APM_MINI_TMR1_PWMOutPut_Init(void)
{
TMR_TimeBase_T timeBaseConfig;
TMR_OCConfig_T occonfig;
GPIO_Config_T gpioconfig;
/** Enable Clock*/
RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOA);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR1);
/** Connect TMR1 to CH4 */
GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_11, GPIO_AF_PIN2);
gpioconfig.mode = GPIO_MODE_AF;
gpioconfig.outtype = GPIO_OUT_TYPE_PP;
gpioconfig.pin = GPIO_PIN_11;
gpioconfig.pupd = GPIO_PUPD_PU;
gpioconfig.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA,&gpioconfig);
/** Set clockDivision = 1 */
timeBaseConfig.clockDivision = TMR_CKD_DIV1;
/** Up-counter */
timeBaseConfig.counterMode = TMR_COUNTER_MODE_UP;
/** Set divider = 47 .So TMR1 clock freq ~= 48/(47+1) = 1MHZ */
timeBaseConfig.div =7;// 47 ;
/** Set counter = 0xffff */
timeBaseConfig.period = 40;
/** Repetition counter = 0x0 */
timeBaseConfig.repetitionCounter = 0;
TMR_ConfigTimeBase(TMR1,&timeBaseConfig);
/** PWM1 mode */
occonfig.OC_Mode = TMR_OC_MODE_PWM1;
/** Idle State is reset */
occonfig.OC_Idlestate = TMR_OCIDLESTATE_RESET;
/** NIdle State is reset */
occonfig.OC_NIdlestate = TMR_OCNIDLESTATE_RESET;
/** Enable CH1N ouput */
occonfig.OC_OutputNState = TMR_OUTPUT_NSTATE_DISABLE;
/** Enable CH1 ouput */
occonfig.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
/** CH1 polarity is high */
occonfig.OC_Polarity = TMR_OC_POLARITY_HIGH;
/** CH1N polarity is high */
occonfig.OC_NPolarity = TMR_OC_NPOLARITY_HIGH;
/** Set compare value */
occonfig.Pulse = 40;
TMR_OC4Config(TMR1,&occonfig);
/** Enable PWM output */
TMR_EnablePWMOutputs(TMR1);
/** Enable TMR1 */
TMR_Enable(TMR1);
//set the default value;
PWM_DUTY_SET=PWR_ON_DEFAULT_DUTY;
}
改变PWM在占空比:
TMR_SetCompare4(TMR1,PWR_ON_DEFAULT_DUTY_RUNTIME); //开机FAN为60%运行10秒
如果需要改变PWM的通道及定时器请注意:
STM32 的PWM初始化
/****************************************************************************
* 名 称: void TIM1_PWM_Init(u32 auto_data,u32 fractional)
* 功 能:PWM输出初始化
* 入口参数:auto_data: 自动重装值
* fractional: 时钟预分频数
* 返回参数:无
* 说 明:设置PA9(TIM1_CH2)输出PWM波
****************************************************************************/
void TIM1_PWM_Init(u32 auto_data,u32 fractional)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM5时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1); //GPIOA9复用为定时器1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOA9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9
TIM_TimeBaseStructure.TIM_Prescaler=fractional; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=auto_data; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化定时器5
//初始化TIM1 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_Pulse = auto_data/2; //占空比 50%
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR2上的预装载寄存器
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM1
}
改变PWM在占空比:文章来源:https://uudwc.com/A/0kB8k
TIM_SetCompare2(TIM1,pwmzkb);
如果需要改变PWM的通道及定时器请注意:
文章来源地址https://uudwc.com/A/0kB8k