Hi All,
This is my FIRST trial using interrupts, something i've never used before, so it's baby steps for me at the moment ;-)
I'm trying to migrate some Rotary Encoder code from an 08m to my 40X2.
It's originally from this posting by "Vertigo" Here
I have prefixed all MY changes with ";;OLDMARTY" and the original (08m) code is prefixed with ";;ORIGINAL" just so you can see what i've changed.
I'm using Port A on the 40X2 (bits A.0, A.1), so everyting is migrated from Port C on the 08m to Port A.
(I've confirmed in the SETINT docs, that 40X2 can use interrupts on A.0 to A.3)
At the moment my 40X2 code seems to only count in one direction, always decreasing the counts with each 'click' of the encoder.
I'm using a standard 2 bit quadrature encoder, with pins A and B being the outputs and a common pin.
Am i doing something as simple as using pins A & B the wrong way around?
It looks like simple code (for me) to understand, but have i missed something in the migration???
This is my FIRST trial using interrupts, something i've never used before, so it's baby steps for me at the moment ;-)
I'm trying to migrate some Rotary Encoder code from an 08m to my 40X2.
It's originally from this posting by "Vertigo" Here
I have prefixed all MY changes with ";;OLDMARTY" and the original (08m) code is prefixed with ";;ORIGINAL" just so you can see what i've changed.
I'm using Port A on the 40X2 (bits A.0, A.1), so everyting is migrated from Port C on the 08m to Port A.
(I've confirmed in the SETINT docs, that 40X2 can use interrupts on A.0 to A.3)
At the moment my 40X2 code seems to only count in one direction, always decreasing the counts with each 'click' of the encoder.
I'm using a standard 2 bit quadrature encoder, with pins A and B being the outputs and a common pin.
Am i doing something as simple as using pins A & B the wrong way around?
It looks like simple code (for me) to understand, but have i missed something in the migration???
Code:
;
;OLDMARTY ROTARY ENCODER TRIAL for 40X2
;
;
#PICAXE 40X2 ; Define PICAXE 40X2 type
#NO_DATA ;Stop EEPROM data from being cleared during programming
#NO_TABLE ;Ignore TABLE data - loads program faster
#Terminal 9600
;
;
'picaxe 08M
'
' +V 1 U 8 0V
' GND - 10K 2 7 (0) OUT
' I/O/ADC4 (4) 3 6 (1) I/O/ADC1 --- rotary encoder
' IN3 (3) 4 5 (2) I/O/ADC2 --- rotary encoder
'
' b1 b0 b3 b2 b5 b4 b7 b6 b9 b8 b11 b10 b13 b12
' --w0-- --w1-- --w2-- --w3-- --w4-- --w5--- --w6---
'b0 = bit7 : bit6 : bit5 : bit4 : bit3 : bit2 : bit1 : bit0
'b1 = bit15 : bit14 : bit13 : bit12 : bit11 : bit10 : bit9 : bit8
#rem
Rotary encoder connection:
0V
|
4.7K
|
o--.----(A) pin1 (phys 6) - ;; OLDMARTY Port A.0
o--330-- +5V
o--.----(B) pin2 (phys 5) - ;; OLDMARTY Port A.1
|
4.7K
|
0V
Encoder result (early detection):
pin1 pin2
1 0 one direction
0 1 other direction
0 0 init and final (= "detent") status
One direction: pin1 goes high before pin2
___ ___
pin1 ___| |___| |___
___ ___
pin2 _____| |___| |___
Other direction: pin2 goes high before pin1
___ ___
pin1 _____| |___| |___
___ ___
pin2 ___| |___| |___
#endrem
' Used bits xxxx xxxx
symbol getBits = b0 'b0 = bit7 : bit6 : bit5 : bit4 : bit3 : bit2 : bit1 : bit0
symbol dir = b1
symbol counter = b3
;;ORIGINAL
;;setint %00000010,%00000010 'interrupt on pin1 (C.1) high only
;;OLDMARTY
setint %00000001,%00000001,A ;interrupt on pin A.0 high only
main:
do
sertxd("Main loop - Counter=",#b3,13,10)
pause 250
loop
interrupt:
;;ORIGINAL
;;bit2 = pin2: bit1 = pin1 'save rotary encoder pins status
;;OLDMARTY
bit2 = pinA.1: bit1 = pinA.0 'save rotary encoder pins status
;;ORIGINAL
;;getBits = getBits & %00000110 'isolate rotary encoder pins
;;OLDMARTY
getBits = getBits & %00000011 'isolate rotary encoder pins ;; on pins 0 & 1 (MARTY A.1, A.0)
if getBits <> 0 then 'if both pins are low, the direction is undetermined: discard
dir = bit2 * 2 'direction: if bit2=low then dir=0; if bit2=high then dir=2
counter = counter - 1 + dir 'change counter variable accordingly
do while getBits <> 0 'wait for the encoder to go to the next "detent" position
;;ORIGINAL
;;getBits = pins & %000000110 ;"pins" on an 08M refers to the entire byte of C port. 'pins' is replaced with 'pinsC' on X2 devices etc.
;;OLDMARTY
getBits = pinsA & %000000011
loop
endif
;;ORIGINAL
;;setint %00000010,%00000010 'restore interrupt on pin1 high
;;OLDMARTY
setint %00000001,%00000001,A ;interrupt on pin A.0 high only
return