Getting PWM counter value STM8S003K3

OP #4143259
hello,

MCU: STM8S003K3

In my application I using 16MHz of system clock and l create a PWM (in 
this case PWM_D) of 200Hz generated by 4MHz clock. Therefor my timer's 
counter value is (4M/200=) 20,000.
Also, l have another PWM (in this case PWM_Input) which consider as 
input interrupt.
l would like to get the PWM_D counter each time l get an interrupt 
caused by PWM_Input.

At the interrupt handler l wrote:
1
#pragma vector = 6
2
__interrupt void EXTI_PORTB_IRQHandler(void)
3
{
4
prevTmpTim2Counter = tmpTim2Counter;
5
tmpTim2Counter = TIM2_GetCounter() ;
6
if (GPIO_ReadInputPin(GPIOB, GPIO_PIN_5) == 0)
7
delta = tmpTim2Counter - prevTmpTim2Counter;
8
}
this code gives us the delta Toff of the PWM_Input.
So, if PWM_Input is 200Hz and 50% duty cycle, l suppose to get counter 
value of 10,000. (Because the counter value of 200Hz is 20,000 as we 
calculate before).
However, I got delta of 9900 +/- 250 i.e. sometimes l get values above 
10,000 but mostly under 10,000.

In addition, if we take in consider the identification of the MCU to 
alternate between "1" to "0" in different voltage (depend on the 
hysteresis voltage), yet we should get the same delta, Not perfect 10000 
but same delta each time.

please your advice soon as possible.
Thanks, Lior
#4338261
Lior M. wrote:
> I using 16MHz of system clock

Maybe you are using the internal RC clock, which is a free running 
oscillator? It can be trimmed to a certain accuracy, but it still 
provides an accuracy of +-1%, while uncalibrated it is +-5% (see 
datasheet, pg 63).

Reply

A question of your own? This thread is over 11 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.