Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

quadrature encoder programming

Status
Not open for further replies.

vibz86

Newbie level 6
Joined
Aug 17, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,373
quadrature encoder

Iv made a motor controller circuit for 2 motors & each has a quadrature encoder. The pic im using is pic16f877a. I got no idea regrading how im going to programme the pic to accommodate the encoders.

Herewith I have attached the motor controller programme with the encoder initializations. Please it would be a great help for me if someone can help me out to complete the code. I have to use the encoder inputs, calculate the speed and direction of the motors, check if they are correct and adjust the duty cycle of the motors if they are incorrect. As in not good in ccs or assembly, I prefer doing it in mikroc


#device PIC16F877

void m1(int direction)
{
if(direction = 1)
{
PORTC.F4=1;
PORTC.F5=0;
}
else
{
PORTC.F4=0;
PORTC.F5=1;
}
}

void m2(int direction)
{
if(direction=1)
{
PORTC.F6=1;
PORTC.F7=0;
}
else
{
PORTC.F6=0;
PORTC.F7=1;
}
}

void setup_timer0() //for encoder 1
{
//should be called after configuring the ports(coresponding tris)
TRISA.F4=1; //encoder signal input bit
OPTION_REG=0x80;
OPTION_REG.T0CS=1; //timer0 counter mode
}

void setup_timer1()
{
TRISC.F0=1;//encorder signal input bit
T1CON=0;//configure prescale of timer1
T1CON.TMR1CS=1;//To set RC0 as input source for timer1
T1CON.T1OSCEN=0;//internal oscillator disabled
T1CON.T1SYNC=1;//disable synchronization
T1CON.TMR1ON=1;//start timer1
}


void main()
{

PORTC=0;
TRISC=0;
setup_timer0();
setup_timer1();


PWM1_INIT(5000);//Both pwm generators share one pulse width value. So one
//initializing is enough
PWM1_change_duty(80);
pwm2_change_duty(200);
m1(1); //m1 forward
m2(0); //m2 backward
pwm1_start();
pwm2_start();



while(1)
{
delay_ms(1000);
PORTB=TMR0; //Encorder1 value
PORTD=TMR1L; //Encorder2 value
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top