 |
|
 |
| Parallax Forums > Public Forums > SX Microcontrollers, SX/B Compiler and SX-Key Tool > Interrupt rate "Trick" | Forum Quick Jump
|
|  Bean Forum Moderator

       Date Joined Jul 2004 Total Posts : 6011 | Posted 2/12/2008 7:47 AM (GMT -8) |   | | I came up with this trick. It's really useful when you need exact interrupt rates.
' Interrupt Rate Trick DEVICE SX28, OSCHS2, TURBO, STACKX, OPTIONX FREQ 78_750_000
' Define pins UnusedRA PIN RA INPUT PULLUP UnusedRB PIN RB INPUT PULLUP UnusedRC PIN RC INPUT PULLUP
FreqPin PIN RB.0 OUTPUT
' Define variables ' None
' *** Interrupt rate "trick" *** ' The oscillator is 78.750 MHz or 22 times the colorburst. ' We want exactly 228 colorburst times per line. ' So 228 * 22 = 5016 <- We want to interrupt every 5016 clocks ' We must use a prescaler of 1:32 ' 156 * 32 = 4992 clocks ' 157 * 32 = 5024 clocks ' ' Hmmm, how can we get the interrupt rate to be exactly 5016. think...think...think (taps head) ' ' Well the prescaler counts from 0 to 31 then overflows back to zero and increments RTCC, ' and writing a value to RTCC will reset the prescaler to zero (this is the "trick") ' ' So if we wait until the prescaler is 24, then reset it back to zero, ' we will add 24 cycles to NEXT interrupt period ' ' If you look at this code, at exactly 23 cycles into the interrupt we do a "CLR RTCC", ' RTCC is already zero, but this has the effect of clearing the prescaler back to zero. '
' NOTE: If the prescaler is set to 8,16,32,64,128 or 256, the instruction that modifies RTCC is 1 count
' If the prescaler is set to 1, the instruction that modifies RTCC is 3 counts
' If the prescaler is set to 2 or 4, the instruction that modifes RTCC is 5 counts
'
' For low prescale values you will have to either set RTCC to a value, or adjust your RETURNINT value.
'
'
'========================================================================================
' Interrupt routine (occurs every 5016 clocks, or 15699.73 times a second)
INTERRUPT NOPRESERVE '(4) <- cycle count ASM JMP $+1 ' (3=7) JMP $+1 ' (3=10) JMP $+1 ' (3=13) JMP $+1 ' (3=16) JMP $+1 ' (3=19) JMP $+1 ' (3=22) NOP ' (1=23) CLR RTCC ' (1=24) This will reset the prescaler, making the next interrupt take 24 cycles longer
' Generate a pulse to count SETB FreqPin MOV FSR,#$FF DJNZ FSR,$ CLRB FreqPin ENDASM RETURNINT 156
PROGRAM Start NOSTARTUP
Start: OPTION = $84 ' Set prescaler to 1:32 DO LOOP END
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Post Edited (Bean (Hitt Consulting)) : 2/13/2008 2:07:13 PM GMT | | Back to Top | | |
 |  pjv Registered Member

       Date Joined Nov 2004 Total Posts : 1304 | Posted 2/12/2008 8:53 AM (GMT -8) |   | Hi Bean;
Something you may not be aware of because it's not really dealt with in the SX documentation, and that is anytime you do a write, or read-modify-write, to the RTCC, it "eats" 3 clock cycles. So if your trick writes to the RTCC - and I suspect CLR RTCC might fall into that category but needs to be confirmed- then you need to account for those 3 cycles.
And in testing this, please be careful, as the debug IDE will not show the proper count....... if I recall correctly, it is out by one.
Cheers,
Peter (pjv) | | Back to Top | | |
  |  Bean Forum Moderator

       Date Joined Jul 2004 Total Posts : 6011 | Posted 2/12/2008 10:55 AM (GMT -8) |   | Peter, Okay I see what you mean. When you modify RTCC the next 3 instructions don't increment RTCC.
