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 CCS Technical Support

I look for C cod for optical encoder, thks

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







I look for C cod for optical encoder, thks
PostPosted: Wed Apr 21, 2004 1:35 am     Reply with quote

I'm begginer with CCS and with C at all. And I need from C code for optical encoder, whitch determine directions.
Thks in advance.
Ttelmah
Guest







Re: I look for C cod for optical encoder, thks
PostPosted: Wed Apr 21, 2004 2:13 am     Reply with quote

beginner with C wrote:
I'm begginer with CCS and with C at all. And I need from C code for optical encoder, whitch determine directions.
Thks in advance.

There are lots of ways of doing this, with the simplest/best, depending on the actual speed that the pulses are likely to arrive. This is a version designed to use the portb 'changed' interrupt, with minimum hardware needed to handle reasonable speeds. This requires that the two pulses are coming into pins b4, and b5, and that these are setup as inputs, and the interrupt enabled. You can go significantly faster, by using a logic gate to decode the direction, and using the CCP pins to count the movement. Details of this are given in a couple of Microchip application notes, on 'servos'.
Code:

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;
}

This code just updates the value 'position', which is a 32bit number, increasing it for every edge in one direction, and decreasing it in the other. This decodes all four of the possible quadrature states (giving 2000 changes on a 500 'line' encoder), and despite looking fairly long, executes quickly, with just three integer 'tests', and a single increment/decrement, whichever edge has changed. The limiting factor on speed here, is th interrupt latency of the processor. Typically on a 20MHz 16F877 for example, the code will handle up to about 60000 edges/second (giving a maximum rotation rate of about 1800rpm, on a 500 line encoder).

Best Wishes
guest
Guest







optical encoder
PostPosted: Wed Apr 21, 2004 5:28 am     Reply with quote

Hi

I could look on the example from ccs.

You can use an IC LS7083 from
http://www.lsicsi.com/

I found it on internet.

Quadrature Encoder ISR
by David Kott

struct RotaryEncoderStruct {
int unused:4; // RB[0:3]
int A:1; // RB4
int B:1; // RB5
int unused2:2; // RB6-7
};

struct RotaryEncoderStruct RotaryEncoder;

#pragma BYTE PORTB = 0x06

#pragma BYTE RotaryEncoder = 0x06 // Place structure right over PORTB at
location 0x06


USHORT g_usEncoderCount = 0;

#pragma INT_RB
void
PortBInterrupt (void)
{

static BYTE OldPortB;
BYTE byTemp;
static struct RotaryEncoderStruct OldEncoder;


if (OldPortB != (byTemp=(PORTB & 0x30))) {


OldPortB = byTemp;

if (RotaryEncoder.A == OldEncoder.A) {
if (RotaryEncoder.B == RotaryEncoder.A)
--g_usEncoderCount;
else
++g_usEncoderCount;
} else {
OldEncoder.A = RotaryEncoder.A;
if (RotaryEncoder.B == RotaryEncoder.A)
++g_usEncoderCount;
else
--g_usEncoderCount;
}



}

// Handle more PORTB stuff here, like a button, or something.


}
beginner with C
Guest







PostPosted: Thu Apr 22, 2004 5:33 am     Reply with quote

Thanks all
I succssed.
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