2015-09-06 08:28 AM
Hello all,
I'm stuck for a long time without results. (I'm using STM8S105C6 discovery with IAR IDE) I'm trying to measure a pulse time in one of 2 ways: 1. Manually, by starting the timer when the pulse rises, and staying in a while loop until it falls and then manually read the TIMx_CNTR. 2. With External Interrupts, by starting the timer when the pulse rises, and then enabling the interrupt that stops the timer when the pulse falls. In both ways(the first one was much more accurate) the timer gives me the same result whatever the Prescaler is. Interesting, is that the Prescaler does work when I use the Overflow Interrupt. Here is my code for the 2nd way:#include <
iostm8s105c6.h
>
#pragma vector = TIM2_CAPCOM_CC1IF_vector
__interrupt void TIM2_CH1_IC_interrupt(void)
{
int distance;
distance = TIM2_CNTRH<<8;
distance += TIM2_CNTRL;
TIM2_CR1_CEN = 0; // Stops the timer
TIM2_CCER1_CC1E = 0; // Disable input capture
PD_ODR_ODR3 = 1;
Delayus(distance); // Output a wave that is linear to the Timer Counter
PD_ODR_ODR3 = 0;
TIM2_SR1_CC1IF = 0; // Clears the Interrupt flag
}
void main()
{
PD_DDR_DDR4 = 0; // input.
PD_DDR_DDR3 = 1; // output.
TIM2_PSCR = 0x09; // Prescaler = 512
TIM2_CCMR1 = 0x01; // Input Capture
TIM2_CCER1_CC1P = 1; // Capture dalling edge
TIM2_IER_CC1IE = 1; // Enable Channel 1 Interrupt
while (1)
{
while (!PD_IDR_IDR4); // Waits for the pulse to rise
TIM2_CR1_CEN = 1; // Starts the timer
TIM2_CCER1_CC1E = 1; // Enable input capture
while (PD_IDR_IDR4); // Until the pulse falls
}
}
Everything works great, the interrupt, and the output, for each pulse I recueve, the output is linear in the same ratio. Only that whatever I put in the Prescaler, the ratio is the same. plus checking ''distance'' in debugging mode - is the same result.
Thank, Dror.