为什么stm32f429不能输出pwm波形
时间:10-02
整理:3721RD
点击:
新手,为什么stm32f429就是不能输出pwm波形 ,检查了好多遍就是找不到问题,怎么办 ? 是我配置出问题了吗?代码如下,有大神能指教一下吗,
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd ( RCC_APB1Periph_TIM2, ENABLE);
/* */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM2);
TIM_TimeBaseStructure.TIM_Period = (1000000-1); //μ±?¨ê±?÷′ó0??êyμ? TIM_Period+1 £??aò????¨ê±?ü?ú
TIM_TimeBaseStructure.TIM_Prescaler = 17; //éè???¤·??μ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1 ; //éè??ê±?ó·??μ?μêy£o2?·??μ(?aà?ó?2?μ?)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //?òé???êy?£ê?
// 3?ê??ˉ?¨ê±?÷TIMx, x[2,3,4,5]
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/*PWM?£ê?????*/
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????aPWM?£ê?1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê1?üê?3?
TIM_OCInitStructure.TIM_Pulse = 0; //éè??3?ê?PWM??3??í?è?a0
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //μ±?¨ê±?÷??êy?μD?óúCCR1_Valê±?aμíμ??? LEDμ?áá
//ê1?üí¨μà1
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
//ê1?üí¨μà2
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
//ê1?üí¨μà3
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
//ê1?üí¨μà4
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
// ?a????êy?÷?D??
//TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
// ê1?ü??êy?÷
TIM_Cmd(TIM2, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd ( RCC_APB1Periph_TIM2, ENABLE);
/* */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM2);
TIM_TimeBaseStructure.TIM_Period = (1000000-1); //μ±?¨ê±?÷′ó0??êyμ? TIM_Period+1 £??aò????¨ê±?ü?ú
TIM_TimeBaseStructure.TIM_Prescaler = 17; //éè???¤·??μ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1 ; //éè??ê±?ó·??μ?μêy£o2?·??μ(?aà?ó?2?μ?)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //?òé???êy?£ê?
// 3?ê??ˉ?¨ê±?÷TIMx, x[2,3,4,5]
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/*PWM?£ê?????*/
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????aPWM?£ê?1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê1?üê?3?
TIM_OCInitStructure.TIM_Pulse = 0; //éè??3?ê?PWM??3??í?è?a0
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //μ±?¨ê±?÷??êy?μD?óúCCR1_Valê±?aμíμ??? LEDμ?áá
//ê1?üí¨μà1
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
//ê1?üí¨μà2
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
//ê1?üí¨μà3
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
//ê1?üí¨μà4
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
/*ê1?üí¨μà????*/
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
// ?a????êy?÷?D??
//TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
// ê1?ü??êy?÷
TIM_Cmd(TIM2, ENABLE);
各位大神帮忙看一下,真的很感谢,检查了好久都找不出问题
中断函数有吗~最好把文件都发出来看看~
没有用到中断,这段代码只是通用定时器输出四路pwm波的初始化,但是我用示波器根本检测不到波形,检查了好久都没查出问题,你帮我看看这段代码的初始化配置哪里不对,万分感谢
。
你的回调函数呢?
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL
这个nopull改成pullup试下看