Interfacing a strip encoder with the dsPIC30F4011

This is what a strip encoder looks like:

strip_sensor

They are used to add position feedback to things that move in a linear way. For example, printers and scanners. Some printers use DC motors with strip encoders as opposed to stepper motors. The reason for this likely because DC motors are cheaper than steppers.

 

The printer parts used in this project were taken from a combined printer/scanner machine. It contained two of these encoders. The encoder from the scanner was experimented on in order to try and figure out the pinout of the device. The encoder consisted of two parts, a red led and a photosensitive transistor. The LED pinout was the easy part, just guess which is positive and which is negative, if that doesn’t work, switch the power cables. The slightly more tricky part was the encoder. It had four pins coming out of it. After guessing that two of these pins were for power ( positive and negative ) the other two pins are signal pins. The likely reason why there are two pins is for a thing called quadrature feedback. This allows the direction to be determined from the phase of the pulses.

quadrature

How position is determined from the above picture is as follows. In the above picture, on the rising edge of A, is B high or low? Is B a 1 or a 0? Whether the encoder strip is moving left or right will determine that answer to that. You can see in the picture in one direction B will be high on the rising edge of A, in the other direction it will be a zero.

For the purposes of this project, ( controlling the position of a printer head ) only one channel is needed. This is because the direction of the printer head will be determined by the microcontroller at all times. The output from the encoder becomes a simple single square wave like in the image below.

SquareWave

These pulses are easily read by the microcontoller. The pulses were read into the microcontroller using interrupts. In this case, as soon as a falling edge is detected on the external interrupt pin int0, the microcontroller jumps to an interrupt service routine (ISR). This routine simply increments a counter by 1. See the code related to the ISR below:


//
// dsPIC30F4011 example - using an ISR to increment a counter
// Written by Shane Ormonde (using Ted Burke's template)
//

#include <xc.h>
#include <libpic30.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

//Functions
void setup(void);
void _ISR _INT0Interrupt(void);

//Variables
int counter = 0;

int main(void)
{
setup();

while(1);
}

void setup(void)
{
IFS0bits.INT0IF = 0;  // int0 flag bit
IEC0bits.INT0IE = 1;  //int0 interrupt enabled
IPC0bits.INT0IP = 3; // int0 prority is 7
_LATD0 = 0;
}

void _ISR _INT0Interrupt(void)
{
IFS0bits.INT0IF = 0;  // int0 flag bit

counter++;

}

Leave a Reply

%d bloggers like this: