PDA

View Full Version : Trying to measure TACH pulses.



eagleman
- 27th January 2020, 06:11
I'm using a PIC18F67K40 on PBP gold at 64Mhz and I am trying to measure "TACH" pulses on PORTB.0 that are being simulated from a signal generator. I'm trying three different methods just to see how things work. I am thinking that once I can be assured that the pulses are being recognized by PORTB.0 in any form or fashion then I can proceed with actual circuitry work out my permanent code to recognize and report true RPM. Problem is that I can't seem to get PORTB.0 (or PORTB.1, etc..) to recognize the signal from the signal generator.
Currently using 60-200 Hz at 5 volts from the signal generator in square wave ( with varying duty cycles, but I've tried sine, sawtooth, and various pulses types).

My test code is basically:

Do_Tach:
COUNT PORTB.0, 1000, RPM ' to count number of pulses in 1 second and store in word RPM
RPM = RPM * 60
serout LCD_out,6,[017,0,0," RPM: ", #RPM," "] ' print out to my LCD on line 0.
return
Do_Count:
COUNT PORTB.0, 1000, PCount' to count number of pulses in 1 second and store in word PCount
serout LCD_out,6,[017,0,1,"COUNT: ",#PCount, "cnts/sec "] ' print out on my LCD line 1
return
Do_Pulse:
PULSEIN PORTB.0, 1, TPulse ' read pulse on PORTB.0 and store result in word TPulse.
serout LCD_out,6,[017,0,2,"PULSE TIME: "] ' print out on my LCD line 2
return

Thus runs as a loop with a loop counter and the loop count is displayed on my LCD line 3 (code not shown)

I can see the generated signals at my board with my scope.
I can see the loops being counted on my LCD.
I get no response from PORTB.0 or PORTB.1

Before I build a new board I thought it would be best to check with the forum to see if I have missed something, done something stupid or whatever. I have not tried interrupts on this but can see where it might be needed for the PULSEIN statement.
I thought about doing an interrupt on a PORTB.0 change to read the system clock and compare it to the last interrupt time, but can't remember how to read the clock.

Open to any criticism that would help.

Ioannis
- 27th January 2020, 17:45
Maybe Timer module can help you on this?

The Tachometer pulses will be counted by a timer and the timer will be gated by a controlled time base.

This is the base of a frequency counter. And you can do other things while the timer counts pulses instead of waiting as is the case with COUNT command.

Ioannis

HenrikOlsson
- 28th January 2020, 06:39
What about the good old analog stuff?


ANSELB.0 = 0 ' Enable digital input circuitry to work properly on PortB,0

Ioannis
- 28th January 2020, 14:16
Maybe I do not see your point Henrik. How does this help?

Ioannis

Dave
- 28th January 2020, 15:23
Ioannis, The porta and portb default to digital input buffers DISABLED. They need to be set for digital operation before this type of use. Also I have to ask, Why are you counting pulses to get the RPM value? Seems like kind of a crude method. I would look at the time between leading edges and then compute the RPM value. Much faster...

Ioannis
- 28th January 2020, 16:01
Oh I see. I thought that was trivial and eagleman would have correct setup of PIC.

Ioannis

HenrikOlsson
- 28th January 2020, 16:39
Trivial yes, but trivial stuff can bite you plenty hard and since no setup code what so ever was posted and "Turn off analog functions" is the solution to 20%* of the questions posted here I thought it was worth a shot :-)

* I have no data to back that number up.

/Henrik.

Ioannis
- 28th January 2020, 21:20
You are absolutely right. Too many times it was the reason for erratic behavior.

Ioannis

mpgmike
- 30th January 2020, 13:41
eagleman, as of this reply, you show 2 posts. Let us get to know you a bit. How long have you worked with PICs? How about PBP specifically? Is this your 1st project, or is it your 17th?

Since you're using a K40 part, you can use MFINTOSC for TIMER1. You can either use an INT0 (or IOC) type interrupt on PORTB.0, triggered by your tach pulse, or even just use TMR1GIE. In the ISR, move your TMR1H_L to your variable Tach, then clear TMR1H_L for next round. You should disable TMR1 before reading, then re-enable after you read your values. Also be sure to clear whatever Interrupt Flag got you to the ISR (INTOIF or TMR1GIF).

The math for converting Timer pulses (Tach) to RPM is:

RPM = (Clock / Tach) * 60

In this case,

RPM = (500,000 / Tach) * 60

Right off the bat it becomes obvious that you will have to enable LONG to handle the math.

I prefer to guide people to self discovery as opposed to handing out answers. If this doesn't make sense to you, ask specific questions. Be sure to tell us what you have tried and the results you got. (You did an excellent job in Post #1).