Calculate Distance Using Rotary Encoder
Hi, I need help how to calculate distance using rotary encoder. I wanna show pulse in LCD.
The rotary encoder spesification as follows:-
Pulse/Revolution: 200 p/r
output: 2 phase, A & B
Micro C: PIC 16F877A, 4 MHz
I try the code below, but I cannot see the pulse of rotation exactly.
Code :
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
define PULSIN_MAX 200
ADCON1 = 4 ' Set PortA 0, 1, 3 to A/D inputs
pulse VAR BYTE
loop:
pulsin portd.2, 1, pulse
Lcdout $fe, 1
Lcdout #pulse
Pause 1000
GOTO loop
Try to use interrupts rather
Hi,
You are using two time consuming statements , LCDOUT and pause. If your encoder generates pulse in this phase you would loose count. So it is best to set an interrupt and increment the value in ASM. I do this for my projects. (A particular application is a depth indicator on a old mine tub line). In fact you can also use instant basic interrupts (See Darrel's post on instant interrupts) to handle this in basic itself. It gives you fair amount of speed without the pain of asm.
Good Luck
Wishing the speedy recovery of our forum member steve who is hurt in a car accident.
Best Regards
Sougata
Use Instant Interrupts by Darrel
Hi,
I understand that you are new to using interrupts but everybody has a first time so go ahead. This forum will always be beside you. Here http://www.picbasic.co.uk/forum/show...ant+Interrupts you will find more information. To learn more about how to setup the interrupts, read the datasheet carefully. Enable global interrupt and portb.0 interrupts (your encoder should be connected here), also set the Interrupt edge as per your encoder. Now when you are in the ISR just increment the counter variable. You should check overflow of your variable or use a comparison to increment a pulse/meter (another variable). It is sure you will find problems but do write some code and test.
Good Luck
Sougata
Have read the asm examle by Luciano
Hi there,
When you are turning the rotary encoder slower you get good results:
Reason : Your loop has enough time to "PAUSE" or "LCDOUT" and does not miss pulse.
When you are turning the encoder faster:
It is missing pulses while you are doing other tasks. It is always reliable to use an asm interrupt. No other way out perhaps.
Regards
Sougata
Can you post the full schematic
Hi,
I do not have a 16F877A lying around still I would try to one. I basically design with PIC18F452. The memory bank is different than 16F877A. At least I will try to simulate it.
Regards
Sougata
Re: Calculate Distance Using Rotary Encoder
Please help me....
I have a encoder of 1024 PPR and i want to have a Calculation of RPM.... Gear Ration is 150:1
Please help me with
Re: Calculate Distance Using Rotary Encoder
Hi, Rishab - ( Bashir :applause: ! )
It's quite simple : distance is number of pulses, multiplied by perimeter of the wheel, multiplied by 150 and divided by 1024 ... :excitement:
no, no ... no need any thanks .
any other Homework to give us ???
Alain
Re: Calculate Distance Using Rotary Encoder
Thanks a Lot but m Using Modicon PLC m218 and need to configure HSC counter for that also i need to convert it to MM for the movement of motor please help if you can!!!
Re: Calculate Distance Using Rotary Encoder
Are you very sure you are on the right forum ???
Here it is the Picbasic Pro for Microchip PICs forum ... not "abcelectronique" with the great and unique " Doudi" san ... :rolleyes:
Alain