In SX Microcontrollers, SX/B Compiler and SX-Key Tool, robotnerd007 wrote: I am working on a project that uses a US digital 400ppr to measure distance and then at two preset values it puts a pin to high in order signaling a basic stamp which takes care of everything else. I am not very experienced programming at all but feel comfortable enough with the basic stamp and have the code i need for that is working. However my code for counting the encoder pulses and signaling the basic stamp does not work. For counting encoder pulses i took the code from an encoder thread listed under "the best SX threads" I kind of understand how it works but could not write it myself. I have channel A of the encoder connected to RA0 and channel B is connected to RA1. I was wondering if someone could take a quick look at this and point out my stupid mistake. A big thank you to anyone who takes the time to help a beginner. P.S. Is there any reason not to run the sx at 50mhz? Would i have to change anything except the initializing code at the beginning? (i do know how to do that) ' BLINK1.SXB DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000 EncPort VAR RA ' encoder port TRIS_Enc VAR TRIS_A MaxVal CON 255 EndVal Con 400 SlowVal Con 300 'encCheck VAR Byte EncodersOld VAR byte EncodersNew VAR byte Encoder_A_Count VAR word tmpB1 VAR byte tmpB2 VAR Byte tmpB3 VAR Byte ' ========================================================================= INTERRUPT ' ========================================================================= ' Runs every 64 uS @ 4 MHz (no prescaler) ISR_Start: EncodersNew = EncPort TmpB1 = EncodersOld XOR EncodersNew ' compare old with new ' to work out which channels have changed TmpB2 = TmpB1 << 1 ' make single bit that shows change on A or B phase TmpB1 = TmpB1 OR TmpB2 EncodersOld = EncodersOld << 1 ' make direction bits by comparing old A with new B EncodersOld = EncodersOld XOR EncodersNew ' xor old A with new B IF TmpB1.3 = 1 THEN IF EncodersOld.3 = 1 THEN INC Encoder_A_Count ELSE DEC Encoder_A_Count ENDIF ENDIF EncodersOld=EncodersNew 'ISR_Exit: RETURNINT ' ========================================================================= PROGRAM Start ' ========================================================================= Start: EncodersNew = EncPort EncodersOld = EncodersNew Encoder_A_Count = 0 OPTION = $88 ' interrupt, no prescaler bgn: Do If Encoder_A_Count >= EndVal then goto stop 'tells BS2 when to slow down and when to stop a robot IF Encoder_A_Count >= SlowVal then goto slowdown loop stop: high RC.7 slowdown: high RC.6 goto bgn ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=259381 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)