Page 1 of 1

quadrature encoders pic18f2331

Posted: Fri Feb 22, 2008 11:03 pm
by arnaud2
hello
the chip have internal quadrature encoder counter on the timer5 how can i use it?
had someone a exanple to use this value?
thanks
arnaud
(sorry i am french my english isn't verry good)

Re: quadrature encoders pic18f2331

Posted: Mon Feb 25, 2008 10:15 am
by Benj
Hello

If you know your way around C then you can use a C code icon to control the quad encoder. There is a manual on the BoostC compiler available from the Flowcode V3/BoostC directory. The PICmicro device datasheet should have everything you need to get the encoder up and running.

Re: quadrature encoders pic18f2331

Posted: Tue Feb 26, 2008 9:56 am
by arnaud2
i dont know how to programming in c
have you a example to use this function
thanks

Re: quadrature encoders pic18f2331

Posted: Tue Feb 26, 2008 10:20 am
by Sean
Hello

I have attached a Flowcode program that uses some C code blocks to access the quadrature encoder module and display the count value on the lcd display. It was originally written for the 18F4431. I have changed the target device to the 18F2331 and compiled it successfully, but have not been able to test it.
Encoder2.fcf
(5.5 KiB) Downloaded 1118 times

Re: quadrature encoders pic18f2331

Posted: Tue Feb 26, 2008 11:07 am
by arnaud2
thanks a lot Sean
this program give me a value of the motion position in a 16 bit word(-32768 to 32767)
in the POSCNT word?
When the counter go over 32767 the counter retur to -32768 ?

Re: quadrature encoders pic18f2331

Posted: Tue Feb 26, 2008 3:56 pm
by Sean
The counter overflowing from 32767 to -32768 is the expected result when using a 16-bit signed integer.
0111 1111 1111 1111 = 32767
1000 0000 0000 0000 = -32768

There are a number of calculations that can be performed to change the range and resolution of the counter.

Example:
If you change the contents of the POSCNT calculation block from

POSCNT = ( POSCNTH << 8 ) + POSCNTL

to

POSCNT = (( POSCNTH << 8 ) + POSCNTL) AND 0x7fff

This will remove the negative half of the range and display a 15-bit result between 0 and 32767, overflowing from 32767 to 0 and underflowing from 0 to 32767.

If the 16th bit is tested before it is cleared, it can be used as a guard bit. it will only be set if an underflow or overflow has occured.

Re: quadrature encoders pic18f2331

Posted: Wed Feb 27, 2008 9:46 am
by arnaud2
in flowcode the interrupt int0 is on rb0 but in the datasheet of pic 18f2331 is on rc3
who can i connect the input?
I use int0 and tmr0 as interrupt can i select that int0 is in priority (when the 2 interrupt fall in the same time)?
THANKS

Re: quadrature encoders pic18f2331

Posted: Wed Feb 27, 2008 12:55 pm
by Benj
Hello

You are correct, RC3 is the INT0 interrupt pin that you will have to use. The reason that Flowcode states this pin is RB0 is that 99% of the time this is correct.

Even if both interrupts happen at the same time they will both be processed. It is probably not worth changing anything here as the possibility will be very remote of both interrupts occurring simultaneously.

quadrature encoders pic18f4331

Posted: Sat May 10, 2008 10:18 pm
by karthick
Dear Sean,

I have problems using the quad encoder interface using the pic18f4331. You have indicated that u had programmed the pic18f4331. Can i see that file. I am unable to use the Encoder.fcf file. I am not able to view it. So please help me.

Thannk you.

Re: quadrature encoders pic18f2331

Posted: Mon May 12, 2008 9:23 am
by Sean
You can not view Flowcode programs directly from the forum, but you should be able to open the program in Flowcode after saving it to your machine.

If you are still having problems reading the fcf file, here is the C file generated by Flowcode.
Most of the code for intefacing with the QEI is written in C code blocks so you should be able to see the use of the QEI registers.
Encoder2.c
(8.22 KiB) Downloaded 960 times

Re: quadrature encoders pic18f2331

Posted: Mon May 23, 2011 6:52 pm
by Mathy
Sean wrote:Hello

I have attached a Flowcode program that uses some C code blocks to access the quadrature encoder module and display the count value on the lcd display. It was originally written for the 18F4431. I have changed the target device to the 18F2331 and compiled it successfully, but have not been able to test it.
Encoder2.fcf

Hello !

Im trying your program with a 18F4431. I think It's works a little because I can transmit a byte trough the RS232 component on my PC when the encoder move.
But I don't understand all the variable you use.

What is exactly MAXCNT ? Why the limit off 4999 ?

I would like to store the position on a DC motor. So I have to count all the pulse and store it into an flash or eeprom memory to ask the motor to return to his old position.
If I try for exemple to stop my motor at POSCNT = 2000. My motor stop almost immediatly but I'm pretty sure that the 2000 pulse are not reaches.

Where do you think the problem come from ?

Other question : Is it possible to count the pulse with a floating point variable to count up to 4 000 000 000 pulses ?

Thank you for your help :)

