In SX Microcontrollers, SX/B Compiler and SX-Key Tool, BPM-Work wrote: This may be due to the quadrature encoder I am using but here it goes... :\ When using the below code from the help file (I removed the display code) when I WATCH the encValue using debug, or even putting encValue out to a port with leds, the count always increments and decrements by 4 decimals. When I turn the encoder VERY slowly, between clicks(my encoder has 32 lock positions) it will show the first two bits momentarily, but they dissappear... So I turn my encoder one click, and it counts 4, and if I turn it again, 8. Then I turn it back, and it counts back to 4, turn it once more back, then counts 0. Is this code somehow counting too many pulses from my quadrature encoder? The encoder I am using is a HRPG-AD32-#13C [url]http://www.avagotech.com/assets/downloadDocument.do?id=610[/url] And code suggestions on how to deal with this? [code] ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000 ID "ENCODER" ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- EncPort VAR RA ' encoder port TRIS_Enc VAR TRIS_A ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- MaxVal CON 100 ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- encCheck VAR Byte encOld VAR Byte ' previous encoder bits encNew VAR Byte ' new encoder bits encValue VAR Byte ' encoder value tmpB3 VAR Byte ' ------------------------------------------------------------------------- INTERRUPT ' ------------------------------------------------------------------------- ' Runs every 64 uS @ 4 MHz (no prescaler) ISR_Start: encNew = EncPort & %00000011 ' get econder bits tmpB3 = encOld XOR encNew ' test for change IF tmpB3 > 0 THEN ' change? encOld = encOld << 1 ' adjust old bits encOld = encOld XOR encNew ' test direction IF encOld.1 = 1 THEN IF encValue < MaxVal THEN ' if max, no change INC encValue ' increase value ENDIF ELSE IF encValue > 0 THEN ' if 0, no change DEC encValue ' decrease value ENDIF ENDIF encOld = encNew ' save last input ENDIF ISR_Exit: RETURNINT ' ========================================================================= PROGRAM Start ' ========================================================================= ' ------------------------------------------------------------------------- ' Subroutine Declarations ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: encNew = EncPort & %00000011 ' read encoder pos encOld = encNew ' copy encValue = 0 ' clear value OPTION = $88 ' interrupt, no prescaler Main: GoTo Main ' ------------------------------------------------------------------------- ' Subroutine Code ' ------------------------------------------------------------------------- [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=164503 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2007 (http://www.dotNetBB.com)