STMicroelectronics STM8 Manual page 95

Table of Contents

Advertisement

Explanation
The clocks and peripherals are set first. We are using 2MHz peripheral clock and the CPU is running
at 0.5MHz.
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV2);
....
....
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
GPIOs must be set too. Since TIM2 is to output PWM, its CH1 must be set output. Likewise, TIM1's
CH1 GPIO must be set as an input.
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
TIM1 need to be configured for input capture. We need to set time base for TIM1 first. It is set as such
that TIM1 will overflow every second. Then we set input capture channel by specifying the edge
sensitivity, channel, mode and scalars. Since we will be using interrupts, we must enable relevant
interrupts.
void TIM1_setup(void)
{
TIM1_DeInit();
TIM1_TimeBaseInit(2000, TIM1_COUNTERMODE_UP, 55535, 1);
TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, 1, 1);
TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
TIM1_Cmd(ENABLE);
enableInterrupts();
}
TIM2 is set for PWM generation on its CH1. The generated PWM will have a frequency of 50Hz and
50% duty cycle. The setup of TIM2 should be by now self-explanatory:
void TIM2_setup(void)
{
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_32, 1250);
TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_LOW);
TIM2_SetCompare1(625);
TIM2_Cmd(ENABLE);
}
In the vector table of stm8_interrupt_vector.c file, we need to specify the interrupts we will be
using:
{0x82, (interrupt_handler_t)TIM1_UPD_IRQHandler}, /* irq11 */
{0x82, (interrupt_handler_t)TIM1_CH1_CCP_IRQHandler}, /* irq12 */

Advertisement

Table of Contents
loading
Need help?

Need help?

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

Questions and answers

Table of Contents

Save PDF