Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

optical encoder help

Status
Not open for further replies.

j_boodoosingh

New Member
hey all im trying to use an quadrature optical encoder and haven't been able to succeed thus far..please help, the code is given below. What the code does is perform 2 initial A/D conversions on portA pins and then continuously samples pins 1 and 2 of port A. Converts the values and outputs to a lcd, and polls for buttons. Ive been trying for weeks to get the encoder to work properly. PLease Help.



#include <p18F452.h>
#include <xlcd.h>
#include <delays.h>
#include <adc.h>
#include <math.h>
#include <portb.h>
#include <stdio.h>
#include <encoder.h>



//..................Delays For LCD...............//
void DelayFor18TCY( void )
{
Nop(); Nop(); Nop(); Nop();
Nop(); Nop(); Nop(); Nop();
Nop(); Nop(); Nop(); Nop();
Nop(); Nop();
}
void DelayPORXLCD( void )
{
Delay1KTCYx(15); //Delay of 15ms
return;
}
void DelayXLCD( void )
{
Delay1KTCYx(5); //Delay of 5ms
return;
}

//..............................


int direction = 0;
int count = 0;
void encoder (void);
void encoder2 (void);

#pragma code high_vector_section=0x08
void
high_vector (void)
{
_asm GOTO encoder _endasm
}
#pragma code

#pragma code low_vector_section=0x18
void
low_vector (void)
{
_asm GOTO encoder2 _endasm
}
#pragma code


#pragma interruptlow encoder2
void
encoder2 (void)
{
if (PORTBbits.RB1 == 0){
direction++;
}
INTCON3bits.INT2IF == 0;
}
#pragma code

#pragma interrupt encoder
void
encoder (void)
{
INTCON3bits.INT1IF == 0;
if(PORTBbits.RB2==1){
direction++;
}
}

#pragma code




