PDA

View Full Version : Audio Frequency Counter



mister_e
- 28th December 2004, 01:34
Hi, here's a simple code based on a PIC16F84A-20/P. Can be use as a audio frequency counter. It show the frequency on 3 x 7 segments display. It also place the decimal point.

have fun !

mister_e
- 7th February 2005, 18:58
And those who want also the schematic + code package, see this link to the Melabs Website. Choose Counter.zip

http://www.melabs.com/resources/samples.htm#submitted

sanchit04421
- 23rd June 2007, 10:02
hi.
i m new here
i wanted t know if i could get a pic program code for frequency counter which can measure upto 1mhz.
is it available for download on net??
i need it urgent for my project! thanks

charudatt
- 27th July 2009, 19:58
Hello Steve,

I have run into some problem using your code for a 16F628A with a 16x2 LCD.

I am basically making a audio frequency counter, which would then be used to control a set of 8 LED's depending upon the Input frequency on PORTB.

Keeping this in mind I tried to output the frequency on the LCD but I get jumping figures at high speed. I believe , I have to introduce a delay between the display. Secondly , connecting 10K to ground on Pin#3 with 1K in series , as your circuit says, I don't get any reading. Which means , I need some impedence matching or may be a signal conditioner.

My audio source is from my desktop computer , audio out.

my code so far -


@ __CONFIG _HS_OSC & _MCLRE_OFF & _LVP_OFF & _WDT_ON & _PWRTE_ON & _BODEN_ON
DEFINE OSC 20

;76543210
TRISB = %00000000
TRISA = %11110000

DEFINE LCD_DREG PORTB 'Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 0 'Define first pin of portb connected to LCD DB4
DEFINE LCD_RSREG PORTB 'Define PIC port used for RS line of LCD
DEFINE LCD_RSBIT 5 'Define Portb pin used for RS connection
DEFINE LCD_EREG PORTB 'Define PIC prot used for E line of LCD
DEFINE LCD_EBIT 4 'Define PortB pin used for E connection
DEFINE LCD_BITS 4 'Define the 4 bit communication mode to LCD
DEFINE LCD_LINES 2 'Define using a 2 line LCD
'DEFINE LCD_COMMANDUS 2000 'Define delay between sending LCD commands
'DEFINE LCD_DATAUS 50 'Define delay time between data sent.


' Interrupt and register definition
' ---------------------------------
'
OPTION_REG = %1111000 ' TMR0 clock source : RA4/T0CKI
' increment on low to high transition
' Prescaler assign to WDT
' WDT rate 1:1
'
INTCON = %10100000 ' Enable global interrupt
' Disable EE write interrupt
' Enable TMR0 overflow interrupt
CMCON = 7 ' disable analog comparator
VRCON = 0 'turns off Vref for min current


' Variable definition
' -------------------
'
DisplayPort var PORTB ' Port for 7 Segments
ClockInput var PORTA.4 ' Input pin for signal
_7Seg1 con 14 ' enable more significant 7 segment display
_7Seg2 con 13 ' enable mid significant 7 segment display
_7Seg3 con 11 ' enable less significant 7 segment display
Digit_1 var byte ' Hundreds digit
Digit_2 var byte ' Tenth digit
Digit_3 var byte ' Unit digit
ToBeDisplay var word ' Result of count to be send to 7 segment display
Display var byte ' Temp variable
DisplayLoop var byte '
Delay var word ' Variable for Delay loop
OverFlowVar var word '
Thousands var bit ' Flag for count >= 1000 & < 10 000
TenThousands var bit ' Flag for count >= 10 000

' Variable and software initialisation
' ------------------------------------
'
tobedisplay = 0 ' set initial value of count
TMR0 = 0 ' reset prescaller
on interrupt goto SetVarToBeDisplay

MainLoop:

