( Legal Looping - Posted by g_jilek 02/20/04 ( ( Here is a technique that i've come up with to help keep the "Loophog" at bay. ( The Word that i'm Looping in is SONAR-Dr><. I'm also including some of the ( associated code to heip clarify how i used the technique. ( ( Keeping in mind the IsoMax system timer's max count is FFFF x 8, you can set ( the count in the IF statement to drive a Display, or any Word, when you need it ( to be run. The example Outputs Data at a rate of about Twice per Second. ( ( I've also included a chunk of code to turn PORTA into an LED Bar Graph Display. ( I used a 10 LED DIP Array and biased each of the center 8 LEDs with a 600 Ohm ( resistors. The LED Arrays have been available at Radio Shack, if not then Mouser or ( ( Digi-Key will have them. ( ( Hope this is of some value. ( ( MAY THE FORTH BE WITH YOU ! ( ( Gerard ( ( ( ------------------------------------------------------------------------- HEX : A/D0 ( -- ) ( F: --r1) \ Calculates the Running Average of the Analog Input RUN# @ S>F RUN-AVG_0 2@ D>F FOVER FOVER FSWAP F/ F- \ AN0 & Leaves the Value on F>D ANA0 S->D D+ 2DUP RUN-AVG_0 2! D>F FSWAP F/ \ the Floating Point Stack ; EEWORD : SMOOTHED_ADA0 ( --n1) ( F: -- ) RUN-AVG_0 2@ D>F RUN# @ S>F F/ rONE F* \ Scales the Floating Point Value FROUND F>D DROP \ Converts Floating Point Value to an Integer ; EEWORD : DISPLAY_PORTA ( -- ) \ This Drives an LED Bar Graph Display 00 PORTA PUTBYTE SMOOTHED_ADA0 ( This is current Analog Data DUP 34 < IF 00 PORTA PUTBYTE ELSE ( This is Min to Turn Off Bit 0 1000 8/ OVER OVER < IF 01 PORTA PUTBYTE ELSE OVER OVER 2* < IF 03 PORTA PUTBYTE ELSE OVER OVER 3 * < IF 07 PORTA PUTBYTE ELSE OVER OVER 4 * < IF 0F PORTA PUTBYTE ELSE OVER OVER 5 * < IF 1F PORTA PUTBYTE ELSE OVER OVER 6 * < IF 3F PORTA PUTBYTE ELSE OVER OVER 7 * < IF 7F PORTA PUTBYTE ELSE OVER OVER 8 * < IF FF PORTA PUTBYTE ELSE ." Analog Converter Overflow" THEN THEN THEN THEN THEN THEN THEN THEN THEN 2DROP ; EEWORD : STARTCNV ( -- ) \ Provides Start Pulse to Initiate an ADC Conversion 2000 0E80 ! ; EEWORD : ADAxy1_RDY ( -- f ) \ Tests ADC Status Register (ADSTAT), (RDY0) & (RDY1) E86 @ 0001 AND ; EEWORD : DISPLAY_SONAR DECIMAL F. HEX CR ; EEWORD : SONAR-Dr>< ( -- ) ( F: -- ) \ Runs the Algorithm & Controls the Output to A/D0 DISPLAY_PORTA \ the Bar Graph & Terminal ?TERMINAL IF ABORT" Press Reset Button to Continue" ELSE LOOPCOUNT @ DUP 0= IF DROP A0 LOOPCOUNT ! DISPLAY_SONAR ELSE 1 - LOOPCOUNT ! FDROP THEN THEN STARTCNV ; EEWORD \ State Machine Code MACHINE CHECK_SONAR-Dr EEWORD ON-MACHINE CHECK_SONAR-Dr APPEND-STATE SONAR-Dr EEWORD IN-STATE SONAR-Dr CONDITION ADAxy1_RDY CAUSES SONAR-Dr>< THEN-STATE SONAR-Dr TO-HAPPEN IN-EE : SONAR_Tx/Rx_TEST PWM_INIT ADC_INIT SONAR-Dr SET-STATE EVERY 4000 CYCLES SCHEDULE-RUNS CHECK_SONAR-Dr ; EEWORD HEX 3C00 AUTOSTART SONAR_Tx/Rx_TEST SAVE-RAM