Counter by Jmarkwolf

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
ideasman
Posts: 50
Joined: 01 Mar 2009 23:10

Counter by Jmarkwolf

#1 Post by ideasman » 24 Apr 2009 01:39

Can someone help? I Picked up this code from a thread from Jmarkwolf.

The code compiles fine, I am using a Funtion generator from 100 to 200 hz and every time I go up to 200 then down to 100hz I get different readings all the time. Can someone point out how to make this code consistent. Also how come Mikroe has no compiler shortcuts for Counters that would be a nice feature.
I have tried adding the MSB and LSB but is acts like a random number generator I cant get a proper timed result.
Im using a Pic30F4013 @ 80MHZ.
Anyone's thoughts much appreciated.
P.S. How is the magic number derived.

Cheers

Ideasman

'******************************************************************************
'
' This program configures the TMR2 & TMR3 timer pairs as a 32-bit timer
' for implementing a simple polled tachometer, implemented with a Hall effect
' switch or opto-sensor. The counts between magnets on the shaft is collected
' in the 32-bit timer. This value is divided into another 32-bit
' "magic number" to derive RPM.
'
' RPM = magic number / counts-between-magnets
'
' magic number = RPM * counts-between-magnets
'
'******************************************************************************
'
program Tachometer_Test
'
'******************************************************************************
'
dim x, i, ch as byte
dim msg1 as string[30]
dim CBM as longint 'CBM = Counts Between Magnets
dim RPM as word
dim Small_Half as word 'lower word of 32-bit timer
dim Big_Half as word 'upper word of 32-bit timer
const MagicNumber as LongInt = 299999960 '
'
'The dsPIC30F4013 device contains 5 timers, T1 - T5.
'
'T1 is generally regarded as a 16-bit TypeA timer (limited
'functionality).
'
'T2 & T4 timers are regarded as TypeB timers which means they can
'be concatenated, with T3 & T5 respectively, as 32-bit timers, among
'other things, in which case the T2 & T4 config bits predominate and
'the T3 & T5 config bits are ignored.
'
'T3 & T5 timers are regarded as a TypeC timer pair with similar
'functionality as T2 & T4, as well as being able to trigger an A/D
'event. In 32-bit mode, the config bits associated with these timers
'are ignored.
'
'There are numerous methods to implement a tachometer and this is only
'one.
'
'******************************************************************************
'
' Hardware Uart routine
'
'******************************************************************************
'
sub procedure Print_To_Port
Uart1_Write_text(msg1) '
end sub
'
'******************************************************************************
'
' main
'
'******************************************************************************
'
main:
'
T1CON = 0x0000
T2CON = 0x0008 'T2 = Off, Fosc/4, T2/3 = 32-bits, 1:1 prescale
T3CON = 0x0000 'Note: T3CON config bits are ignored. T2CON
'bits are used for 32-bit control.
'For 32-bit control Timer3 is the MSword,
'Timer2 is the LSword.
T4CON = 0x0000
T5CON = 0x0000
'
ADPCFG = 0xFFFF '
'
TRISA = $0800 '
TRISB = $02e2 '
TRISC = $4010 '
TRISD = $0302 '
TRISF = $0005 '
'
Uart1_Init(9600) '
'
u1mode.10 = 1 'specify alternate uart pin pairs (altio bit)
'
'******************************************************************************
'
' Start
'
'******************************************************************************
'
Start:
ClearBit(t2con,15) 'stop timer
'
TMR3HLD = 0 'clear timers here (in exactly this sequence)
TMR2 = 0
'
CBM = 0 're-init variables
RPM = 0
'
loop_here:
if portA.11 = 0 then goto loop_here 'loop tight until rising edge
end if
'
' Pin = high here
'
SetBit(t2con,15) 'start timer
'
' Pin might still be high here
'
loop_here2:
if portA.11 = 1 then goto loop_here2 'loop tight while pin still high
end if
'
' Pin is low here
'
loop_here3:
if portA.11 = 0 then goto loop_here3 'loop tight while pin is low
end if
'
' Pin is high again here
'
ClearBit(t2con,15) 'stop timer
'
Small_Half = TMR2 'Read timers in exactly this sequence (TMR2 first)
Big_Half = TMR3HLD
'
CBM = Big_Half * 65535 + Small_Half
'
RPM = MagicNumber / CBM 'calculate RPM
'
msg1 = "RPM = "
Uart1_Write_text(msg1) 'write string out the usart
'
WordToStr(RPM,Msg1)
Uart1_Write_text(msg1) '
'
Uart1_Write_char(13)
Uart1_Write_char(10)
'
delay_ms(1000)
'
goto Start 'start over
'
end.

Skyline
Posts: 267
Joined: 10 Jan 2006 09:35

#2 Post by Skyline » 24 Apr 2009 14:54

Hi,

I compiled and tested the posted code, works fine. If the input clock signal is 100Hz, the output is "RPM = 1500". If the input signal is changed to 200Hz, the output is "RPM = 3000". When the input clock drops back to 100Hz, the output also drops back to "RPM = 1500". The output readings are exactly consistent with the input signals when I move the input clock up and down.

Are you using a function generator that linearly sweeps up and down from 100Hz through 200Hz?

The 100Hz signal is 100 cycles per second, or 6000 cycles per minute. The magic formula translate 6000 cycles per minute to 1500 rpm. This formula implies that there are 4 tacho pulses per revolution for this specific tacho implementation.

ideasman
Posts: 50
Joined: 01 Mar 2009 23:10

Thanks that cleared it up

#3 Post by ideasman » 25 Apr 2009 07:30

Thanks for clearing that up.
Yes 4 counts a second works perfect, would you happen to know how to get it to one pulse a second. What I mean is what is the formula for the magic number how was it derived.
I know you can divide CBM by 4 but I would like to use the known formula for this code.

Thanks ALOT

Ideasman

Skyline
Posts: 267
Joined: 10 Jan 2006 09:35

#4 Post by Skyline » 25 Apr 2009 16:32

Let's see.

Assume one pulse per second. Then the variable "Counts Between Magnets" is 20,000,000, because the 80MHz 4013 counts at 80/4 = 20MHz.

If you have one magnet on the flywheel, one pulse per second = 60 rpm.

60 rpm = 20,000,000 counts,
30 rpm = 40,000,000 counts,
10 rpm = 120,000,000 counts,
1 rpm = 1,200,000,000 counts.

So the "magic" number to convert counts to rpm is 1,200,000,000.

In the previous example, there are 4 magnets on the flywheel. Therefore
1 rpm = 300,000,000 counts, hence the magic number is 300,000,000.

Do remember,
'There are numerous methods to implement a tachometer and this is only
'one.

ideasman
Posts: 50
Joined: 01 Mar 2009 23:10

Many Thanks

#5 Post by ideasman » 27 Apr 2009 05:22

Thanks for your reply skyline every thing is now working just fine.
Code works very well.
Cheers

Ideasman

jmarkwolf
Posts: 89
Joined: 01 Apr 2007 15:45
Location: Southeast Michigan
Contact:

#6 Post by jmarkwolf » 08 May 2009 12:57

Glad to see someone finding my tachometer code of interest.

I should've mentioned in the comments that my "magic number" was specified for a shaft with 4 magnets on it. So, time between magnets is relative to 1/4 revolution.

Bear in mind, that this tachometer will over flow the counters if the shaft is running slow or stopped.

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”