Frequency detection (audio)


Closed Thread
Results 1 to 23 of 23

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Frequency detection (audio)

    Thanks for the input Bert, I changed the interrupt to ASM...
    Code:
    ASM
    INT_LIST  macro ; IntSource,    Label,         Type, ResetFlag?
        INT_Handler  CMP2_INT,  _CMP2_Interrupt,  ASM,  YES 
        endm
        INT_CREATE       ; Creates the interrupt processor
    ENDASM
    and modified my ISR...

    Code:
    CMP2_Interrupt:   ' COMPARATOR2 INTERRUPT.
    BSR=0 
    toggle portc.7
    TMR1=0
    @ INT_RETURN
    but I still can't my audio input above 900Hz, without the ISR toggle pin scoping wrong :-(

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Frequency detection (audio)

    Hank do the numbers below 900 seem to make sense? We should be able to predict the TMR output based on some simple math. You are running 4MIPS (16Mhz OSC). so that would be .25uS per instruction. If the timer is running from Fosc, a 1 K signal would have a count of 4000? 1/1000 = 1mS. 1mS/.25uS=4000. Assuming the ISR fires 1 time per cycle. Likewise we would expect a count of around 8000 for a 500Hz signal. But either you don't have it set up as I assume, or something is wrong that will require seeing more of your code.

    BTW, using my assumptions, 82.4Hz is 48,543.
    330Hz = 12,121
    600Hz = 6666
    650Hz = 6153

    I can't decide if that looks linear, but it should.

    Also try clearing the TMR1 high byte and low byte seperatly in the ISR. I don't think this should help, but it can't hurt
    Last edited by cncmachineguy; - 7th June 2012 at 20:36.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Frequency detection (audio)

    Hi Bert,

    I re-enabled hserout out in the ISR again, and these are the TMR1 counts I'm getting for successive comparator2 interrupts with an audio input of 82.4Hz (the lowest note/frequency on a standard tuning electric guitar)...

    42947
    42995
    42946
    42998
    42944
    43003
    42960
    43012
    42964
    43022
    42974
    43032
    42991
    42948
    18932
    18559
    42953
    43012
    42955
    43005
    42957
    43001
    42943


    A couple of dodgy ones in there, but those aside, the TMR1 counts are coming in lower at about 43,000?

    Code:
    @ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
    @ __CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF 
    DEFINE  OSC 16
    INCLUDE "DT_INTS-14.bas"     
    INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 25  ' 38400 Baud @ 16MHz, 0.16%
    SPBRGH = 0
    BAUDCON.3 = 1         ' Enable 16 bit baudrate generator
    '16F1828 Pin settings....
    ' PIN# NAME     USE & CONNECTION
               '  1   Vdd      +5VDC power supply
    TRISA.5 = 0'  2   RA5      
    TRISA.4 = 1'  3   RA4      
    TRISA.3 = 1'  4   RA3      
    TRISC.5 = 0'  5   RC5      
    TRISC.4 = 0'  6   RC4      C2OUT 
    TRISC.3 = 1'  7   RC3         
    TRISC.6 = 0'  8   RC6           
    TRISC.7 = 0'  9   RC7      
    TRISB.7 = 0'  10  RB7       
    TRISB.6 = 1'  11  RB6       
    TRISB.5 = 1'  12  RB5       
    TRISB.4 = 1'  13  RB4       
    TRISC.2 = 1'  14  RC2      
    TRISC.1 = 1'  15  RC1      C12IN1- (FREQ DETECT)
    TRISC.0 = 1'  16  RC0      
    TRISA.2 = 0'  17  RA2               
    TRISA.1 = 0'  18  RA1      
    TRISA.0 = 0'  19  RA0      
    '             20  Vss      Ground
    ANSELA     = 0 
    ANSELC     = 0   
              
    APFCON0.2 = 0  'put hserout onto RB7 pin 10 (Pickit 2 RX pin)
      
    '-------------------------------------------------------------------------
    Osccon = %01111010   'sets the internal oscillator 
    CPSCON0 = 0    ' capacitive sense module off
    CM1CON0 = 0     ' comparator1 module off
    CM2CON0.7 = 1 ' comparator2 on.
    CM2CON0.5 = 1   'enable the output on a pin
    CM2CON0.4 = 1 'invert the polarity
    CM2CON0.1 = 1   'ENABLE HYSTERISIS
    CM2CON1.7 = 1' enable interrupts on _+ve going transition
    CM2CON1.5 = 0   ' set comparator comparison input pin to the DAC
    CM2CON1.4 = 1   ' set comparator comparison input pin to the DAC
    CM2CON1.1 = 0   'SELECT C12IN1- (PIN 15, RC1)
    CM2CON1.0 = 1   'SELECT C12IN1- (PIN 15, RC1)
    
    DACCON0.7 = 1 ' DAC ON
    DACCON0.6 = 1 ' DAC ON
    DACCON0.5 = 0  'put the output of the DAC o the DAC out pin
    DACCON0.3 = 0   'SET DAC +VE REFERENCE REFERENCE TO VCC
    DACCON0.2 = 0   'SET DAC +VE REFERENCE REFERENCE TO VCC
    DACCON1 = %00000001   'sets the DAC voltage output to be very low (to use for the comparator to compare against.
    OPTION_REG.7 = 0
    ASM
    INT_LIST  macro ; IntSource,    Label,         Type, ResetFlag?
        INT_Handler  CMP2_INT,  _CMP2_Interrupt,  ASM,  YES 
        endm
        INT_CREATE       ; Creates the interrupt processor
    ENDASM
    TMR1 = 0
    T1CON = %00000111
    @ INT_ENABLE CMP2_INT   ; Enable 'Int On Change' interrupts
    loop1:
    PAUSE 1
    GOTO loop1
    
    'Interrupt+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    CMP2_Interrupt:   ' COMPARATOR2 INTERRUPT.
    @ INT_DISABLE CMP2_INT
    BSR=0
    hserout [dec tmr1,13,10] 
    toggle portc.7
    TMR1=0
    @ INT_ENABLE CMP2_INT
    @ INT_RETURN
    Last edited by HankMcSpank; - 7th June 2012 at 21:32.

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Frequency detection (audio)

    Just as an experiment, I would like to see how high in audio frequency I can go with something like the ASM interrupt method discussed in this thread...

    http://www.picbasic.co.uk/forum/showthread.php?t=14513 (even though I asked a question in that thread, I confess to not being able to move forward!)

    So would anyone do a little bit of ASM code that will toggle portC.7 when comparator2 interrupts on a 16f1828?

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Frequency detection (audio)

    Just thinking out loud here...if all the overhead comes from entering & exiting the interrupt routine ...if I dedicate my main loop solely to measuring high/low transitions on a pin (ie feed the PICs comparator output from its output pin into another digital input pin), would this be workable?

    Just pondering how I can get the PIC to detect up to 1.5khz!

  6. #6
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default Re: Frequency detection (audio)

    Did you try COUNT or PULSIN or using TIMER to count freq...
    I have set TMR0 to overflow each second and trigger interrupt. In ISR read TMR3, then reset it.
    It goes to 50KHz without any problem. You also can use shorter time, but then you have lower resolution.

  7. #7
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Frequency detection (audio)

    Hi Hank!
    There are several approaches to solving what you wish to do. One site ( http://www.intmath.com/trigonometric...hase-shift.php ) will give you the math involved in determining a given musical note. Another is ( http://liutaiomottola.com/formulae/freqtab.htm ). If you look at the frequencies for each note notice that if you take the highest "C" note, the next octave of "C" below it is exactly 1/2 the frequency. Where things become "spritual" is that when mixing musical note the mix will give the sum, the difference, and the individual frequencies. The only way I can see to make the distictions would be with "band-pass" filters or a graphic equalizer. You would probably have to take the range of audio (20-20,000 Hz) and divide it into something like 10 or more ranges and then using a high pass and low pass filter "comb" the input sound. Or one big look-up table! The secondary problem would come from how perfectly the guitar is tuned. Hope this helps, Ed

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts