In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Matthias09 wrote: and here the complete code for reading an US Digital 360PPR quad encoder. May it help people the same way I was helped. at startup, only the index channel interrupt is enabled, the system has to find it's reference point. once the index channel interrupt fires, both encoder channel interrupts will be enabled and the program starts to count the angle. Whenever the encoder bypasses the index channel again, the angle is 'reset' to the default value (90deg), making use of the additional information of having an absolute reference point. [code] DEVICE SX28, OSCHS3, TURBO, STACKX, OPTIONX FREQ 50_000_000 Signal_A1 PIN RB.0 INPUT SCHMITT 'Channel A Signal_A2 PIN RB.1 INPUT SCHMITT 'Channel A too Signal_I PIN RB.2 INPUT 'Index Channel Signal_B PIN RC.0 INPUT 'Does not create an interrupt, is just read within the interrupt handler angle VAR BYTE 'saves the current angle INTERRUPT ISR_Start: WKPND_B = int_storage IF int_storage = %0001 THEN 'Channel A1 interrupts on rising edge. IF signal_B = 1 THEN 'count only, if B is high angle = angle - 1 'decrement (or increment?) ENDIF ELSEIF int_storage = %0010 THEN 'Channel A2 interrupts on falling edge. IF signal_B = 1 THEN 'count only, if B is high angle = angle + 1 'increment (or decrement?) ENDIF ELSE 'Index Channel Interrupt (including combinations of Index and the A channels). WKEN_B = %11111000 'enable channel A interrupt ports (actually only has to be done after the first fire (as A is initally disabled). 'but to a routine would slow the process, which is not good. angle = 90 'reset angle to 90deg ENDIF ISR_Exit: WKPND_B = %00000000 'clear pending register, ALL bits RETURNINT PROGRAM Start Start: WKPND_B = %00000000 'clear pending register WKED_B = %00000010 'rising edge detect on the whole B Byte, except the second port of A channel WKEN_B = %11111011 angle = 0 DO if angle = 80 then led1 = ison endif if angle = 100 then led1 = isoff endif LOOP END [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=375331#m376363 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2009 (http://www.dotNetBB.com)