Low frequency audio level detector using PIC ADC?


Closed Thread
Results 1 to 40 of 69

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    Ok, so in the light of your point about taking longer in the interrupt routine, I went back to basics...

    Code:
    ADC_Sample:
    @ INT_DISABLE  TMR2_INT     ; enable Timer 0 interrupts
        toggle PortC.3
    @ INT_ENABLE  TMR2_INT     ; enable Timer 0 interrupts
    @ INT_RETURN
    This gives me an interrupt frequency of 11.44khz (ie 5.72khz on the scope)...so my interrupt routine is severely skewing my interrupt! (told you I sucked!)

    It's late here in the UK...need to sleep on this one - many thanks for your help.

  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    Having slept on this....think I'm gonna have to give this one much more thought.

    Not the interrupt frequency problem (btw - Bert, I think you were right...put the peak detect code in the main loop!), but I noticed late last night that for the above code 'trapped' the peak of a nice sigen sine wave very well, a real life guitar signal caused problems. Basically, a guitar signal is full of harmonics...so you've not just got a fundamental going on, you've got strong second harmonic immediately after a string has been plucked - these harmonics are lower in signal magnitude & worse still, force the signal to cross zero too - which plays havoc with the larger fundamental peak detect bit.

    I guess what I really need is a slick bit of code that acts like a diode with a smoothing cap following on - there's probably a role to play with DTs averaging routines here!

    Something along these lines...

    1. Is present ADC sample larger than last ...if so then store it (that'll be the diode bit)

    2. When cycle crosses zero, then largest sample from the logic above is the peak.

    3. Have a rolling average for all the peaks (that'll be the capactor/LPF bit)


    One to mull.
    Last edited by HankMcSpank; - 17th February 2011 at 10:46.

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


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    Quote Originally Posted by HankMcSpank View Post

    This gives me an interrupt frequency of 11.44khz (ie 5.72khz on the scope)
    Hank, this still bothers me.

    Can you try this:
    Code:
    BMAIN:  'make this your new temporary main for this test
     LATC = LATC ^ %00001000 ' this assumes you can use portc.4 as an output.
     GOTO BMAIN
    
     'new ISR
    LATC = LATC ^ %00000100 ' this will toggle portc.3
    the point here is main will just toggle portc.4 and the ISR will toggle portc.3. When you scope these, we will see the speed at which you are running. We expect main to be ~4 instructions, maybe 6. so that would give C.4 a frequency of .5M (.25M) and the ISR should interupt every 7 or 8th time through main.
    Last edited by cncmachineguy; - 17th February 2011 at 12:23. Reason: Fixed frequency for 8M osc
    -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!

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    Hi Bert,

    Sure, I will certainly try that (it troubles me & I'd like to understand why I'm not getting the interrupt frequency that I should), but it won't be until tonight after the kids have been put to bed! I'm in the UK, so it'll probably be about 5.00pm EST when I have some results.

  5. #5
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    I am no fan of doing the RMS thing on the PIC itself, but in any case you probably be better served by having the CCP module trigger the ADC conversion. That way you get a very precise sampling rate (look at section 15.2.5 SPECIAL EVENT TRIGGER of the datasheet).

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


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    Oh Thats a pretty sneaky way to do it. (BTW, section 16.2.5 not 15.)

    @Hank, are you by any chance using this in the same applaction as the phase do-hicky you were working on a little while back? Seems if so, you may already have the parts in place for the trigger.
    -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!

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    languer - tks, I'd actually seen that previously in the datasheet, but couldn't immediately work out how to apply it, then I forgot about it.

    Bert - phase project has been parked up.

    Meanwhile, this ongoing problem is going to turn out so easy that I'll never be able to show my face on here again.

    Ok, the latc didn't work, so I'm using a basic toggle, here the stripped back code...

    Code:
    @ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF & _LVP_OFF & _CLKOUTEN_OFF 
    @ __CONFIG _CONFIG2, _LVP_OFF & _PLLEN_OFF
    
    
    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
    
    Osccon = %01110010      'Osc 8Mhz
    
    DEFINE  OSC 8
    DEFINE  NO_CLRWDT 1   ' PBP doesn't clear WDT automatically
    
    '16F1828 Pin Connections*********************************************
    TRISC.4 = 0     '6   RC4      Main Output         
    '**********************************************************************
    CM1CON0 = 0   ' COMPARATORS OFF
    CM2CON0 = 0   ' COMPARATORS OFF
    
    ADCON0 = 0
    ADCON1 = 0
    
    ANSELA     = 0          ; All Port A ports to be digital
    ANSELB     = 0          ; All Port B ports to be digital
    ANSELC     = 0          ; All Port B ports to be digital
    
    txsta = %10100100  'needed to turn on pin 10 as valid HSerout pin. 
    
    WPUA = 0        'weak pullups disabled.
    WPUB = 0        'weak pullups disabled.
    WPUC = 0        'weak pullups disabled.
    
    'Timer2 Registers:
    'Prescaler=1:1; TMR2 PostScaler=1:1; PR2=66 - Freq = 30,303.0303Hz - Period = 33,000 ns
    T2CON.6  = 0 ' bits 6-3 Post scaler 1:1 thru 1:16
    T2CON.5  = 0
    T2CON.4  = 0
    T2CON.3  = 0
    T2CON.2  = 1 ' Timer2 on bit: 1=Timer2 is on;
    T2CON.1  = 0 ' bits 1-0  Prescaler Rate Select bits
    T2CON.0  = 0 
    PR2 = 66            ' PR2 (Timer2 Match value) 
    
    
    ASM
    INT_LIST  macro ; IntSource,    Label,         Type, ResetFlag?
        INT_Handler   TMR2_INT,  _Bert_ISR,   PBP,  yes
        endm
        INT_CREATE       ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE  TMR2_INT     ; enable Timer 0 interrupts
     
    ;----[Main Program Loop]------------------------------------------------------------------
    BMAIN:  'make this your new temporary main for this test
     toggle portc.4   'pin 6   (blue scope trace)
     'LATC = LATC ^ %00001000 ' this assumes you can use portc.4 as an output.
    
     GOTO BMAIN
     
     '---[TMR2 - interrupt handler]--------------------------------------------------
    Bert_ISR:
    @ INT_DISABLE  TMR2_INT     ; enable Timer 0 interrupts
        toggle PortC.3   'pin 7 (yellow scope trace)
        'LATC = LATC ^ %00000100 ' this will toggle portc.3
    
    @ INT_ENABLE  TMR2_INT     ; enable Timer 0 interrupts
    @ INT_RETURN
    
    end
    & here's the result....

    green trace is the main loop toggle (port C.4) with a frequency of just 1kHz(!!!) & the yellow trace is the ISR toggle (port C.3)with a frequency that's higher at 12Khz(!!!) , I have not got one clue what is going on here - it feels like 'week one - intro to PICs' all over again!


    Last edited by HankMcSpank; - 17th February 2011 at 23:23.

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


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    Hank, 2 things to try. First turn off use PBP in the int setup. check the output. then change pr=200. no reason for the value, just much larger. You don't need to post the Scope unless you want. I am happy with just the numbers right now.

    Code:
    INT_Handler   TMR2_INT,  _Bert_ISR,   PBP,  yes
    
    becomes
    
    INT_Handler   TMR2_INT,  _Bert_ISR,   ASM,  yes
    -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!

  9. #9
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Low frequency audio RMS signal level detector using PIC ADC?

    Hi Bert,

    You're onto something!

    Firstly, this doesn't stack up, but when I disable interrupts my Main loop sees a frequency of 142Khz on my scope (therefore double that at 284khz?!)

    Ok, now when I enable interrupts with the 'pbp' entry ....much baddo, my main loop toggle shows just 498Hz on my scope (therefore about 1khz)

    change the bit you outlined to it so asm, I once again get the main running 142khz (albeit choppy - I guess the interrupts are stopping a nice clean toggle in the main) & the interrupt toggle showing about 15khz on my scope (30khz)

    changing PR=200, changes the interrupt toggle rate to about 5khz on my scope (therefore 10khz)

    it's after 1.30am here in London.....and so to bed, g'night.
    Last edited by HankMcSpank; - 18th February 2011 at 01:44.

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