2012-10-27 11:36 AM
Dear STM-Experts,
i tried my first PWM with the STM32F4Discovery. It should create a 7 MHz PWM with 50 % duty-cycle. But i can only get a changing 8 bzw 6 Mhz Pwm like shown here: Here is the code (Picked up and then changed from STM Std. Peripheral Libraries): //GPIOE Clocks enable RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE , ENABLE); //GPIOE settings GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP ; //GPIOE Configuration: Channel 2 as alternate function push-pull GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_11 ; GPIO_Init(GPIOE, &GPIO_InitStructure2); GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1); //TIM1 clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); //Time Base configuration TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 23; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //Channel 2 Configuration in PWM mode TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = 11; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC2Init(TIM1, &TIM_OCInitStructure); //TIM1 counter enable TIM_Cmd(TIM1, ENABLE); //TIM1 Main Output Enable TIM_CtrlPWMOutputs(TIM1, ENABLE); Thank you a lot for your help! Florian2012-10-27 01:50 PM
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 24 - 1; // 168 / 24 = 7 MHz, confirm system clock
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//Channel 2 Configuration in PWM mode
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 24 / 2; // 50/50 duty ie 12 cycles
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
//TIM1 Main Output Enable
TIM_CtrlPWMOutputs(TIM1, ENABLE);
//TIM1 counter enable
TIM_Cmd(TIM1, ENABLE);
2012-10-27 02:40 PM
/* Compute the value to be set in ARR regiter to generate signal frequency at 7 Mhz*/
TimerPeriod = (SystemCoreClock / 7000000 ) - 1;
/* Compute CCR2 value to generate a duty cycle at 50% for channel 2 and 2N */
Channel2Pulse = (uint16_t) (((uint32_t)5 * (TimerPeriod - 1)) / 10);
And you are right, 168 Mhz clock. But thats not the Thing, I don't know why the PWM is looking weird like it does :(2012-10-27 06:14 PM
The pulse width computation is definitely wrong.