2014-03-14 09:46 PM
Hi All,
Does anyone know how to get PWM working with the Stm32f407zg starter kit? https://old.iar.com/website1/50.0.1.0/581/1/image2_48c107d4df71ca235b658b9c3935accb.jpg Specifically if anyone has any tested code which drives either the Led's (TIM10_CH1, TIM1_CH1, TIM12_CH1, TIM13_CH1) on PF6-PF9 respectively or the Buzzer (TIM1_CH3 on PA10) I would be hugely obliged. I've tried a whole multitude of code and the most I can get out of the buzzer is a single 'click'. Any help is greatly appreciated.2014-03-15 06:09 AM
Are you sure the buzzer takes a modulated PWM signal, and is not a single frequency, turn it on type of device?
I've posted a shed load of working PWM examples, I don't have your board, so anything I write will be blind, rather than tested.2014-03-15 03:58 PM
Hi Clive,
Thanks for your reply. I'm fairly certain it requires a PWM signal. I have attached the buzzer excerpt from the schematic. If I drive the pin PA10 manually with:- BUZZER_GPIO_PORT->ODR ^= BUZZER_GPIO_PIN; with a 1Kz task I can hear the buzzer 'ticking'. I've pasted what I have below. I suspect I've made a rookie error somewhere.TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
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_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / 42000000;
//50%
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = SystemCoreClock / 2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
Any help is greatly appreciated.
________________
Attachments : buzzer.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzif&d=%2Fa%2F0X0000000bPf%2FNopW9f7OYCY8oxcgxixCf6TSafuWx3dZQxbhww5hFFU&asPdf=false
2014-03-15 04:54 PM
A couple of things going on there, here's a blind mod to generate 1 KHz on PA10
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
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_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1); // TIM1_CH3
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 MHz -> 1 Khz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period + 1) / 2; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
2014-03-15 06:47 PM
Hi Clive,
You hit the nail on the head. It works perfectly. Originally I thought that I was initializing one of the GPIO structures incorrectly. Would I be right in saying that I was incorrectly down-scaling the system clock; and then incorrectly setting the PWM period and duty cycle? I would like that thank you for your help; if it was possible I would buy you a beer. Regards, Matt