My Very first code

Thread Starter

R!f@@

Joined Apr 2, 2009
9,682
Hi there fellow members.Below is the first attempt to write a code alone and it did work but as usual it has bugs.
#define Bank0 BCF STATUS,RP0
#define Bank1 BSF STATUS,RP0
#define X GPIO,5 ; Encoder X,Y impute on GPIO.GP<5:4>
#define Y GPIO,4
#define DR GPIO,0 ; Direction indiacator, a HIGH for CW and a LOW for CCW
#define Pout GPIO,1 ; Output Pulse as the Encoder is rotated either direction
;------------------------------------------------------------------------------
;RESET VECTOR

ORG 0x0000 ; processor reset vector
GOTO MAIN ; go to beginning of program

;------------------------------------------------------------------------------
;Interrupt Service

ORG 0x0004 ; interrupt vector location

;Oscillator Caliberation

errorlevel -302
Bank1 ; set file register bank to 1
call 0x3FF ; retrieve factory calibration value
movwf OSCCAL ; update register with factory cal value
Bank0 ; set file register bank to 0
errorlevel +302

;-------------------------------------------------------------------------------
; MAIN ROUTINE

MAIN
clrf GPIO ; Clear Outputs
movlw 07h ; Disable-
movwf CMCON ; comparator

Bank1
movlw b'111000'
movwf TRISIO

Bank0
bcf DR
bcf Pout


CheckEncoder
btfsc X
goto CWR
btfsc Y
goto CCWR
goto CheckEncoder

CWR
btfss X
goto CheckEncoder
bsf DR
btfss Y
goto CWR
bsf Pout
CW1 btfsc X
goto CW1
bcf Pout


CW2
btfsc Y
goto CW2
goto CheckEncoder


CCWR
bcf DR
bcf Pout
btfss Y
goto CheckEncoder
bcf DR


CCW1
btfss X
goto CCW1
bsf Pout


CCW2
btfsc Y
goto CCW2
bcf Pout


CCW3
btfsc X
goto CCW3
goto CheckEncoder


end
This circuit used a 12F629, a standard rotary encoder is connected to two inputs that are held low by pull downs. Two output drives two LED.
One indicates direction of rotation. (a lit LED for CW and an off LED for CCW rotation).
The other one pulses as the shaft is rotated in either direction.
When hard wired these are the results.

At first I tested with CW rotation, first wrote up that part of code and tested and it works perfectly. the CCW rotation is not tested during this time.
The CW is lit continuously and the other one pulses as the shaft is rotated.

Now after writing the CCW part. the code works but now the CW also creates a bit of problem, the CW LED turns off sometimes and same thing happens during CCW and the pulse LED sometimes hardly pulses. :confused:

Now my observation is contact bounce so I am trying to put some delays in between the direction instructions. But am not sure of how much a delay that I should give.

Any comments are most welcome and of course if any of you can think of a better way will be most appreciated.

Rifaa
 

n9352527

Joined Oct 14, 2005
1,198
I haven't go through your code yet, so i'll not comment on that.

The encoder, is it an optical encoder? I think there would be no contact bounce on rotary encoder. It usually doesn't use mechanical switches to generate the pulses.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,682
I haven't go through your code yet, so i'll not comment on that.

The encoder, is it an optical encoder? I think there would be no contact bounce on rotary encoder. It usually doesn't use mechanical switches to generate the pulses.
That was my very first hiccup :p
I assumed that even though it is a mechanical one which just scratches the surface there would be no the bouncing thingy problem.
But if I carefully veryyyyyyyyyyy slowly rotate, it indicated some kind of bouncing, I guess when the leaf is dragged on at the very edge of the carbon, there is a bouncing at atomic level. :D... u know what I mean.

WELL! NOT BOUNCING BUT DRAG BOUNCING.. umm some thing like that

I still working on the delay routine so Ill comment later
Rifaa
 

rjenkins

Joined Nov 6, 2005
1,013
At a quick look through the code, everything seems to follow a specific program path for a given state on the encoder inputs.

This will not give directional information, as the two output both cycle regardless of direction.

Also, you are reading the actual inputs multiple times at different points in the program, so the inputs could change part way through.

You need to read and store the inputs in ram at the start of a loop, then refer to the RAM image in the main code.

At the end of the loop, copy the saved values to a 'last pass' store before reading the new set of values.

By comparing the inputs between the newly fetched data and the last pass data, you can can see how the inputs have *changed*, and it is the sequence of changes that give the directional information.

Typical encoder quadrature output sequence:
Clockwise
A=01100110011001100
B=00110011001100110

Anti-clockwise
A=01100110011001100
B=11001100110011001
 
Top