Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Tip for PIC tacho

Status
Not open for further replies.

Mosaic

Well-Known Member
Hi all:
Thought I'd share this:

I built a Pic tacho using the counter feature of timer0 (16f886). Basically it would calc counts/pulses over a 20 ms ISR and then calc the RPM from that. All very straightforward.

However, the tacho needs to accept 5V or 12v signals plus direct from the coil -ve primary (perhaps 150V).

Well I used a 4n25 optoisolator fed by the following sequence , pulse signal goes into a pair of forward biased 1n4001 diodes then a 390 ohm resistor then to the opto +ve in. Include a .22uf cap to gnd at the junction of the 2nd diode and the 390 ohm and include a .01uf cap to gnd at the input of the opto. The raw signal pulse is fed into the first 1n4001.

That's all good for 3-5VDC or 12VDC pulses from a Hall sensor or ECU. In order to handle a 150V pulse all u need to do is add a 10K resistor in series with the 1n4001 input and you are done. All tested and stable!.

The ringing extra counts are absorbed by the caps and diode voltage drop. This is suitable for an Inductive coil, not a capacitive discharge coil.
 
Hi Mosiac

Are you measuring incoming pulses for a 20mS time & multiply by 300 to get RPM?

I’m wondering how you counting pulses? In the ISR?
Is this how you do!!

*Enable TMR2 to generate 20mS time.
*configure TMR0 as a counter by T0CS bit
*when signal input present it will generate T0IF
*count T0IF counts on 20mS period
*multiply by 300 to get RPM
 
Gayan , that's about it. I use RA4, t0ck. U don't check the flags, I just check & reset the Timer0 count every 200ms not 20ms...typo.. My ISR is actually a 1 ms ISR with a 10ms Edge triggered counter. Then multiply by the appropriate factor based on the # of cylinders or if u are sampling pulses from a single plug etc. I have a 1ms ISR cuz I am also using it for soft PWM to drive electronic boost control.

In the case of a 6 cylinder I alter the sampling period to 300ms to keep the calcs simple integer calcs - same as the 4 cylinder calc. 2 coil pulses per rev. So I update the digital display about 3 times per sec.

To do bench top testing I setup a 12VDC coil relay that has a N.C. SPDT contact and run the current to the coil via the N.C. contact so the relay causes it's own 'chatter'. That came out at around 530 Hz of 150Vdc spikes on the coil back EMF. When I set the tacho to 8 cylinder mode I found it accurately displayed an RPM of 530 * 60/4 = about 8000 rpm. I used simple integer calcs accurate to 100rpm.

The oscilloscope trace showed the usual spike & ringing but the tacho was stable and accurate.

Here's a bit of the commented code in the ISR.
Code:
incf Tachperiod,f ;this routine counts pulses on ra4,t0ck, to calc rpm.
	movf Tachperiod,w;                                                                         1 Tachperiod = 10msec, so 20 = 200mSec
; handle 6cylnders setting
	btfsc RPM6cyl,0;                                                                             skip if 6cylinders not selected, else
	addlw .10;                                                                                   makes next sublw .30 seem like a sublw .20 for a net 200 msec period.
	sublw .30
	skpz
	goto ISR1 ; 
;this defaults to a 4CYL RPM reading, .30 sec updates.
	
	clrf Tachperiod;
	movf TMR0,w ;                                                                               load undivided timer0
	movwf Timer0 ;                                                                              capture timer 0  into gpr.
	bcf STATUS,C
;check for 8cyl setting (1:2)
	btfsc RPM8cyl,1 ;                                                                            if set then
	rrf Timer0,f ;                                                                               div by 2 , else
	clrf TMR0 ;                                                                                    reset Timer0
	bcf STATUS,C
;check for ecu signal (1:1) setting
	btfsc RPMecu,3
	rlf Timer0,f;                                                                                    X 2 for  ECU signal
;check for Coil on plug (4:1) setting
	btfsc RPMplug,4
	rlf Timer0,f; x 2
	bcf STATUS,C
	btfsc RPMplug,4
	rlf Timer0,f;                                                                                   x4 for RPM from plug pack
 
Last edited:
Hi Mosaic thanks for your details.

Just forget the automotive industry cuz I do not anything related to that field.

Just think as a simple tachometer.
Ah now I see you counting incoming pulses over 200mS time. So the TMR0 maximum count is 255.So the maximum RPM is 255 X 5 X 60 = 76500 RPM. Never seen a motor like this speed :D most motors RPM is 2500 - 4500RPM.So the TMR0 never overflow during this 200mS time.

My problem is, the minimum RPM is having an offset of 300RPM.
If TMR0 count is “1” during 200mS time then it will show 300 RPM. (1 X 5 X 60 = 300)

To calculate below 300RPM is a problem.

The correct way is to measure the time between pulses.
 
Last edited:
Yes, for a static application of fixed rpm a longer sampling, or sampling between pulses, would give better results. U see with a 4 cylinder car, 1 rev is 2 pulses and in an 8 cylinder, 1 rev is 4 pulses so that's where the resolution comes from. Where 1 rev is 1 pulse the resolution drops. To compensate for that simply increase your sampling period and alter the arithmetic.

For my app I need rapid updates so the sampling needs to be shorter. Plus I have everchanging rpms so sampling over a fixed time period gives a better average than a single snapshot between two pulses. But this whole thread was about the interface to an auto tacho which often is the trickiest to deal with given the number of signal types. 5V logic, 12V logic, Reluctor 5-50V, Hall effect 3-5V, Coil backemf >100V etc.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top