EasyManuals Logo

STMicroelectronics STM8 User Manual

STMicroelectronics STM8
126 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #84 background imageLoading...
Page #84 background image
}
void GPIO_setup(void)
{
GPIO_DeInit(GPIOA);
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, ((GPIO_Pin_TypeDef)GPIO_PIN_3 | GPIO_PIN_4), GPIO_MODE_OUT_PP_HIGH_FAST);
}
void TIM2_setup(void)
{
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_32, 1000);
TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_HIGH);
TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_LOW);
TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_HIGH);
TIM2_Cmd(ENABLE);
}
Explanation
Again, the CPU and the peripheral clock is set at 2MHz.
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
….
….
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
Next, we need to configure the PWM GPIOs as outputs.
void GPIO_setup(void)
{
GPIO_DeInit(GPIOA);
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, ((GPIO_Pin_TypeDef)GPIO_PIN_3 | GPIO_PIN_4), GPIO_MODE_OUT_PP_HIGH_FAST);
}
Just like other microcontrollers, PWM generation involves a timer. Here as said TIM2 is that timer. We
need to set time base first before actually configuring the PWM channels.
void TIM2_setup(void)
{
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_32, 1000);
TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_HIGH);
TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_LOW);
TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_HIGH);
TIM2_Cmd(ENABLE);
}
In the codes above, TIM2 has a time base of 16ms or 62.5kHz. This time base is further divided by the
Output Compare (OC) unit. Thus, here the 62.5kHz base is further divided by 1000 to get 62.5Hz PWM
frequency. The maximum duty cycle is therefore 1000. Additionally, we can set PWM polarity and
command the channel if or if not should it behave in an inverted manner.

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the STMicroelectronics STM8 and is the answer not in the manual?

STMicroelectronics STM8 Specifications

General IconGeneral
BrandSTMicroelectronics
ModelSTM8
CategoryMicrocontrollers
LanguageEnglish

Related product manuals