This has taken my interest really, and I've come up with some code to use 4 input pins on an 08M and display the frequency in Hz up to 65,535,000 Hz (ie 65 MHz). It will display the frequency with commas and blanking leading zeroes.
The use of 4 inputs from the 4040 allows the frequency measurement to be maximised.
It is UNTESTED, but just compiles for an 08M.
And plenty of symbols !
Pity the PICAXE Projects website isn't functioning yet ;-)
<code><pre><font size=2 face='Courier'>
#rem
##########################################################
# #
# PICAXE 08M FREQUENCY COUNTER #
# #
# Code Version :1 #
# Status :Untested #
# Date
ecember 2006 #
# PICAXE Type :08M #
# Software Version :5.06 #
# #
# Author :Jeremy Leach #
# #
##########################################################
LCD DISPLAY:
------------
.---..---..---..---..---..---..---..---..---..---..---..---.
| 9 || 9 || , || 9 || 9 || 9 || , || 9 || 9 || 9 || H || Z |
'---''---''---''---''---''---''---''---''---''---''---''---'
Digit: 0 1 2 3 4 5 6 7
CIRCUIT DIAGRAM:
----------------
.----------.
.--(/4096)--|Q12 +V|--
| (/64)--|Q6 Q11|--(/2048)
| (/32)--|Q5 Q10|--(/1024)
| (/128)--|Q7 Q8|--(/256)
| .- (/16)--|Q4 Q9|--(/512)---------.
| | (/8)--|Q3 Reset|-- |
| | (/4)--|Q2 /Clk|-- |
| | --|0v Q1|--(/2) ---. |
| | '----------' | |
| | 4040 | |
| | | |
| | | |
| | | |
| | .----------. | |
| | --|+V 0V|-- | |
| | --|Serin Out0|-- | |
| '---------|In4 In1|----------' |
'-----------|In3 In2|-----------------'
'----------'
PICAXE 08M
DESCRIPTION:
------------
This frequency counter is capable of reading and displaying a frequency in
the range 0 to 65MHz. However this is relying on the accuracy of the internal
osciallor.
The circuit uses a 4040 binary ripple counter to divide down the
input frequency to a level that the PICAXE08 can read. Four separate
inputs are used on the PICAXE08M connected to the 4040 /2,/16,/512 and
/4096 outputs. This allows the PICAXE08M to sample, using the Count
command, using the input that will give the best accuracy.
The slection of the input pin to use is done automatically. A quick initial
'sample is taken using the highest division output of the 4040. This gives
'a rough frequency which is used to select the input pin for the main sample.
Careful maths is used for each input pin to maximise accuracy.
The frequency value is displayed to an LCD in Hz. Blanking of leading
zeroes is performed.
CALCULATION DETAIL:
-------------------
The Count command can sample at a maximum input frequency of 50KHz with the
PICAXE running at 8MHz. So the maximum signal frequency that can be measured
at each input is:
MEANING OF
INPUT MAX READABLE SIGNAL FREQUENCY (MRSF) FrequencyW = 65535
/2 50KHz * 2 = 100,000 Hz 65,535 Hz
/16 50KHz * 16 = 800,000 Hz 655,350 Hz
/512 50KHz * 512 = 25,600,000 Hz 6,553,500 Hz
/4096 50KHz * 4096 = 204,800,000 Hz 65,535,000 Hz
The right-most column shows the meaning of a FrequencyW value of 65535
when obtained from the calculations for each input.
Note though that the maximum frequency the 4040 chip (HC type) can read
is 90MHz, and the code puts a convenient cap of 65.535 MHz on the top
frequency that the counter can read.
#endrem
'#########################################################
'# VARIABLES #
'#########################################################
'Word0 (b0 and b1)
Symbol DisplayStarted = bit0
Symbol ASCIIChr = b1
'Word1 (b2 and b3)
'Word2 (b4 and b5)
Symbol FrequencyW = w2
'Word3 (b6 and b7)
Symbol WRemainder = w3
'Word4 (b8 and b9)
Symbol DivisorW = w4
'Word5 (b10 and b11)
Symbol CountW = w5
'Word6 (b12 and b13)
Symbol WholeW = w6
Symbol Digit = b12
Symbol StartDigit = b13
'#########################################################
'# CONSTANTS #
'#########################################################
'Miscellaneous
Symbol Sample1000ms = 2000 'at 8MHz. Tune this value to calibrate
'the frequency counter against a known
'source.
Symbol Sample100ms = 200 'at 8MHz
Symbol Yes = 1
Symbol No = 0
'Pin assignments
Symbol Div2InputPin = 1
Symbol Div16InputPin = 4
Symbol Div512InputPin = 2
Symbol Div4096InputPin = 3
Symbol LCDOutPin = 0
'#########################################################
'# INITIALISE #
'#########################################################
SetFreq M8 'To maximise accuracy
Pause 1000 'half a second at 8MHz. For the AXE033 LCD to initialise.
'Write "Hz" at the end of line 1 on the LCD.
Serout LCDOutPin,N1200,(254,136,"Hz"
'#########################################################
'# MAIN PROGRAM #
'#########################################################
Main:
Gosub GetFrequency
Gosub DisplayFrequency
Goto Main
'#########################################################
'# SUBROUTINES #
'#########################################################
GetFrequency:
'Take an initial sample and do a rough calculation of the frequency. Use
'this result to select the best input pin to use for a more accurate
'one-second sample.
'ON EXIT: FrequencyW holds the frequency value. However it has different
'meaning depending on the Input pin used.
'Take an initial quick sample in one tenth of a second using the
'/4096 input.
Count Div4096InputPin,Sample100ms,CountW
'Calculate a rough input frequency, using the formula
'Freq = (Count * 10 * 4096)/1000.'Here, a frequencyW value of 65535
'means 65.535 MHz.
'As this is a rough calculation then we can approximate this formula to
'be: Freq = Count * 41. This also raises the calculated frequency slightly
'which is helpful for cases which lie on the threshold between which input
'is 'best'.
FrequencyW = CountW * 41
'Use this rough frequency to decide which input is best to read
'the main sample. 'Best' meaning where the input signal frequency is
'just within the input's Maximum Readable Signal Frequency (MRSF).
Select Case FrequencyW
Case >6553 '65,530,000 Hz
'ActiveInput = Div4096InputPin
Count Div4096InputPin,Sample1000ms,CountW
'Calculate frequency value using formula:
'Freq = (Count * 4096)/1000.
WholeW = CountW / 125
WRemainder = CountW // 125
FrequencyW = WholeW * 512
FrequencyW = WRemainder * 512 / 125 + FrequencyW
StartDigit = 0
Case >655 '6,500,000 Hz
'ActiveInput = Div512InputPin
Count Div512InputPin,Sample1000ms,CountW
'Calculate frequency value using formula:
'Freq = (Count * 512)/100.
WholeW = CountW / 25
WRemainder = CountW // 25
FrequencyW = WholeW * 128
FrequencyW = WRemainder * 128 / 25 + FrequencyW
StartDigit = 1
Case >65 '65,000 Hz
'ActiveInput = Div16InputPin
Count Div16InputPin,Sample1000ms,CountW
'Calculate frequency value using formula:
'Freq = (Count * 16)/10.
WholeW = CountW / 5
WRemainder = CountW // 5
FrequencyW = WholeW * 8
FrequencyW = WRemainder * 8 / 5 + FrequencyW
StartDigit = 2
Else '<= 65,000 Hz
'ActiveInput = Div2InputPin
Count Div2InputPin,Sample1000ms,CountW
'Calculate frequency value using formula:
'Freq = Count * 2.
FrequencyW = CountW * 2
StartDigit = 3
EndSelect
Return
DisplayFrequency:
'Display the frequency on the LCD, adding in commas and blanking leading
'Zeroes.
'Initialise display variables
WRemainder = FrequencyW
DisplayStarted = No
DivisorW = 10000
'Move to the start of line1
Serout LCDOutPin,N1200,(254,128)
'Display the frequency value
For Digit = 0 To 7
If Digit >= StartDigit Then
ASCIIChr = WRemainder / DivisorW + "0"
WRemainder = WRemainder // DivisorW
'If the value is >0 then start the display of digits
If ASCIIChr >"0" Then
DisplayStarted = Yes
EndIf
DivisorW = DivisorW/10
Endif
'Blank the digit if the Display hasn't started
If DisplayStarted = No Then
ASCIIChr = " "
Endif
Gosub WriteChrToLCD 'Write the digit
'If the digit is 1 or 4 then need to write a comma, but
'only if the display has started.
If Digit <> 1 And Digit <> 4 Then GADF_1
ASCIIChr = DisplayStarted * 12 + " "
Gosub WriteChrToLCD
GADF_1:
Next
Return
WriteChrToLCD:
'Writes the value of ASCIIChr to the LCD.
Serout LCDOutPin,N1200,(ASCIIChr)
Return
</font></pre></code>