reading an input pattern

Benjie

Senior Member
I am still in the initial learning using a 20x1. I successfully drove a unipolar step motor and now I would like to drive it using at the input a quadrature encoder taken from a computer mouse. The sequence code of the encoder is the same as the output sequence but I need to read the two inputs simultaneously and generate the outputs in accordance. This will allow to turn the step motor with a knob connected to the input encoder.

Reading the manual sec. 3 I found the setint command only capable of this but I think there is a better way to do it.

Can you please help?
 

BeanieBots

Moderator
You can use "let b0 = pins" and then mask off which inputs you are interested in.
Have a read of manual 2 page 22 for more details or ask again if the method is not clear.

You can still use interrupts to trigger an event which then reads in the entire port to get all the input pin states or you can simply poll.
Try a search for "encoder" or similar. Quadrature encoder methods have been covered here in depth before.
 

westaust55

Moderator
As there is no 20X1 PICAXE chip, I presume that you mean the 20X2 (not the 20M chip).

So when you wish to read the data from all pins on an X2 part IO port, you will need to use

Let b0 = pinsb ; and substitute “b” for which ever port you are actually using.

This is covered in the X2 addendum/briefing: http://www.rev-ed.co.uk/docs/picaxex2.pdf

If the operation is relatively slow, then directly monitoring the inputs using such a command, masking to identify the state of the two pins and using that information to drive the two outputs for control of the stepper motor is relatively straight forward.

Alternatively, if you are trying to do other activities as well as monitoring the inputs and driving the outputs to control the stepper motor, it may also be possible to use the Hardware Interrupt Pins (three available on X2 parts) with the HINTSETUP command but as each pin changes state you would need to also change the interrupt parameters to then be able to monitor the alternate state for the next half-cycle of the waveform.
 

boriz

Senior Member
This is the smallest routine I have yet written to decode a quadrature rotary encoder. (I’m still working on making it smaller). Don’t see the need for an interrupt.

Code:
do
	do
		b1=b0
		b0=pins & %11
	loop until b0<>b1
	b1=b1&%10/2^b0&%1
	if b1=1 then
		'encoder rotated one click clockwise
	else
		'encoder rotated one click anti-clockwise
	endif
loop
 

boriz

Senior Member
P.P.S. The calculation performs an XOR between the current state of pin0 and the previous state of pin1.
 

Benjie

Senior Member
Boriz,
last question of a newcomer: can you explain the line of your code "b1=b1&%10/2^b0&%1"?
I read b1= [b1 (only bit 1 ) divided by 2] excl.OR with b0 bit 0

The division by 2 is my question. I guess that the timing is what escape my understanding.
Thanks
 

Benjie

Senior Member
Great subtlety, I would had never imagine that!!

Now....why using the programming editor the simulation run stops at the 4th line "b0=pins &%11"? Only by clicking on the step by step button it proceeds as expected.

Learning is a slow process, sorry.
 

Technical

Technical Support
Staff member
Great subtlety, I would had never imagine that!!

Now....why using the programming editor the simulation run stops at the 4th line "b0=pins &%11"? Only by clicking on the step by step button it proceeds as expected.

Learning is a slow process, sorry.
Have you set a breakpoint by mistake on this line - look for the coloured mark in the margin.
 

MPep

Senior Member
This is the smallest routine I have yet written to decode a quadrature rotary encoder. (I’m still working on making it smaller). Don’t see the need for an interrupt.

Code:
do
    do
        b1=b0
        b0=pins & %11
    loop until b0<>b1
    b1=b1&%10/2^b0&%1
    if b1=1 then
        'encoder rotated one click clockwise
    else
        'encoder rotated one click anti-clockwise
    endif
loop
Hi Boriz,
Nice and concise code. Thanks for sharing that. Am thinking of a project that will include a quadrature encoder, so this helps tremendously.
Cheers,
MPep.
 
Top