Hi Terry,
Thanks, but I wouldn't call myself an expert on much beyond getting in trouble. At least, that's what my wife says..;o}
However, I have spent an hour or two messing with timers & hardware. Grab yourself an 18F2431, and give this a shot. It's really simple, but very precise.
I'm not really sure what the benefits of (sub hertz) measurements are going to give you. Oversampling A/D measurements is normally a good idea, so you can average measurements over time, but I really don't think this approach is going to give you any advantage when measuring a frequency. I mean, it's either X hZ or it's not.
Oversampling is just going to give you an average over time / the number of samples. If your frequency is changing, the result is garbage anyhow.
This is real simple. It sets up an 18F2431 to measure the time period between rising edges, which is the frequency.
Code:
DEFINE OSC 4
DEFINE DEBUG_REG PORTC
DEFINE DEBUG_BIT 6
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true
T1 VAR WORD
Capture VAR PIR3.1
False CON 0
True CON 1
' auto time base reset, frequency mode, capture on every rising edge
Frequency CON %01000101
ANSEL0=0 ' All digital
TRISA.2 = 1 ' this is your (CAP1) input pin measuring the frequency
INTCON = 0 ' Interrupts off
TMR5H = 0 ' Clear high byte of TMR5 counter
TMR5L = 0 ' Clear low byte
T5CON = %00000001 ' prescale=1:1, int clock, TMR5=on
CAP1CON = Frequency ' we're measuring a frequency
DEBUG "START",13,10
Main:
Capture = False ' Reset capture flag
GOSUB Freq ' get frequency
PAUSE 500
GOTO Main
Freq:
' Frequency measurement mode, capture Timer5 count every rising edge
' with auto reset of Timer5 on each event
WHILE Capture = False
WEND
T1.HighByte = CAP1BUFH
T1.LowByte = CAP1BUFL
DEBUG "Freq = 1/",DEC T1,"uS",13,10
RETURN
END
With another PIC programmed to output a 4kHz signal into the 18F2431 CAP1 pin, this returns;
Freq = 1/250uS
Freq = 1/250uS
Freq = 1/250uS
1/250uS = 4kHz.
The 18F2431 has a lot of nifty features that can save you a boat-load of code & time.
It's definitely worth looking into. This captures the time between rising edges + resets the timer after each capture event. Very handy stuff...;o}
Bookmarks