STMicroelectronics STM8 Manual page 96

Table of Contents

Advertisement

We have to specify the interrupt subroutine (ISR) prototype functions in the stm8s_it.h file. These
functions are the places where the code will jump when respective interrupt occurs:
@far @interrupt void TIM1_UPD_IRQHandler(void);
@far @interrupt void TIM1_CH1_CCP_IRQHandler(void);
The ISR functions are coded in the stm8s_it.c file:
void TIM1_UPD_IRQHandler(void)
{
overflow_count++;
TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
TIM1_ClearFlag(TIM1_FLAG_UPDATE);
}
The first part is dealing with TIM1 overflow. If a capture occurs when TIM1 count is near to reset value
we need to take this account. This part does so and an overflow counter is incremented.
void TIM1_CH1_CCP_IRQHandler(void)
{
end_time = TIM1_GetCapture1();
pulse_ticks = ((overflow_count << 16) - start_time + end_time);
start_time = end_time;
overflow_count = 0;
TIM1_ClearITPendingBit(TIM1_IT_CC1);
TIM1_ClearFlag(TIM1_FLAG_CC1);
}
The second part is where TIM1 capture is recorded. Once a rising edge is captured, an interrupt is
issued. In the interrupt, we must first save the current TIM1 counter count in the variable named
end_time. The formula for pulse tick is then computed. Note how the TIM1 overflow is addressed in
the formula. The new start time should be the previous capture time because we need to deduct old
capture count from new capture count. Lastly, overflow counter, capture flag and pending interrupts
are cleared.
In the main loop, we are just displaying the time period of capture in a LCD while everything is being
processed in the background by interrupts:
while(TRUE)
{
time_period = pulse_ticks;
lcd_print(0, 1, time_period);
delay_ms(400);
};

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

Subscribe to Our Youtube Channel

Table of Contents

Save PDF