Counter to track engine crankshaft angle

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Counter to track engine crankshaft angle

Post by PicoPuls »

Can thid be done in Flowcode7 ?

An engine has an incremental rotary encoder.
It gives 1024 pulses and one reset pulse per revolution

A Counter is needed to track engine crankshaft angle

The best solution would probably be to use the internal counter in PIC
As an example: The processor 16F1937 has a 16 bit counter.

The idea is to dedicate one pin to count pulses and one pin to reset the counter for each engine revolution.
Then the counter works alone without taking any processor power
The executed program will look at the counter and generate pulses for ignition and injection when the counter has reached
specified angles,

Is this possible ? Is this the right approach?
How is this specified in Flowcode 7
to initiate the counter and connect it to the two dedicated pins?

Is there a better choice than 16F1937 ?
I could even consider testing dsPIC or PIC32 as there will probably be many calculations and multiplications

Thanks and best regards
Bengt

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Counter to track engine crankshaft angle

Post by QMESAR »

Hi,

You can do this very easily in Flowcode however as you mentioned a dsPIC33EP device is the better choice as it has the following

(1) the processing power for the Engine calculations
(2) it has dedicated Rotary encoder modules which counts track pulses and also the direction its moving which is important in your application as when you get a wrong ignition point the tendency for the crankshaft is to slow down
and in very worst cases even want to turn direction
a good device is the dsPIC33EP with tthe MC extension which means motor control devises .

If it is a small engine you can even do this with a PIC16F16x device which has the angular timer module designed to handle such applications as you will see in the Apllication Note AN1980 how to use this timer and lookup tables to
reduce processing power and implement a CDI ignition on a 2 cyclinder engine

Hope this helps
Attachments
AN1980.pdf
(2.37 MiB) Downloaded 510 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Counter to track engine crankshaft angle

Post by PicoPuls »

Thanks for these ideas.

I obviously better use dsPIC,

I have dsPIC33 EP 512 MU810 at home with experimental board, but it has the MU extension.
Would that still be OK ?

If not,
An other alternative would be to buy dsPIC33 EP 512 MC 502-I/SP, SPDIP-28,
as it is DIP and easy to experiment with
Here I have no experimental board
The eBLOCK EB064 doesn't seem to support it
http://www.matrixtsl.com/resources/file ... 4-30-1.pdf
Any idea ?

Thanks and best regards
Bengt

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Counter to track engine crankshaft angle

Post by QMESAR »

The dsPIC33EPMU810 will work perfectly as it has 2 QEI interfaces however If you intend to make a product out of your development I will then use the MU810 for development and look for a MC device which is cheaper for production
becasue it is not a USB device with that said the code you developed on a MU810 will translate 1 to 1 to a MC device

:D
Attachments
12.jpg
12.jpg (50.75 KiB) Viewed 13424 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Counter to track engine crankshaft angle

Post by PicoPuls »

That's great news!

Where in Flowcode7 do I tell the compiler to dedicate 2 or 3 pins
for CountUp and Reset ? I have never done this before.

As you indicated, possible to use 2 CountUp signals with phase difference to tell direction = 3 pins .
(Makes everything better)

Thanks & Best regards
Bengt

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Counter to track engine crankshaft angle

Post by QMESAR »

Hi
In my opinion there are now two ways you can go about this pulse counting

(1) You can use the standard FC Encoder component located in Inputs menu
place it on one of the panels and then edit the pins for the Encoder inputs under the properties menu
this is the easy way as FC does all the coding for you however this is a SW encoder function

(2) This method you use the hardware QEI module of the chip which count and resets the counter as you have configured it including the pins for this you need either to make your own QEI component
or use a 2 x C function call one to initialize the module before the while loop and one during your code execution to read you the QEI counters all of this needs a bit of C coding from your side but it is also the most cost effective regarding cycles used to execute the Rotary encoder function ,you have to read the QEI interface chapter in the datasheet to understand how to configure and use the module

:D
Attachments
1.jpg
1.jpg (73.59 KiB) Viewed 13407 times
2.jpg
2.jpg (55.38 KiB) Viewed 13407 times
3.jpg
3.jpg (55.5 KiB) Viewed 13407 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Counter to track engine crankshaft angle

Post by PicoPuls »

Hi
Can any example code be found
exactly how to initialize QEI
and to understand the C code needed to read the QEI registers ?

There is a pdf describing QEI but it doesn't tell anything about C-programming
http://ww1.microchip.com/downloads/en/DeviceDoc/S15.pdf

There is a Programmer's reference manual, but it doesn't tell anything about QEI
http://ww1.microchip.com/downloads/en/D ... 70157F.pdf

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Counter to track engine crankshaft angle

