In SX Microcontrollers, SX/B Compiler and SX-Key Tool, bean wrote: I came up with this trick. It's really useful when you need exact interrupt rates. [code] ' 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. ' ' 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 [/code] Bean. ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=249607 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2008 (http://www.dotNetBB.com)