counter in PIC

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

counter in PIC

Postby shafiz » Wed Jan 19, 2011 10:16 pm

im now taking final project, making an autonomous robot using encoder to determine the distance it has move.
can the PIC act as a counter?
shafiz
Greenhorn
 
Posts: 3
Joined: Wed Jan 19, 2011 10:06 pm

Re: counter in PIC

Postby shahrul » Thu Jan 20, 2011 12:16 am

That is general question, the answer is YES..
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: counter in PIC

Postby temporary40 » Mon Feb 07, 2011 11:32 pm

You can use TIMER as counter. You can also use EXTERNAL INTERRUPT and count up at every interrupt. You can also use QEI if you encoder is a quadrature encoder.
temporary40
Novice
 
Posts: 23
Joined: Thu Apr 01, 2010 9:30 pm

Re: counter in PIC

Postby ABSF » Tue Feb 08, 2011 9:32 am

How accurate do you want your distance travelled to be? ARe you using this distance to calculate the speed of your robot?

If the accuracy is not an issue, the method used could be the same as a bike computer. A bike computer uses a magnet mounted on the front wheel spoke. And a reed switch is mounted on the stationary wheel support. When the wheel turns one round, it will be detected by the reed as a pulse on the PIC. So the circumference of the wheel is the distance covered. If more accuracy is required, then 4 or 5 magnets may be mounted and the distance will be 1/4 or 1/5 of the circumference of the wheel. If speed is required, you'll need a RTC to get distance travelled /time spent = Speed.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: counter in PIC

Postby shafiz » Tue Feb 08, 2011 8:23 pm

thanks...
the problem is now to program the pic..can somebody tell me how to program pic to make a counter..i preferring to use c programming.please help me!!!
shafiz
Greenhorn
 
Posts: 3
Joined: Wed Jan 19, 2011 10:06 pm

Re: counter in PIC

Postby shahrul » Tue Feb 08, 2011 8:56 pm

shafiz WROTE:thanks...
the problem is now to program the pic..can somebody tell me how to program pic to make a counter..i preferring to use c programming.please help me!!!

You want to count the encoder. My way is use RB0 interrupt. You see my example DC Motor.

If you don't want use interrupt, you can use normal code.
But if you do
CODE: SELECT_ALL_CODE
if(RB0==1) counter++;

it's will never working. You should count at the positive edge or negative edge.
CODE: SELECT_ALL_CODE
data=((data<<1)|RB0)&0x03;
if(data==0x01) counter++;

If hard to understand, you just try the code.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: counter in PIC

Postby shafiz » Sun Feb 20, 2011 11:19 pm

thankz for the reply..but what if i want to use the pic timer as a counter..is it possible?..and how to write the code?thankz again.
shafiz
Greenhorn
 
Posts: 3
Joined: Wed Jan 19, 2011 10:06 pm

Re: counter in PIC

Postby shahrul » Sun Feb 20, 2011 11:37 pm

shafiz WROTE:thankz for the reply..but what if i want to use the pic timer as a counter..is it possible?..and how to write the code?thankz again.

Counter timer usually do if you want count specific time. Also can use Timer0 interrupt. You can see example on Timer Module or Timer Interrupt.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 4 guests

cron