Go Down

Topic: Encoder Counting Using PIC (Read 5511 times) previous topic - next topic

RuH_iranga

Component:
16f877
POLOLU encoder motor  http://www.pololu.com/catalog/product/1447 2096 pulses per one shaft revolution.

Use RB0 interrupts and turn on the PORTB pull-up resistors.
use MikroC language.

Code: [Select]

int count=0;           // This count use in interrupt routine

void interrupt()               // enter when RB0 Falling
{
  if(portb.f0&portb.f1)         // By checking B0 and B1, Position and Direction Find
  {count++;                     // If "+" direction
  }
  else                          // If "-" direction
  {count--;
  }
  INTCON.INTF=0;                // Clear flag
}

void main()
{
     Pwm_Init(5000);
     PWM_Start();
     TRISB=0b11111111;          // B0 and B1 encoder inputs
     TRISD=0b00000000;          // Motor Control Pin
     INTCON=0b11010000;         // enable RB0 interrupts
     OPTION_REG=0b01000000;     // Enable portB internal PULL-UP resistors
                                // And Rising edge of RB0 int
     Pwm_Change_Duty(240);      // Whatever speed
     portd.f1=1;                // Motor drive to "+"
     portd.f0=0;

     while(1)
     {

         if (count>4192)    // Count exceeds 4192(2 round), Stop
         {
           portd.f1=0;
           portd.f0=0;
         }

         
     }
}


By changing RD0 and RD1 you can change motor direction, then it should be reduced the count. and also change the (count<-4192) you can ensure that program detect the position as well as direction. this just reading encoder and you can use it with your application. Also possible to use 10k external Pull-UPs on B0 and B1.


Thanks.

mowcius

Ok, I'm intrigued, why did you post it?
Is it in some way a question or Arduino related?

RuH_iranga

It may helps to someone like me. sorry for use C code.

mowcius

Quote
It may helps to someone like me. sorry for use C code.

Possibly but not on this forum - this is the arduino forum and you just posted non-'arduino standard' code for a non-arduino chip.

Perhaps a PIC forum would be a better bet.

Go Up