' MainLoop
' ---------
'
' 1. display the result of the count on RA4 pin
' 2. refresh display
' 3. reset Timer0
' 4. reload prescaler.
'
' Duration of the procedure : 1 sec
' fine tuned by DelayBetweenEachDisplay Sub
'
' Looping 1 sec and get results of the pulse count in
' TMR0 + OverFlowVar
'
DisplayRefresh:
'
' Testing amount of count
' -----------------------
'
' Get the result of count and place decimal point flag
' on the according 7 segments
'
If tobedisplay>=1000 then
tobedisplay=tobedisplay/10
if tobedisplay>=1000 then
tobedisplay=tobedisplay/10
Thousands=0
TenThousands=1
else
TenThousands=0
thousands=1
endif
else
thousands=0
tenthousands=0
endif

LCDOut $FE,$C0, "COUNT: ",DEC tobedisplay , " "
gosub DelayBetweenEachDigit
tobedisplay = OverFlowVar + TMR0
OverFlowVar = 0 ' Reset OverFlowVar
TMR0 = 0 ' reset prescaller

goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh


DelayBetweenEachDigit:

' DelayBetweenEachDigit
' ---------------------
' Produce delay of about 3 mSec
'
' Fine tuned with MPLAB StopWatch to get MainLoop = 1 sec
'
for delay=1 to 307
@ nop
next
@ nop
@ nop
@ nop
@ nop
@ nop
@ nop
@ nop
return


disable
SetVarToBeDisplay:
'
' SetVarToBeDisplay
' -----------------
' interrupt routine of TMR0 overflow
'
' Reset prescaller
' Reset overflow flag
'
OverFlowVar = OverFlowVar + 256
INTCON.2 = 0 ' clear overflow flag
TMR0 = 0 ' reload TMR0
resume
enable

Archangel
- 28th July 2009, 08:24
but I get jumping figures at high speed.

Hi charudatt,
Here is an Idea to help with the jitters, after the LCDOUT, save tobedisplay var to a dummy var, and then before updating the display check to see if tobedisplay = dummy and if it does, go back to the loop until it changes.
Also, what's that about?


goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh
goto DisplayRefresh

charudatt
- 28th July 2009, 08:43
I thought I got my bearing all messed up. My project is basically a Sound to Light idea (Disco light), and I thought , it was done by sampling the frequency and hence , I went about trying a Audio frequency meter.

There was a error in the code, I wanted to introduce a slight delay in the LCD routine and i put in those lines to goto DisplayRefresh, actually it should have been Gosub DisplayRefresh.

Anyway, any pointer / idea for disco lights using PIC.

Thanks all.

regards

Archangel
- 29th July 2009, 06:31
I built one (actually several) back around 1971 in high school electric shop, used an audio transformer secondary to speaker and primary to the gates of 3 SCRs through 3 R/C filters for 3 channel outputs to control 3 strings of colored lights put behind diffuser panels made for fluorescent lights. Wow shades, some Metal music, some get high, and who could even think of going to war ?
Back on topic . . . R/C network, Analog voltage detection, digital output . . . same theory should work.

charudatt
- 29th July 2009, 07:28
OK , did you mean this http://4.bp.blogspot.com/_BQac7gcHPK0/RcAeca-ghrI/AAAAAAAAADk/08fmP-keI3U/s1600/color.gif

Anyway , I got a lot of dope on this forum relating to this topic and shall give it a try.

Thank you for the help.

regards

Archangel
- 29th July 2009, 08:25
OK , did you mean this http://4.bp.blogspot.com/_BQac7gcHPK0/RcAeca-ghrI/AAAAAAAAADk/08fmP-keI3U/s1600/color.gif

Anyway , I got a lot of dope on this forum relating to this topic and shall give it a try.

Thank you for the help.

regardshttp://4.bp.blogspot.com/_BQac7gcHPK0/RcAeca-ghrI/AAAAAAAAADk/08fmP-keI3U/s1600/
Yes, that was it.