Question
STM32L Discovery PWM
Posted on December 16, 2013 at 11:28
Hello,
I tried to post this to the following thread, since this was my initial guidance to configure PWM, but was unsuccessful due to some permission error: https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32VLDiscovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32VLDiscovery%2FExample%20PWM%20on%20STM32-Discovery%20Blue%20LED&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000491D59B8574F8049B5DFA3E8B21CBA51¤tviews=11598 Anyway, I am trying to enable PWM on the two LED's on the STM32L discovery board. I can confirm that the LED's work with the following code:int main(void)
{
/*!<
At
this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32l1xx_md.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32l1xx.c file
*/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
// Enable HSI Clock
RCC_HSICmd(ENABLE);
// Wait till HSI is ready
while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET) {}
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
// SysTick 1 msec interrupts (
1000
=
1ms
,
100
= 10 ms ...)
if(SysTick_Config(SystemCoreClock / 1000))
{
// capture error
while(1);
}
// Enable clock for GPIOB
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
// Enable clock for TIM3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin
=
GPIO_Pin_6
| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode
=
GPIO_Mode_OUT
; // GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType
=
GPIO_OType_PP
;
GPIO_InitStructure.GPIO_Speed
=
GPIO_Speed_40MHz
;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//GPIO_PinAFConfig(GPIOB,GPIO_Pin_6 | GPIO_Pin_7,GPIO_AF_TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_ClockDivision
=
TIM_CKD_DIV4
;
TIM_TimeBaseInitStruct.TIM_Period
=
1000
- 1;
TIM_TimeBaseInitStruct.TIM_Prescaler
=
240
- 1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_OCStructInit(&TIM_OCInitStruct);
TIM_OCInitStruct.TIM_OutputState
=
TIM_OutputState_Enable
;
TIM_OCInitStruct.TIM_OCMode
=
TIM_OCMode_PWM1
;
TIM_OCInitStruct.TIM_Pulse
=
0
;
TIM_OC3Init(TIM3,&TIM_OCInitStruct);
TIM_OC4Init(TIM3,&TIM_OCInitStruct);
TIM_Cmd(TIM3,ENABLE);
while(1) {
// TIM3->CCR3 = 800;
// TIM3->CCR4 = 0;
GPIOB->BSRRL = GPIO_Pin_6;
GPIOB->BSRRH = GPIO_Pin_7;
Delay(1000);
// TIM3->CCR3 = 0;
// TIM3->CCR4 = 800;
GPIOB->BSRRH = GPIO_Pin_6;
GPIOB->BSRRL = GPIO_Pin_7;
Delay(1000);
}
}
If I change the code to (AF pin mode and change the mapping of the pins):
int main(void)
{
/*!<
At
this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32l1xx_md.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32l1xx.c file
*/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
// Enable HSI Clock
RCC_HSICmd(ENABLE);
// Wait till HSI is ready
while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET) {}
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
// SysTick 1 msec interrupts (
1000
=
1ms
,
100
= 10 ms ...)
if(SysTick_Config(SystemCoreClock / 1000))
{
// capture error
while(1);
}
// Enable clock for GPIOB
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
// Enable clock for TIM3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin
=
GPIO_Pin_6
| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode
=
GPIO_Mode_AF
; // GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType
=
GPIO_OType_PP
;
GPIO_InitStructure.GPIO_Speed
=
GPIO_Speed_40MHz
;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_Pin_6 | GPIO_Pin_7,GPIO_AF_TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_ClockDivision
=
TIM_CKD_DIV4
;
TIM_TimeBaseInitStruct.TIM_Period
=
1000
- 1;
TIM_TimeBaseInitStruct.TIM_Prescaler
=
240
- 1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_OCStructInit(&TIM_OCInitStruct);
TIM_OCInitStruct.TIM_OutputState
=
TIM_OutputState_Enable
;
TIM_OCInitStruct.TIM_OCMode
=
TIM_OCMode_PWM1
;
TIM_OCInitStruct.TIM_Pulse
=
0
;
TIM_OC3Init(TIM3,&TIM_OCInitStruct);
TIM_OC4Init(TIM3,&TIM_OCInitStruct);
TIM_Cmd(TIM3,ENABLE);
while(1) {
TIM3->CCR3 = 800;
TIM3->CCR4 = 0;
// GPIOB->BSRRL = GPIO_Pin_6;
// GPIOB->BSRRH = GPIO_Pin_7;
Delay(1000);
TIM3->CCR3 = 0;
TIM3->CCR4 = 800;
// GPIOB->BSRRH = GPIO_Pin_6;
// GPIOB->BSRRL = GPIO_Pin_7;
Delay(1000);
}
}
Nothing happens. What am I doing wrong in the initialization?
Thank you and best regards,
K