Post by QMESAR »

Hi

You will have to read this AN note and use what is needed for your application with the QEI interface document you have
you will not get example code that fits exactly your application ,you need to learn and play with the QEI and then apply the knowledge to your application or use the
Standard FC 7 QEI component to learn to use it as mentioned before.

you can also search at the Microchip web site for example code

http://www.microchip.com/doclisting/cod ... yfunc.aspx

:D
Attachments
Calc Speed position with QEI.pdf
(287.36 KiB) Downloaded 540 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Counter to track engine crankshaft angle

Post by PicoPuls »

I am using Flowcode as I am no C programmer
Can anyone give me an idea how to make it without adding C-code ?

The basic problem seems quite simple. A trigger signal resets the counter and the Angle Signal counts up.
That's all- 2 pins needed

dsPIC33EP512MU810 has two QEI modules
and when you pick Input - Encoder Rotary and place it on 3D canvas
it suggests $PORTB.0 and $PORTB.1

So far so good, and everything seems defined.
Now I would expect to insert this into the block schematics without additional C-code
How ??

Best regards
Bengt
Attachments
kistler.jpg
kistler.jpg (12.66 KiB) Viewed 13319 times

AndrewC
Flowcode v5 User
Posts: 28
Joined: Wed Oct 02, 2013 12:16 pm
Has thanked: 7 times
Been thanked: 7 times
Contact:

Re: Counter to track engine crankshaft angle

Post by AndrewC »

I don't have FC7 but in 6 you have to select the "Component Macro" block. Once you have selected the block, opened it and selected the encoder option a sub menu will open up. Moving your pointer over these options explains there use. In FC6 my first choice would be to select "Enable". It's all pretty much self explanatory at least in FC6. And I'm not a programmer of any sort. I hope that helps :?

But FWIW, a couple of years ago I did a similar project in FC5. Instead I used a 60-2 tone wheel and a Hall effect sensor. Actually it was the wife's car although for testing purposes I had another chip to emulate the crank sensor output and a pot to control the simulated engine speed. I used a timer interrupt to time the changes in the hall effect. Then divide that time by 3 to calculate how long it took one degree to pass. Then use this calculation to predict, very closely, when the next degree of revolution occurs. At 3000 rpm it was accurate to +/- 1.5* (perfect at idle). I could of probably improve on that. Where the project got abandoned was when I had it firing a Mazda ignition coil at a predetermined selected angle but every now and then (quite regularly actually) it would reset the chip. I may return to it one day

Good luck with your project.

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Counter to track engine crankshaft angle

Post by PicoPuls »

Thanks for valuable info.

The two phase delayed count pulses should obviously be connected to B0 and B1

To the right in the image at the red arrow, you are dealing with hardware connections
totally outside the program flow (I guess).

But to the right; QuadEncoder1: ReadCounter etc... should and must be in the software loop

The problems as I see it is

1. ResetCounter is controlled by software.
It should be dedicated to a pin and hardware connected to the counter. Is that possible in FC7 ?
( I am working with dpPIC33EP512MU810 and it has 2 QEI )
I guess it is important for speed reasons to keep count-up*2 and reset outside of program flow

Best regards
Bengt
Attachments
QEI.png
(43.49 KiB) Downloaded 8835 times

AndrewC
Flowcode v5 User
Posts: 28
Joined: Wed Oct 02, 2013 12:16 pm
Has thanked: 7 times
Been thanked: 7 times
Contact:

Re: Counter to track engine crankshaft angle

Post by AndrewC »

To the right in the image at the red arrow, you are dealing with hardware connections
totally outside the program flow (I guess).
Yes. But they can be user selectable. Although if you are using an Interrupt On Change (IOC) your choice will be limited.
1. ResetCounter is controlled by software.
It should be dedicated to a pin and hardware connected to the counter
Again that has to be decided by the user. FC can't work out what resets the counter - temp, the time, or after so many pulses which is what you want. In your case I would use an encoder with an index - third output that occurs once per revolution. That's hooked up to another pin so when that occurs the program goes to a routine that then resets the counter.
Is that possible in FC7 ?
I don't think we have to worry about Flowcode's ability's. It is pretty good. I had it emulating "code" to enable start, by passing Ford's 'Smart Lock'. And I'm in novice class.

When I first started with Flowcode I would use someone else's program that vaguely represented what I wanted it to do. Chop parts out,change parts and see what happens. In the program here viewtopic.php?f=7&t=17117 you can see how I have reset the counter 'Zero Macro". Also Medelec has put in some helpful links.

Post Reply