CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

pic18f452 timer0

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
microjack64



Joined: 10 Apr 2020
Posts: 7

View user's profile Send private message

pic18f452 timer0
PostPosted: Tue Apr 14, 2020 11:48 am     Reply with quote

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/
Sad
_________________
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: 286

View user's profile Send private message

PostPosted: Tue Apr 14, 2020 11:58 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Tue Apr 14, 2020 12:33 pm     Reply with quote

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: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 14, 2020 12:56 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Tue Apr 14, 2020 1:16 pm     Reply with quote

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: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 14, 2020 1:50 pm     Reply with quote

and of course you can't SEE an LED being toggled at 42us......
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group