本帖最后由 li460135301 于 2014-2-19 15:58 编辑
我在写一个PWM功能程序,测试的时候用的是PC9引脚(TIM3CH4),产生PWM正常!
后来我画板子的时候把PWM引脚用了PB9,也就是切换到TIM17CH1。怎么产生PWM就不正常了!不知道有没有人测试过!我用的是STM32F030R8T6。就是STM32F030 Discovery板子上测试也是这现象!
附上代码:请高手帮忙看一下是哪地方问题?感激不尽!!!
TIM3CH4的代码:
//PWM_32K PIN PB8
#define PWM_32K_CLOCK RCC_AHBPeriph_GPIOC
#define PWM_32K_BASE GPIOC
#define PWM_32K_PIN GPIO_Pin_9
#define PWM_32K_PINSOURSE GPIO_PinSource9
RCC_AHBPeriphClockCmd(PWM_32K_CLOCK, ENABLE);
//PWM_32K_OUTPUT
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_Pin = PWM_32K_PIN;
//gpio.GPIO_OType = GPIO_OType_OD;
//gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_PuPd = GPIO_PuPd_UP ;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PWM_32K_BASE, &gpio);
GPIO_PinAFConfig(PWM_32K_BASE, PWM_32K_PINSOURSE, GPIO_AF_1);
//TIM3_Mode_Config PWM_32K_output
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1463;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 732;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, DISABLE);
}
切换成PB9(TIM17的代码):
//PWM_32K PIN PB8
#define PWM_32K_CLOCK RCC_AHBPeriph_GPIOB
#define PWM_32K_BASE GPIOB
#define PWM_32K_PIN GPIO_Pin_9
#define PWM_32K_PINSOURSE GPIO_PinSource9
//TIM17_Mode_Config PWM_32K_output
static void TIM17_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17 , ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1463;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 732; //F030
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM17, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM17, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM17, ENABLE);
TIM_Cmd(TIM17, DISABLE);
}
//PWM_32K_OUTPUT
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_Pin = PWM_32K_PIN;
//gpio.GPIO_OType = GPIO_OType_OD;
//gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_PuPd = GPIO_PuPd_UP ;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PWM_32K_BASE, &gpio);
GPIO_PinAFConfig(PWM_32K_BASE, PWM_32K_PINSOURSE, GPIO_AF_2);
|