PDA

View Full Version : Encoder rotation calculation



Stefan
- 31st October 2007, 03:46
Greetings, I am new member and this is my first post.
Please help me how to read encoder speed and direction for my application.

I am using 24 position rotary encoder, pulled high, connected to ground when active. PIC 16F88
Variables are : speed , direction

This is what I want:
1) If I turn the shaft slowly ( say 1 rpm ) then speed = 1
2) If I turn the shaft fast ( say 10 rpm ) then speed = 255
3) The direction will be 1 for clockwise, 0 for ccw rotation

RodSTAR
- 31st October 2007, 07:37
Greetings, I am new member and this is my first post.
Please help me how to read encoder speed and direction for my application.

I am using 24 position rotary encoder, pulled high, connected to ground when active. PIC 16F88
Variables are : speed , direction

This is what I want:
1) If I turn the shaft slowly ( say 1 rpm ) then speed = 1
2) If I turn the shaft fast ( say 10 rpm ) then speed = 255
3) The direction will be 1 for clockwise, 0 for ccw rotation

First of all, to get direction, you may have a quadrature encoder [2 light sources, 4 pin interface]
There are lots of sample codes and discussions about this topic over this forum. you could use them as a base for your creativity.
One option is to use PULSIN. I haven't used that method.
Other more powerfull option (I use) is to play with Interrupts to access PIC's hardware Timers. Timers (timer0) works in two modes:
a) as Timer (T0CS=0). TMR0 variable counts instruction cycles (as time reference) between capture period. Real time depends on OSC & bits settings and crystal speed.
b) as Counter (T0CS=1). TMR0 variable counts pulse's rising edge (T0SE=0) or falling edge (T0SE=1)

Read datasheet for the other timer's info.
One of the most beautifull of this is that this can work in background while you run other code.

At least that's what I understood from datasheet.