CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how to use encoder code?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
williamho
Guest







how to use encoder code?
PostPosted: Wed Jun 22, 2005 11:04 pm     Reply with quote

Hi,
I am new in PIC and I am using 18F458 at 40MHz.
I need to capture optical encoder at 500 counts/rev at 1800 rpm.
I noticed that someone posted encoder code at below with remarks
that this code can gives a "maximum rotation rate of about 1800 rpm on 500 line encoder".

If this code can be used for my application, can anyone guide me how to put this code in main{} and how to printf it out?

Thanks.


signed int32 position;
#INT_RB
void quad(void) {
static int old;
static int new;
static int value;
//Here I have an edge on one of the quadrature inputs
new=portb;
/*Now I have to decode the quadrature changes. There are four
possibilities:
I can have a rising or falling edge, on each of the two inputs. I have to
look at the state of the other bit, and increment/decrement according to
this. /*
value=new^old;
//'value', now has the bit set, which has changed
if (value & 0x10) {
//Here the low bit has changed
if (new & 0x10) {
//Here a rising edge on A
if (new & 0x20) --position;
else ++position;
}
else {
//Here a falling edge on A
if (new & 0x20) ++position;
else --position;
}
}
else {
//Here the high bit (B) must have changed
if (new & 0x20) {
//Here a rising edge on B
if (new & 0x10) ++position;
else --position;
}
else {
//Here a falling edge on B
if (new & 0x10) --position;
else ++position;
}
}
old=new;
}
Ttelmah
Guest







PostPosted: Thu Jun 23, 2005 4:10 am     Reply with quote

The code must _not_ go in main.
It is interrupt driven, by the portB 'changed' interrupt. You need to connect the two encoder lines to port B4, and B5. Then in main, have:
Code:

   enable_interrupts(INT_RB);
   enable_interrupts(global);


The code will automatically be called whenever the encoder moves. At any time in main, you can simply read the value of the variable caled 'position', to get a current location 'count'.
So, if you did:
Code:

//have a int16 variable defined called 'old_position'  as well
   if (old_position!=position) {
       old_position=position;
       printf("\f%5ld",old_position);
       delay_ms(10); //Just to avoid too fast printing...
   }


Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group