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, here's what I see with a PR value of 200....

    (top green trace is main loop 'toggle', the lower yellow trace is interrupt 'toggle ....oh btw, ignore the spikes sitting on the square waves!)



    now here it is with a PR value of 66....



    Now, whilst I can understand the need to get a clean main loop (& establish what PR2 value yields that), I'm wondering here what's going on with the PR2 value ....does higher mean less interrupts? (ie should I be taking the PR2 value higher or lower)...cos whichever way I quickly experimented with I got an uneven main toggle trace!

    oh & btw, how did you determine the port toggle took 7 instruction cycles a couple of posts ago?!!


    I've pondered the end goal a little bit - an anlogue peak detect circuit emulator.

    What's going on with a peak detector is that a cap charges up to reflect the value an AC signal....a diode stops the cap charge decrementing when the waveform traverses back towards zero - meanwhile there's another resitor in parallel across the cap that disharges the cap (at a t rate chosen by the circuit designer).

    So I guess I need some code like this...

    1. Take an ADC Sample (via a timer ...or special event trigger etc)
    2. Is the present signal sample bigger than the last? ......if yes, then update a variable with the latest value, if not then do nothing.

    Have another timer decrement the same variable (this is the discharge time)


    Ought to be simple enough (once I get a grip on this interrupt frequency melarkey!).....and would stop the strong second harmonic presence messing up the overall results as seen when I put a guitar into the circuit.
    Last edited by HankMcSpank; - 21st February 2011 at 19:05.

  2. #2
    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?

    Ok hank, since you choose to swallow the red pill, here goes:

    PR works opposite from the timer's. Timers ALWAYS count up and generate interrupt (if enabled) on overflow, So for a given count, we preload the timer to start somewhere other then zero. PR on the other hand, is a match for the timer. So if PR=200, the timer will start counting (from zero). When it gets to 200 (TMR = PR), timer is reset and starts again. If there is no PR postscaler, interrupt will be set.

    So a higher PR will yeild less interrupts. I would like to take back my statement about a "clean" main. You can NEVER acheive that in the literial sense if using interrupts by definiation it will be interrupted. On the other hand, lets say the main is 7 instructions, you would never expect main toggle to be less then 1/7th of its uninterupted rate. Do you see why?

    Make sense?

    How did I know 7 insctructions? Its just math. you are running a 8 Mhz clock. that becomes 2MIPS. (2 million insctructions per second). 1 instruction takes .0000005 sec * 7 = .0000035 seconds. 1/.0000035 = ~285KHz. Ok really this is the proof, but go backwards to get the number of instructions.
    -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: Low frequency audio RMS signal level detector using PIC ADC?

    Ok, yes, I'm getting there (thanks), but I come back to how did you know the main took 7 instructions, once again, here's the main...

    Code:
    BMAIN:  'make this your new temporary main for this test
     toggle portc.4   'pin 6   (green scope trace)
    GOTO BMAIN
    So, to my eyes, there are two instructiions in there ...the toggle & the goto ...do these add upto to the aforementioned 7?
    Last edited by HankMcSpank; - 21st February 2011 at 20:11.

  4. #4
    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
    Ok, yes, I'm getting there (thanks), but I come back to how did you know the main took 7 instructions, once again, here's the main...

    Code:
    BMAIN:  'make this your new temporary main for this test
     toggle portc.4   'pin 6   (green scope trace)
    GOTO BMAIN
    So, to my eyes, there are two instructiions there ...the toggle & the goto ...do these add upto to the 7?
    The confusion comes from my exclusion. When I am talking about number of instructions, I mean to say ASM instructions. This just shows the beauty of PBP. I prolly should use the term fosc/4, but I have always just thought about it as instructions. BTW, some take 1(fosc/4) and some take 2. GOTO (in ASM) takes 2 while BCF (clears a bit) takes 1
    Last edited by cncmachineguy; - 21st February 2011 at 20:16.
    -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!

  5. #5
    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?

    Quote Originally Posted by cncmachineguy View Post
    The confusion comes from my exclusion. When I am talking about number of instructions, I mean to say ASM instructions. This just shows the beauty of PBP. I prolly should use the term fosc/4, but I have always just thought about it as instructions. BTW, some take 1(fosc/4) and some take 2. GOTO (in ASM) takes 2 while BCF (clears a bit) takes 1
    thanks bert...ok, so taking PR2 = 8 yields an interrupt toggle of 22khz, therefor 44khz (I took PR2 down to 6, but the interrupt frequency on my scope stayed the same?!)

    I'm thinking about putting the 'capacitor discharge' emulation into the same interrupt, so something high level like this...

    (I will use a variable called previous_sample to store highest ADC sample)

    1. does present incoming ADC sample equal (or is greater than) previous_sample.

    i)if so previous_sample = present incoming sample .....& retrun

    ii) if not, decrement previous_sample by '1' & return (the 1 here can be altered depending on how fast you want the cap emulation to dischare)

    with an ADC sample of 8 bits, the maximum signal sample is 255.....the maximum pseudo discharge time will be the interrupt rate 22.27uS (44khz) * 255, therefore about 6ms (which is nicely tied in with the lowest frequency 'half cycle' on a guitar @82Hz)....sound about right?
    Last edited by HankMcSpank; - 21st February 2011 at 21:58.

  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?

    Quote Originally Posted by HankMcSpank View Post
    thanks bert...ok, so taking PR2 = 8 yields an interrupt toggle of 22khz (I took PR2 down to 6, but the interrupt frequency on my scope stayed the same so it looks like I'll have to stick with that?!)
    This doesn't sound to good. lets see if it makes sense: PR2=8 means every time TMR2 counts to 8, an interrupt should fire and TMR2 gets reset. with PR2=66, you had ~30K. So remember the main thats approx 7 clock ticks(clock ticks = instructions, so as not to continue confusing everyone). The math remains the same, for TMR to count to 8 thats 4uSec! 1/.000004 = 250K, not 22K. So my guess is you are going too fast. what was mains frequency? start back at PR2=66 and increase it. see if the INT gets slower, until it is 20K. At PR2=200, I think you were at 15K? so just guessing, maybe a value of ~130?

    Now about that ADC time. I haven't read the errata, but did it say not to run faster then 8mHz or not to run the ADC faster? I ask because I saw something about the speed of the ADC implying there is a prescaler for that so maybe you could run at 32mHz?

    I'm thinking about putting the 'capacitor discharge' emulation into the same interrupt, so something high level like this...

    (I will use a variable called previous_sample to store highest ADC sample)

    1. does present incoming ADC sample equal (or is greater than) previous_sample.

    i)if so previous_sample = present incoming sample .....& retrun

    ii) if not, decrement previous_sample by '1' & return

    with an ADC sample of 8 bits, the maximum signal sample is 255.....the maximum pseudo discharge time will be the interrupt rate (22khz) * 255, therefore about 12ms (which is nicely tied in with the lowest frequency 'cycle' on a guitar @82Hz)....sound about right?
    I don't know much about this, I will have to think on it tonight when I get home from work.
    -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?

    thanks bert...slight oversight in my previous (noe edited)...the toggle frequency on my scope is HALF the interrupt frequency!

    Re the pseudo cap - a peak detect cicuit (the end goal here)....it's basically a diode followed by a cap...when the incoming ac signal is higher in magnitude in voltage than that across the cap the diode allows it through...and the cap's voltage increases......when the incoming signal is lower the diode switches off....the cap holds the voltage, but for this circuit to be useful, we actually add in a resistor to discharge it at the designer's choice.....hence wanting to decrement the ADCsample variable when the incoming signal is lower.

    http://hades.mech.northwestern.edu/i...#Peak_Detector

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