Mathy.

Re: quadrature encoders pic18f2331

Posted: Tue May 24, 2011 10:55 am
by Benj
Hello Mathy,

Not sure about the maxcount question hopefully Sean knows more about this.

With your poscnt problem are you resetting the poscnt variable to 0 at the start if your program?

Regarding using a float to count this would work but floats require a large amount of program memory to allow manipulation. It would probably be much more code efficient to use longs via C code icons.

Defining counter variable

Code: Select all

unsigned long counter = 0;
Adding 1 to counter variable

Code: Select all

counter = (long) counter + 1;
Reading Variable back into 4 x Flowcode byte variables.

Code: Select all

FCV_VAR1 = counter;
FCV_VAR2 = counter >> 8;
FCV_VAR3 = (long) counter >> 16;
FCV_VAR4 = (long) counter >> 24;

Re: quadrature encoders pic18f2331

Posted: Tue May 24, 2011 11:26 am
by Sean
The MAXCOUNT value demonstrates a feature of the encoder module that allows the position to overflow back to zero at values other than the 65535 limit of the main counter. This value is also loaded into the main counter if it underflows (running backwards) instead of 65535.

The value can be used to represent individual revolutions, specific distances etc. The under/overflow event can be detected and used to keep track of large counts in measured units.

Re: quadrature encoders pic18f2331

Posted: Wed May 25, 2011 8:42 am
by Mathy
Hi !

Thank you for your answers.

I'll try this afternoon, taking account of my mistakes. My optical encoder is on the engine and not on my gear box. So I have 25000 pulse per revolution !

It is not too much ? What is the maximum pulse / revolution the microcontroler can count ? The engine can turn up to 300 tr/m.

I'm at 20 Mhz but I will try with a 10 Mhz x 4PLL or a 40 Mhz crystal. That I would like to do is impossible ?

I am waiting for my 20 pF capacitor for the 40 Mhz quartz and I redo some tests.

Thanks and see you soon.

Re: quadrature encoders pic18f2331

Posted: Thu May 26, 2011 5:46 pm
by Mathy
Benj wrote:Hello Mathy,

Not sure about the maxcount question hopefully Sean knows more about this.

With your poscnt problem are you resetting the poscnt variable to 0 at the start if your program?

Regarding using a float to count this would work but floats require a large amount of program memory to allow manipulation. It would probably be much more code efficient to use longs via C code icons.

Defining counter variable

Code: Select all

unsigned long counter = 0;
Adding 1 to counter variable

Code: Select all

counter = (long) counter + 1;
Reading Variable back into 4 x Flowcode byte variables.

Code: Select all

FCV_VAR1 = counter;
FCV_VAR2 = counter >> 8;
FCV_VAR3 = (long) counter >> 16;
FCV_VAR4 = (long) counter >> 24;
Hi !

Thank you for the tips but how to increment this counter from POSCNT?
As POSCNT reset at 65535, the counter will never exceed this value.

I don't understand how to do this.

I know, I should stop electronics....

Mathy.

Re: quadrature encoders pic18f2331

Posted: Tue May 31, 2011 5:19 pm
by Mathy
Hello :)

Any ideas ?

Mathy.

Re: quadrature encoders pic18f2331

Posted: Tue May 31, 2011 5:34 pm
by Sean
The encoder module can be configured to generate an interrupt when an over/under-flow occurs. Alternatively, the count value can be polled regularly so that over/under-flow events can be detected in software.

In either case, the events can be used to increment/decrement an additional counter (e.g. a Flowcode integer) to provide a +/-32767 multiple of the counts available directly from the encoder module.

Re: quadrature encoders pic18f2331

Posted: Wed Jun 01, 2011 9:36 am
by Benj
Hello Mathy,

Are you using the interrupt method or the polling method? This dictates whether you are counting multiples of 65536 or if you are reading the count value directly.

Re: quadrature encoders pic18f2331

Posted: Tue Jun 21, 2011 3:28 pm
by Mathy
Hi,

Sorry for my very late response. I had some trouble with my board but I think it is OK now.

Im using the same method that is used in the example "Encoder2.fcf" a the top a the topic.

I think it's the interrup method but im not sure. I don't understand exactly what you mean.

I am completely lost. I am trying to count a large number of steps. Ideally in a 4 byte, or float variable to control the position of a DC motor.

Do you have an idea or an example based on the encoder2.fcf program ?

Thanks a lot for your precious help,

Mathy