BUT it seems that if you have prescaler set to 1:8 or higher (1:16, 1:32:, etc), then the instructions DO increment the prescaler (I guess because there won't be a problem with the RTCC value in the pipeline).
If the prescaler is set to 1:4 or lower (1:2, 1:1), then the next 3 instructions DO NOT increment the prescaler (I guess because there could be a problem with RTCC value changing in the pipeline).
That is why my example worked because I had the prescaler set to 1:32.
Bean.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| | Back to Top | | |
 |  Bean Forum Moderator

       Date Joined Jul 2004 Total Posts : 6011 | Posted 2/13/2008 6:10 AM (GMT -8) |   | Okay by experiments here is what is works out to be:
If the prescaler is 1:8 or higher, the instruction that modifes RTCC is one count.
If the presclaer is 1:1, the instruction that modifies RTCC is three counts.
If the prescaler is 1:2 or 1:4, the instruction that modifes RTCC is five counts.
Seems weird, but that is how it works in my experiments. What I did was get the interrupt rate to be the same when I modifed RTCC and when I didn't. If the resulting rate was the same, the calculation must be correct.
Bean.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| | Back to Top | | |
 |  dkemppai Registered Member
        Date Joined Oct 2004 Total Posts : 315 | Posted 2/13/2008 6:36 PM (GMT -8) |   |
pjv said... Hi Bean;
Something you may not be aware of because it's not really dealt with in the SX documentation, and that is anytime you do a write, or read-modify-write, to the RTCC, it "eats" 3 clock cycles. So if your trick writes to the RTCC - and I suspect CLR RTCC might fall into that category but needs to be confirmed- then you need to account for those 3 cycles.
And in testing this, please be careful, as the debug IDE will not show the proper count....... if I recall correctly, it is out by one.
Cheers,
Peter (pjv) This my version of modifying the RTCC time...
mov RTCC,#47 ;Sets RTCC Cycles = ((255 - x) +3 +3 +3) ;Where x is (mov RTCC,#x) ;for x = #47, RTCC happens every 217 I've experimentally found that the interrupt taks 3 cycles in, 3 cycles out, and there are 3 more lost in the confusion. I measured the time on an o-scope... ...and adjust my math accordingly. It also took a while to figure out what was going on. Of course I wasn't using a prescaler on anything here ;)
-Dan
"A saint-like quantity of patience is a help, if this is unavailable, a salty vocabulary works nearly as well." - A. S. Weaver | | Back to Top | | |
 |  pjv Registered Member

       Date Joined Nov 2004 Total Posts : 1304 | Posted 2/14/2008 8:34 AM (GMT -8) |   | Hi Dan;
When dealing with interrupts, and NOT directly messing with the RTCC, it takes 3 cycles to get in, and 3 cycles to get out. Under those circumstances, never have I experienced any cycles lost "in the confusion". That said, when the RTCC is directly accessed by a write, or a read-modify-write, (but not with a mov w,RTCC) then several (three as I recall) cycles are not counted in the RTCC. No confusion here. As stated previously this is without the use of a pre-scaler.
Bean's discoveries are quite interesting, and I'll have to make some tests on that so that I can fully appreciate his comments.
There is some confusion as to what the IDE indicates as the value of RTCC while in interrupt in DEBUG. I believe the count is out by one.
Cheers,
Peter (pjv) | | Back to Top | | |
 |  Bean Forum Moderator

       Date Joined Jul 2004 Total Posts : 6011 | Posted 2/14/2008 9:58 AM (GMT -8) |   | Peter, Yes the real point of the trick is that you can have the prescaler set to 1:32 (or whatever), and have interrupts generated with 1 cycle resolution instead of 32 cycle resolution.
Here are the programs I used to determine the cycles lost with the prescaler. You connect the FreqPin to a frequency counter. You should get the same frequency on the counter if the "MOV RTCC,W" line is commented out or not. Because either way RTCC will be 3, and the prescaler will be cleared.
Bean.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
File Attachment : Int Trick4.SXB 1KB (application/octet-stream) This file has been downloaded 138 time(s). File Attachment : Int Trick8.SXB 1KB (application/octet-stream) This file has been downloaded 184 time(s). | | Back to Top | | |
| Forum Information | Currently it is Tuesday, February 09, 2010 3:17 AM (GMT -8) There are a total of 415,988 posts in 57,637 threads. In the last 3 days there were 78 new threads and 885 reply posts. View Active Threads
| | Who's Online | This forum has 18517 registered members. Please welcome our newest member, try388. 50 Guest(s), 7 Registered Member(s) are currently online. Details Graham Stabler, humanoido, Bob Lawrence (VE1RLL), computer guy, Rick Brooks, JohnandElspeth, mikestefoy |
Forum powered by dotNetBB v2.42EC SP2.02 dotNetBB © 2000-2010 |
|
|