View previous topic :: View next topic |
Author |
Message |
microjack64
Joined: 10 Apr 2020 Posts: 7
|
pic18f452 timer0 |
Posted: Tue Apr 14, 2020 11:48 am |
|
|
I am using Timer0 on a Microchip PIC18F452 so that it will trigger an interrupt and toggle an LED_pin. It takes at least 42 µs for the LED to toggle.
At an increment rate of Fosc/4 (= 8 MHz/4 = 2 MHz), a TMR0 preload of 0xFF and a two cycle loss when making to TMR0 I expect to get an interrupt every 1.5 µs, at least on paper. Even if I factor into some instruction cycles for my LED operations, I can't figure out why it takes 42 µs. What is wrong here?
https://projectiot123.com/2020/04/10/introduction-to-pic18f452-microcontroller/
_________________ mini engineer at https://projectiot123.com
Last edited by microjack64 on Fri Apr 17, 2020 8:04 am; edited 1 time in total |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 306
|
|
Posted: Tue Apr 14, 2020 11:58 am |
|
|
There is code that needs to run to save the status registers and variables before your interrupt code runs and then those need to be restored on exit. 42us seems about right. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Tue Apr 14, 2020 12:33 pm |
|
|
You don't tell us your CPU clock speed.
It typically takes about 30 instruction times to actually get into the
interrupt handler after the event triggers. Then if your interrupt does
anything else this will use more time. The toggle itself if ypu are using
standard IO, will set the tris before doing the output so another three
instruction times. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9272 Location: Greensville,Ontario
|
|
Posted: Tue Apr 14, 2020 12:56 pm |
|
|
Dump the listing, play computer and see how the program actually runs.
If you set the clock to 4MHz, then almost every instruction will take 1us (except for gotos and jumps). Have a copy of the instruction set handy to confirm times though.
Once you have this timed out at 4Mhz, it's easy to recalculate for other speeds. At 20MHz, it'll be 1/5 the time..... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Tue Apr 14, 2020 1:16 pm |
|
|
As a comment, you can't handle an interrupt every 1.5uSec, even on a PIC
at 64MHz. By the time the overhead of getting into and out of the handler is
met, you have no time to do anything else. If you want a pulse at this sort of frequency, use a PWM. If you want to actually 'do' something, then you would
at the very least need to poll the interrupt flag, rather than having an interrupt
handler, and use a much faster PIC than it sounds as if you have.
Think in terms of having to allow 100 instructions minimum to 'handle' an
interrupt.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9272 Location: Greensville,Ontario
|
|
Posted: Tue Apr 14, 2020 1:50 pm |
|
|
and of course you can't SEE an LED being toggled at 42us...... |
|
|
|