quadrature encoders pic18f2331

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
arnaud2
Posts: 9
Joined: Wed May 16, 2007 7:00 pm
Contact:

quadrature encoders pic18f2331

Post 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)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

arnaud2
Posts: 9
Joined: Wed May 16, 2007 7:00 pm
Contact:

Re: quadrature encoders pic18f2331

Post by arnaud2 »

i dont know how to programming in c
have you a example to use this function
thanks

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: quadrature encoders pic18f2331

Post 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 1058 times

arnaud2
Posts: 9
Joined: Wed May 16, 2007 7:00 pm
Contact:

Re: quadrature encoders pic18f2331

Post 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 ?

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

arnaud2
Posts: 9
Joined: Wed May 16, 2007 7:00 pm
Contact:

Re: quadrature encoders pic18f2331

Post 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

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

karthick
Posts: 1
Joined: Sat May 10, 2008 9:47 pm
Contact:

quadrature encoders pic18f4331

Post 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.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: quadrature encoders pic18f2331

Post 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 891 times

Mathy
Posts: 333
Joined: Mon Oct 05, 2009 2:39 pm
Has thanked: 30 times
Been thanked: 33 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: quadrature encoders pic18f2331

Post 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;

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

Mathy
Posts: 333
Joined: Mon Oct 05, 2009 2:39 pm
Has thanked: 30 times
Been thanked: 33 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

Mathy
Posts: 333
Joined: Mon Oct 05, 2009 2:39 pm
Has thanked: 30 times
Been thanked: 33 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

Mathy
Posts: 333
Joined: Mon Oct 05, 2009 2:39 pm
Has thanked: 30 times
Been thanked: 33 times
Contact:

Re: quadrature encoders pic18f2331

Post by Mathy »

Hello :)

Any ideas ?

Mathy.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: quadrature encoders pic18f2331

Post 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.

Mathy
Posts: 333
Joined: Mon Oct 05, 2009 2:39 pm
Has thanked: 30 times
Been thanked: 33 times
Contact:

Re: quadrature encoders pic18f2331

Post 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

Post Reply