void main( )
{


double hund;
double ten;
double one;
double hunds;
double tens;
double ones;
long set1=0;
long set2=0;
long set3=0;
long set4=0;
int setx;
int sety;
int s3;
int s4;
int H;
int T;
int O;
int HH;
int TT;
int OO;
int HHH;
int TTT;
int OOO;
int output1;
long int rx;
long int ry;
long int sen1;// voltage from sensor 1
long int sen2;// voltage from sensor 2
long int sen3=0;
long int sen33=0;
long int off1;//offset voltage for sensor 1
long int off2;//offset voltage for sensor 2
long int result=0;
int val;
int direction = 0;
TRISBbits.TRISB1 = 1; //enable encoder pins as inputs
TRISBbits.TRISB2 = 1;

RCONbits.IPEN = 1; // enable priority interrupts

INTCON2bits.INTEDG1 = 1;// edge select bits
INTCON2bits.INTEDG2 = 1;

INTCON3bits.INT2IF = 0; //clear flags
INTCON3bits.INT1IF = 0;

INTCON3bits.INT2IE = 1; //enable INT2 interrupt
INTCON3bits.INT1IE = 1; //enable INT1 interrupt

INTCON3bits.INT2IP = 0; //set priority to low
INTCON3bits.INT1IP = 1; // set priority to high
INTCONbits.GIEH = 1; //enable high and low proirity interrupts
INTCONbits.GIEL = 1;

OpenXLCD( FOUR_BIT & LINES_5X7 );
while(BusyXLCD());
WriteCmdXLCD(0x01);
while(BusyXLCD());

while( BusyADC() );
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8ANA_0REF, ADC_CH0 & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
off1 = ReadADC(); // Read result
CloseADC(); // Disable A/D converter

while (BusyADC());
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8ANA_0REF, ADC_CH1 & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
off2 = ReadADC(); // Read result
CloseADC(); // Disable A/D converter




//................Main Loop...................for(;;)
while (1)
{

// result = direction;

if (result < 0){
result = -result;
}

if ((result >181) && (result <= 360)){
result =360 - result;
}

if ((result > 360) && (result < 540)){
result = result - 360;
}

if ((result > 540) && (result < 720)){
result = 360 - result;
}
HHH = (result/100)%10+48;
TTT = (result/10)%10+48;
OOO = result%10+48;


//.......................................sample from ADC

while (BusyADC());
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8ANA_0REF, ADC_CH0 & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
sen1 = ReadADC(); // Read result
CloseADC(); // Disable A/D converter



if (off1>sen1)
sen1 = (off1 - sen1);
else
{
sen1 = sen1 - off1;
}


sen1=(sen1*50000)/1023;
sen1=sen1*72;

if (sen1>1810000)
{
sen1 = 1800000 - ( sen1 - 1800000);
}


hund = (sen1/1000000)%10+48;
ten = (sen1/100000)%10+48;
one = (sen1/10000)%10+48;

//...........................................................................//


while (BusyADC());
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8ANA_0REF, ADC_CH1 & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
sen2 = ReadADC(); // Read result
CloseADC(); // Disable A/D converter

if (off2>sen2)
sen2 = (off2 - sen2);
else
{
sen2 = sen2 - off2;
}

sen2=(sen2*50000)/1023;
sen2=sen2*72;

if (sen2>1810000)
{
sen2 = 1800000 - ( sen2 - 1800000);
}

hunds = (sen2/1000000)%10+48;
tens = (sen2/100000)%10+48;
ones = (sen2/10000)%10+48;


//........................................Set Angle............



if (PORTCbits.RC3 == 0) //if x is selected
{
if (PORTCbits.RC7 == 1){ //if the increment button is pressed
while (PORTCbits.RC7 == 1); //wait till they release button

if (set1 < 180)
{
++set1;
}
}
if (PORTCbits.RC5 == 1){ //if the increment button is pressed
while (PORTCbits.RC5 == 1); //wait till they release button

if (set3 < 18)
{
++set3;
}
}
}


if (PORTCbits.RC3 == 0) //if x is selected
{
if (PORTCbits.RC6 == 1){ //if the decrement button is pressed
while (PORTCbits.RC6 == 1); //wait till they release the button

--set1;
}
if (PORTCbits.RC4 == 1){ //if the decrement button is pressed
while (PORTCbits.RC4 == 1); //wait till they release the button


--set3;

}
}





//...................................... set 1 conversion for output to lcd
s3=set3*10;
setx = set1 + s3;
if(setx<0)
{
setx = 0;
}

H = (setx/100)%10+48;
T = (setx/10)%10+48;
O = (setx/1)%10+48;



//................................................................................


if (PORTCbits.RC3 == 1) //if y is selected
{
if (PORTCbits.RC7 == 1){ //if the increment button is pressed
while (PORTCbits.RC7 == 1); //wait till they release button

if (set2 < 180)
{
++set2;
}
}
if (PORTCbits.RC5 == 1){ //if the increment button is pressed
while (PORTCbits.RC5 == 1); //wait till they release button

if (set4 < 18)
{
++set4;
}
}
}

if (PORTCbits.RC3 == 1) //if y is selected
{
if (PORTCbits.RC6 == 1){ //if the decrement button is pressed
while (PORTCbits.RC6 == 1); //wait till they release the button

--set2;
}

if (PORTCbits.RC4 == 1){ //if the decrement button is //pressed
while (PORTCbits.RC4 == 1); //wait till they //release the button

--set4;

}
}


//......................................set 2 conversion for output to lcd
s4 = set4*10;
sety = set2 + s4;
if(sety<0)
{
sety = 0;
}
HH = (sety/100)%10+48;
TT = (sety/10)%10+48;
OO = (sety/1)%10+48;

//........................................visual output for set angle = sensor angle.



if (H == hund){
if ( T == ten){
if (O == one){

TRISCbits.TRISC1 = 0; //configure RC1 as input pin
PORTCbits.RC1 = ~PORTCbits.RC1; // complement RC1
}
else
{
TRISCbits.TRISC1 = 1; //configure RC1 as output pin
}
}
}

if (HH == hunds){
if ( TT == tens){
if (OO == ones){

TRISCbits.TRISC0 = 0; //configure RC0 as input pin
PORTCbits.RC0 = ~PORTCbits.RC0; // complement RC0
}
else
{
TRISCbits.TRISC0 = 1; //configure RC0 as output pin
}
}
}


//..................OUTPUT to LCD...................

while( BusyXLCD() );
putrsXLCD(" Y= ");
while( BusyXLCD() );
putcXLCD(HHH);
while( BusyXLCD() );
putcXLCD(TTT);
while( BusyXLCD() );
putcXLCD(OOO);
while( BusyXLCD() );


}
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top