View previous topic :: View next topic |
Author |
Message |
luisjoserod
Joined: 10 Jan 2019 Posts: 16 Location: Maracaibo, Venezuela
|
TIMER0 PROBLEM |
Posted: Wed Feb 13, 2019 6:25 pm |
|
|
Hello guys, i'm using this code:
Code: |
#include <16F876A.h>
#FUSES NOWDT
#FUSES XT
#FUSES PUT
#FUSES NOPROTECT
#FUSES NODEBUG
#FUSES BROWNOUT
#FUSES NOLVP
#FUSES NOCPD
#FUSES NOWRT
#FUSES RESERVED
#use delay(clock=4000000)
#use standard_io (B)
#define LED PIN_B6
#int_TIMER0
void TIMER0_isr(void)
{
output_toggle(LED);
set_TIMER0(56);
}
void main() {
set_tris_b(0b01111111);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_TIMER0(56);
while(TRUE)
{
}
}
|
It should overflow at exactly 400us, but i'm checking the signal on RB7 with an oscilloscope and it's about 447us WHY!? but if i change the values and try bigger overflow times like 32ms everything goes perfect, it's related to my relatively low 4Mhz Crystal?
P.D: I'm not trying to generate a square signal, i'm just checking if the timer is working ok.
Thanks in advance. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1357
|
|
Posted: Wed Feb 13, 2019 7:44 pm |
|
|
Interrupts don't happen as instantaneously as you expect. There is a bit of latency before the ISR is triggered. You are most likely seeing this. Plus your output_toggle() will add a small bit of drift as well. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Thu Feb 14, 2019 1:31 am |
|
|
It takes typically about 30uSec to get into an interrupt.
Your reset of the timer is then happening this long after the timer
interrupt triggers, plus the time to toggle the pin (five more instructions).
Then the actual timer update happens on the next tick of the timer.
Your time is a fraction slower than I'd have expected. It should be about 439uSec. |
|
|
luisjoserod
Joined: 10 Jan 2019 Posts: 16 Location: Maracaibo, Venezuela
|
|
|
|