2013-11-11 07:01 PM
HELP!!!!
I've been banging my head against my keyboard and I can't find what's wrong with this code:/*********************************************************
**** INITIALIZE TIMER AND PWM OUTPUTS *****
*********************************************************/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStruccture;
/* GPIOC and GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* Connect TIM1 pins to AF1 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_TIM1);
/* GPIOA Configuration:
* Phase A high: TIM1_CH1(PA8)
* Phase B high: TIM1_CH2(PA9)
* Phase C high: TIM1_CH3(PA10) */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Pin = PIN_PHASE_A_H | PIN_PHASE_B_H | PIN_PHASE_C_H;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIOB Configuration:
* Phase A low: TIM1_CH1N(PB13)
* Phase B low: TIM1_CH2N(PB14)
* Phase C low: TIM1_CH3N(PB15) */
GPIO_InitStructure.GPIO_Pin = PIN_PHASE_A_L | PIN_PHASE_B_L | PIN_PHASE_C_L;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
/* PWM Mode configuration */
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PWM_Duty;
/* PWM1 Mode configuration: Channel1 */
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_BDTRInitStruccture.TIM_LOCKLevel = 0;
TIM_BDTRInitStruccture.TIM_DeadTime = 10;
TIM_BDTRInitStruccture.TIM_AutomaticOutput = 0;
TIM_BDTRInitStruccture.TIM_Break = 0;
TIM_BDTRInitStruccture.TIM_BreakPolarity = 0;
TIM_BDTRInitStruccture.TIM_OSSIState = 0;
TIM_BDTRInitStruccture.TIM_OSSRState = 0;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStruccture);
/* Compute the prescaler value */
PrescalerValue = (uint16_t) ((SystemCoreClock) / TIMER_COUNTER_CLOCK) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = (uint16_t) (TIMER_COUNTER_CLOCK / PWM_FREQ);
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
I get PWM signals on channels 2 and 3, but channel 1 does not work. The positive output is always 0 and the negative output is always 1. I checked the compare value and it's correct. Is there any other place I need to look at???? I am using the STL32F4 discovery board.
Thanks,
#selective-cut-n-paste
2013-11-12 04:18 PM
New news. I have an interrupt enabled for channels 1. That seems to be the problem. Here's the code:
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
What's wrong with this code. Why am I losing the PWM when configuring the interrupt?
2013-11-12 05:04 PM
I don't know, you keep posting incomplete code fragments, dynamic analysis requires them to compile, static analysis need ALL the relevant code and definitions.
If the interrupt causes a problem, perhaps providing the interrupt service routine might be enlightening? You need to enable peripheral clocks before programming the peripheral, otherwise it will not get configured.