aeroboy
Joined: 26 Apr 2009 Posts: 9
|
3 encoder reading and serial transmition |
Posted: Thu Apr 30, 2009 7:19 pm |
|
|
Hello,
I made a code to read three differential encoders, save the increments each one does and send that increments to other pic every 6.3 ms via serial comunication. Also I am recieveing data. Since I have three encoders what I think I could do is to read the encoders as fast as possible. The problem its that I am losing data. The encoders have a resolution of 1200 per revolution. Is there a way to fix the loss of data or I had better change to absolute encoders?
Thanks.
--Luis
Code: |
#include <16F877A.h>
#include <stdlib.h>
#define LED PIN_C4
#use delay(clock=20000000)
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7)
#byte PORTB=0x06 //Define PORTB in memory location
#byte PORTC=0x07 //Define PORTC in memory location
#byte PORTD=0x08 //Define PORTD in memory location
#byte TXREG=0x19 //Define TXREG in memory location
#byte RCREG=0x1A //Define RCREG in memory location
unsigned char dirp,diry,dirr; // Direction of each sensor
struct VBITS{ // structure with the values of the encoders
unsigned char uA:1; //UnusedA
unsigned char uB:1; //UnusedB
unsigned char pA:1; //PitchA
unsigned char pB:1; //PitchB
unsigned char yA:1; //YawA
unsigned char yB:1; //YawB
unsigned char rA:1; //RollA
unsigned char rB:1; //RollB
};
unsigned char CHANN;
#INT_RDA // Recieve interrupt
void RS232C_Recieve_Interrupt()
{
CHANN=RCREG;
// delay_us(20);
PORTD=CHANN;
// delay_us(20);
}
signed char CHANN2,PWMX2; // Variables to send data
signed char PitchCnt; // Variables to count positions
signed char YawCnt;
signed char RollCnt;
short flag=1;
#INT_TIMER1 // Timer 1 Interrupt
void Timer1_Interrupt()
{
flag=~flag;
set_timer1(0xF09F); // Initializes timer1
// Send Channel or encoder
if(CHANN2==PWMX2) // If equal send convertion
{
if(PWMX2==123)
{
TXREG=PitchCnt;
PitchCnt=0; // Send Pitch to PIC 3
}
if(PWMX2==124)
{
TXREG=YawCnt;
YawCnt=0; // Send Yaw to PIC 3
}
if(PWMX2==125)
{
TXREG=RollCnt;
RollCnt=0; // Send Roll to PIC 3
}
++CHANN2; // Increment channel
if(CHANN2>125)
{
CHANN2=123;
}
}
else
{
TXREG=CHANN2; // Send value to PIC 1
PWMX2=CHANN2; // Update channel
}
}
void main()
{
int a;
struct VBITS TEMP;
struct VBITS OLD;
a=1;
set_tris_c(0b11000000); //PWM1 & 2 Output
//LED PORT Output
// USART TX & RX
set_tris_b(0xFF); // Port B all Input
set_tris_d(0x00); // Port D all Output
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
// clock source=internal=clock/4=5 MHz
// overflow at every 1 / 5MHz * ( 2^16 - 0xF09F ) = 0.0007874 sec
// prescaler = 8 | 3937 |
// interrupt every 0.0001874 * 8 = .0062992 sec
set_timer1(0xF09F); // Initializes timer1
enable_interrupts(INT_RDA); // Enable recieve interrupt
enable_interrupts(INT_TIMER1); // Enable timer1 interrupt
enable_interrupts(GLOBAL);
output_high (LED); // Turn on LED
YawCnt=0; // Initialize variables
PitchCnt=0;
RollCnt=0;
OLD=PORTB;
TEMP=OLD;
CHANN2=123;
PWMX2=0;
while(a==1)
{
TEMP=PORTB; // Save Actual Sensor Value
/*F O R P I T C H*/
if ((OLD.pA != TEMP.pA) && (OLD.pB != TEMP.pB)){
if(dirp==1) PitchCnt=PitchCnt+2;
if(dirp==0) PitchCnt=PitchCnt-2;
}
else{
if ((OLD.pA != TEMP.pA) || (OLD.pB != TEMP.pB)){
if (TEMP.pA == OLD.pA){
if (TEMP.pB == TEMP.pA){
--PitchCnt;
dirp=0;
}
else{
++PitchCnt;
dirp=1;
}
}
else{
if (TEMP.pB == TEMP.pA){
++PitchCnt;
dirp=1;
}
else{
--PitchCnt;
dirp=0;
}
}
}
}
/*F O R Y A W*/
if ((OLD.yA != TEMP.yA) && (OLD.yB != TEMP.yB)){
if(diry==1) YawCnt=YawCnt+2;
if(diry==0) YawCnt=YawCnt-2;
}
else{
if ((OLD.yA != TEMP.yA) || (OLD.yB != TEMP.yB)){
if (TEMP.yA == OLD.yA){
if (TEMP.yB == TEMP.yA){
--YawCnt;
diry=0;
}
else{
++YawCnt;
diry=1;
}
}
else{
if (TEMP.yB == TEMP.yA){
++YawCnt;
diry=1;
}
else{
--YawCnt;
diry=0;
}
}
}
}
/*F O R R O L L*/
if ((OLD.rA != TEMP.rA) && (OLD.rB != TEMP.rB)){
if(dirr==1) RollCnt=RollCnt+2;
if(dirr==0) RollCnt=RollCnt-2;
}
else{
if ((OLD.rA != TEMP.rA) || (OLD.rB != TEMP.rB)){
if (TEMP.rA == OLD.rA){
if (TEMP.rB == TEMP.rA){
--RollCnt;
dirr=0;
}
else{
++RollCnt;
dirr=1;
}
}
else{
if (TEMP.rB == TEMP.rA){
++RollCnt;
dirr=1;
}
else{
--RollCnt;
dirr=0;
}
}
}
}
OLD = TEMP; // Update Old Sensor value
}
}
|
|
|