Envelope detection via PIC ADC?

Started by Gurner, February 16, 2011, 10:04:31 AM

Previous topic - Next topic

Galego


Gurner

#21
Quote from: Galego on February 22, 2011, 07:07:59 PM
Try running it at 20Mhz.

You think I'd not pondered that one?!

The PIC I'm using  (16f1828) has an issue with ADC'ing above an internal clock of  8Mhz :-(  (PIC16f1828 ...errata sheet - page 3 http://ww1.microchip.com/downloads/en/DeviceDoc/80510C.pdf )

But seriiously, there's no need to go that high using the special event timer for my purposes here  - in fact I'm going to reduce my internal clock to 1Mhz (less is more in my books!).

Galego

Well... that sucks.

I wouldn't say less is more with PICs and Basic. If you were programming in ASM and had full control of what was happening, then yes, you could go for lower frequencies because you'd know how many cycles each instruction was going to take, and you'd also know when that nothing would interfere with the interrupts. But if you do make it at 1Mhz, that's great.

cpm

Quote from: Gurner on February 22, 2011, 07:11:09 PM
But seriiously, there's no need to go that high using the special event timer for my purposes here  - in fact I'm going to reduce my internal clock to 1Mhz (less is more in my books!).

so what is your max frequency that can be processed at the input?
For the release you said it only has a linear decrement of max value, is it?

Gurner

#24
Quote from: cpm on February 23, 2011, 04:35:37 AM
Quote from: Gurner on February 22, 2011, 07:11:09 PM
But seriiously, there's no need to go that high using the special event timer for my purposes here  - in fact I'm going to reduce my internal clock to 1Mhz (less is more in my books!).

so what is your max frequency that can be processed at the input?
For the release you said it only has a linear decrement of max value, is it?


Well my sig gen only goes to 20khz and it seemed fairly stable at that! But it's going to be directly related to your chosen background (special event trigger)  ADC sampling rate ...& how fast your main loop runs to act upon the incoming ADC samples.

A little bit of jitter here, also it should read 64 (2.5V), but the error is likely attributable to the DC level the AC signal is sitting on.....




the other beauty here is that you lose no diode forward voltage drop! (as you do with a hw peak detect circuit).

The release (discharge) rate is configurable...but yes, it is linear.

cpm

#25
at 8mhz, the fastest ADC time is FOSC/8 setting, which needs 12us to complete the reading.
Even if the ADC is constantly working, the fastest you can get is 24 instruction cycles in which you have to do: switch into interrupt code (ADC completion int-flag), set bit for new analog read, "do things", clear adc-int-flag, restore state after interrupt.
the "do things" part may be about 15 instructions spare, and must include reading the ADDRESH byte, do some calculations, and write to
the output... (thats a tight margin)

edit: actually, i see the 16f automatically saves state on interrupt switching, using shadow registers, that means there is no overhead?

If you want at least N samples per per period of input, the limit for an input frequency would be around 80/N Khz

Gurner

#26
Quote from: cpm on February 23, 2011, 06:20:14 AM
at 8mhz, the fastest ADC time is FOSC/8 setting, which needs 12us to complete the reading.
Even if the ADC is constantly working, the fastest you can get is 24 instruction cycles in which you have to do: switch into interrupt code (ADC completion int-flag), set bit for new analog read, "do things", clear adc-int-flag, restore state after interrupt.
the "do things" part may be about 15 instructions spare, and must include reading the ADDRESH byte, do some calculations, and write to
the output... (thats a tight margin)

If you want at least N samples per per period of input, the limit for an input frequency would be 74/N Khz

I'm using the 16f1828's special event timer, this from the datasheet....

"(CCPxM<3:0> = 1011), the CCPx module does the
following:
• Resets Timer1
• Starts an ADC conversion if ADC is enabled
The CCPx module does not assert control of the CCPx
pin in this mode.
The Special Event Trigger output of the CCP occurs
immediately upon a match between the TMR1H,
TMR1L register pair and the CCPRxH, CCPRxL register
pair. The TMR1H, TMR1L register pair is not reset
until the next rising edge of the Timer1 clock. The Special
Event Trigger output starts an A/D conversion (if
the A/D module is enabled). This allows the CCPRxH,
CCPRxL register pair to effectively provide a 16-bit programmable
period register for Timer1."


so there's no interrupts involved here (therefore no overhead of context saving) - all the 'ADC overhead' is done in hw in the background, which allows you to have a  very fast main loop that pulls in the value of the ADC register & then massage it as you require.

Edit: Just re-read your post, so at an 8mhz clock, the fastest the ADC can run at is 1Mhz? (FOSC/8) Therefore every 1uS an ADC sample? But it takes 12us to complete...which suggests a maximum sample rate of 83,333Khz? Have I got that right?

If so, certainly an input audio signal of 20Khz is pushing it - but should be fine for a guitar's bandwidth (I'm only interested in extracting signal level at the fundamental which for myguitar is  1.5khz...therefore about 50 samples per cycle). That said once Microchip sort the aforementioned 16f1828 ADC problem, the internal oscillator can go as high as 32Mhz - and all this for an chip that costs £1!




cpm

a lot of goodies from that 16f pic...

¿how do you output the voltage? the builtin DAC? pwm?

Gurner

#28
Quote from: cpm on February 23, 2011, 07:24:34 AM
a lot of goodies from that 16f pic...

¿how do you output the voltage? the builtin DAC? pwm?

I'm not outputting any voltage in this case...I'm simply using the PIC for general switching duties....& digital AGC/envelope detection (the latter being why I want to extract signal magnitude from the incoming AC signal).

The onboard DAC 5 bit sucks...it's more of a simple 32 level potential divider to allow you to output voltage reference levels to suit .....so for all there are a lot of features onboard, external RAM & and a DAC would be needed if you want to use this for digitizing an audio stream (I'd not go this route if that was my end goal)

JKowalski

Quote from: Gurner on February 23, 2011, 07:30:30 AM
Quote from: cpm on February 23, 2011, 07:24:34 AM
a lot of goodies from that 16f pic...

¿how do you output the voltage? the builtin DAC? pwm?

I'm not outputting any voltage in this case...I'm simply using the PIC for general switching duties....& digital AGC/envelope detection (the latter being why I want to extract signal magnitude from the incoming AC signal).

The onboard DAC 5 bit sucks...it's more of a simple 32 level potential divider to allow you to output voltage reference levels to suit .....so for all there are a lot of features onboard, external RAM & and a DAC would be needed if you want to use this for digitizing an audio stream (I'd not go this route if that was my end goal)


Dont' write off the PWM, it makes a good DAC, especially for low frequency output. ( I know you don't need it, just saying)