Cool Walter.
All is well (± 0.1Hz)...
154.3Hz
154.3Hz
154.3Hz
154.3Hz
154.3Hz
154.3Hz
154.2Hz
154.2Hz
154.3Hz
154.3Hz
154.3Hz
154.3Hz
So, for anyone who may find this thread via a search, to summarize here's the code to frequency detect audio signals fed into pin 7 of a 16f690 (using the PIC's internal comparator to generate interrupts, which are in turn 'timed' successively)....
Code:
@ __CONFIG _FCMEN_OFF & _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF
'***********************************************************************************************
'16F690 Pin Connections....
' PIN# NAME USE & CONNECTION
' 1 Vdd +5VDC power supply
' 7 RC3 C12IN3- (external comparator input)
'10 RB7 HSEROUT Pin (fed into Pickit2's pin 4 to display data onsreen via the Pickit2's UART tool)
'19 RA0 Ext VREF for Comparator1
' 20 Vss Ground
'*************************************************************************************
DEFINE OSC 4 ' set Oscillator at 4Mhz.
DEFINE NO_CLRWDT 1 ' PBP doesn't clear WDT automatically
DEFINE HSER_SPBRG 25 'HSEROUT Stuff.
DEFINE HSER_TXSTA 24h 'HSEROUT Stuff.
DEFINE HSER_CLROERR 1 'HSEROUT Stuff.
txsta = %10100100 'setup the tx register
RCSTA.7 = 1 ' Enable RB7 for TX USART
INTCON.0 = 0 ' clears the RABIF Flag (to 0), COULD be 1 on reset (unique to F690)
ANSEL = 0 'disable AtoD.
ANSELH = 0 'disable AtoD.
CM2CON0 = 0 'turn off comparator 2.
'Turn on & Set up Comparator 1
CM1CON0 = %11100011 'Comparator ext op pin disabled (op of comparator avaible internally only), compare against external VREF
MyTime var word ' used to amalgamate TMR1 High & Low Bytes.
Frequency var word 'used to convert the 'count' to frequency.
' the following is pretty much a straight lift from the compiler manual (DIV32 section)
a Var Word
b Var Word
c Var Word
dummy Var Word
'these two below will later set the dummy variable to total 5,000,000 in the interrupt handler for DIV32 to use
b = 5000
c = 1000
MyTime = 0 'clear down Mytime, prior to starting.
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System PO90OOO9
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler CMP1_INT, _Comp1_Int, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
T1CON = $11 ; Prescaler setting - this one means 2us between successive clocks
@ INT_ENABLE CMP1_INT ; enable Comparator 1 interrupts
T1CON.0=0 'stop the timer
TMR1H = 0 'Set the high part of the timer value to 0
TMR1L = 0 'Set the low part of the timer value to 0
'Main body of (dummy) Code*********************************************************************************************
Main:
pause 10
goto Main
end
'Comparator1 Interrupt Handler+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Comp1_Int:
if T1CON.0= 0 then 'if timer1 is not running...
TMR1H = 0 'Set the high part of the timer value to 0
TMR1L = 0 'Set the low part of the timer value to 0
T1CON.0= 1 'start timer
else 'therefore if it is running, stop the timer & calculate the number of 'clock' counts between comparator interrupts....
T1CON.0= 0 'stop the timer
MyTime.Lowbyte = TMR1L 'puts the timer's low byte in MyTime's lower 8 bits
MyTime.Highbyte = TMR1H 'puts the timer's high byte in MyTime's upper 8 bits
dummy = b * c '5,000,000
frequency = div32 mytime ' this convertes to Hz, but with no decimal points
HSEROUT [dec frequency/10,".",dec frequency//10,"Hz",13, 10] .....this places a decimal point in the right place to make the reading easier on the eye.
endif
@ INT_RETURN
Note: for the comparator to have something to erhm 'compare' against, I've still not got the PIC's internal voltage reference to work yet, so I was feeding 1/2 VCC externally into the 16F690's RA0 Pin 19 (the bolded bit in this command sets this up.... CM1CON0 = %11100011 )
Bookmarks