Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Sampling rate of a PIC. Please Help!

john_19

New Member
Hello everybody, I'm currently using the PIC18F4550 with 20MHz crystal and the PLL enabled so I have an Fosc = 32MHz.
I want to use the ADC for an audio application and the thing is that I searched the forum and read the datasheet but still don't have a clear idea of how to set up a sampling rate of 8KHz.

Can anybody help me please?? I don't have much experience with PIC's.
Thanks!!!
 
I would set up a timer to interrupt at the frequency you need, 8KHz. Have the timer ISR read the last sampled value then start the next sample. It sounds backwards but by doing this you do not have to wait for the ADC to finish (assuming there is enough time between interrupts), but just for kicks and giggles check the done bit prior to reading till you trust the code. ADC interrupts are not used.

As always keep the ISR short.

What I have not addressed is setting up the ADC and if or not it will be ready in time.
 
Last edited:
Thanks for the reply 3v0. I'll try the ISR. I was actually re-reading the datasheet and you have the posibility of setting the acquisiton time (Tacq) in a manual mode by setting 3 bits in "0" (ADCON2 register) so I guess I can do that and use a Timer for the ISR. My next silly question is, how would I know the Tacq?? I mean, if I set it up in a manual option...do I have to use a delay or something to make the Tacq bigger than the minimun 2.45us required (according to datasheet)???
Again thanks for the help.
Regards.-
 
Thanks for the reply 3v0. I'll try the ISR. I was actually re-reading the datasheet and you have the posibility of setting the acquisiton time (Tacq) in a manual mode by setting 3 bits in "0" (ADCON2 register) so I guess I can do that and use a Timer for the ISR. My next silly question is, how would I know the Tacq?? I mean, if I set it up in a manual option...do I have to use a delay or something to make the Tacq bigger than the minimun 2.45us required (according to datasheet)???
Again thanks for the help.
Regards.-

Tacq is the time it takes to get one sample (acquisition time), not the time between samples. Longer Tacq gives the sampling capacitor (inside PIC) more time to charge and results in more accurate conversion for low frequency signals. Too long Tacq acts a bit like low-pass filter for high frequency signals. Too short Tacq results in poor conversion accuracy.

EDIT:
I got confused about how the Tacq affects the conversion accuracy. The above might be incorrect. But the point is that the Tacq is the acquisition time, not the time between samples.. and that the Tacq does affect the sampling accuracy somehow.

EDIT2:
I read the datasheet (page 265) and it says: "the A/D module continues to sample the input for the selected acquisition time, then automatically begins a conversion." So the Tacq is the sample-and-hold time. With that cleared I believe everything I originally said is correct. I suggest that you do not use the manual mode for Tacq. In manual mode you need to wait for the wanted acquisition time and then start the conversion manually.

I would use the CCP2 module with Timer1 to automatically trigger the ADC conversion at constant intervals (8Khz). This way you get the most constant sampling rate possible and save processing time for other things. Read the datasheet page 268. https://www.electro-tech-online.com/custompdfs/2012/08/39632d.pdf

Use the A/D interrupt to read the conversion result. This way you get the smallest delay possible.
 
Last edited:
Take a look at section "15.3.4 SPECIAL EVENT TRIGGER". The special event trigger can automatically trigger the ADC conversion at whatever rate you program.
 
Audio..... With a sample rate of 8khz it won't be very audible.... The rule of thumb is sample twice as fast as the highest frequency... The highest audio frequency is 22khz so most audio is sampled at 44khz.....

That said, the fastest I could ever get out of a pic is 15khz... The speed and size of the pic has no bearing on the results.. The DSP range of pics are more equipped for this task..


If you need just simple sound https://www.moty22.co.uk/sd.php
 
Last edited:
Audio..... With a sample rate of 8khz it won't be very audible.... The rule of thumb is sample twice as fast as the highest frequency... The highest audio frequency is 22khz so most audio is sampled at 44khz.....

Not exactly - audio is generally considered as 20Hz-20KHz, and you should really sample at MORE than twice the highest frequency - audio CD's are filtered at 20KHz, and then sampled at 44.1KHz.

8KHz will be perfectly audible, about the same as a normal telephone conversation, but you should have a sharp lo-pass filter at about 3.5KHz before the A2D.
 
Nigel... but surely if he is sampling at 8khz the audio will be around the 4khz ( as you said ) this will be be for voice only... music audio will be very poor...

Where did he say he was wanting to do music? - and while it WOULD be poor, music was sold via the phone system for a great many years, and was a successful popular service (analogue phones, long pre-dating the Internet).

But it's folly to suggest it wouldn't be 'very audible', as it would be perfectly audible - just like a phone call.
 
Hi!! Thank you so much for the replies. misterT, languer and 3v0 thanks for clarified the Tacq, I'll take a look at CCP2 and see how it works.
Ian Rogers, Nigel Goodwin, sorry if I was not clear. I'm willing to get the quality of a telephone call so based on what I read an 8KHz sampling rate should work fine.
I'll get back with the results!
Regards to all!
 
Hi!! Thank you so much for the replies. misterT, languer and 3v0 thanks for clarified the Tacq, I'll take a look at CCP2 and see how it works.
Ian Rogers, Nigel Goodwin, sorry if I was not clear. I'm willing to get the quality of a telephone call so based on what I read an 8KHz sampling rate should work fine.
I'll get back with the results!
Regards to all!

Don't forget, you MUST filter before the A2D.
 
I do not see any advantage in using anything but a free running timer with a 8KHz freq as described in my first post. When you service the timer interrupt read the value from the now done conversion and start the next one.
 
I do not see any advantage in using anything but a free running timer with a 8KHz freq as described in my first post. When you service the timer interrupt read the value from the now done conversion and start the next one.

Hi 3v0, well I did try what you described in your first post and I could set up the sampling rate of 8KHz. In order to check this I toggled a pin and measured the freq and I'm getting 8KHz. Now, I'm dealing with another problem. I created a UDP packet of 160 bytes and send chunks of 20ms of audio but when I measured the time between packets what I got is 28ms and I don't know what it's causing that delay.
Regards.
 
Thanks for the replys Nigel. Yes I designed a filter I just have to build it. I'll try with a second order filter.

Have a nice weekend!
 
I do not know where the delay is from. What happens if you set the packet size to 30ms.

Are you running the microchip stack for the UDP or are you using another chip to handle the Ethernet?

I haven't tried that. I'm using the ENC28J60 and the microchip stack. I'll keep looking and see if I can figure out what the problem is.

Regards.-
 
I do not see any advantage in using anything but a free running timer with a 8KHz freq as described in my first post. When you service the timer interrupt read the value from the now done conversion and start the next one.

Well, I don't see any advantage in that method compared to using the event trigger and A/D interrupt.. I am a little hung over.. sorry for a useless post :)
 
Nigel... but surely if he is sampling at 8khz the audio will be around the 4khz ( as you said ) this will be be for voice only... music audio will be very poor...

Probably not as bad as most people would think! :)

When I was a PC games programmer in the late '80's to early 90's many of the games used 8kHz and 11kHz mono sound with 8bit sampling. Speech will play with very nice quality and even music etc sounds pretty good. I used 11kHz 8bit mono for music on one game, and was able to get fairly decent rock music playback.

All the notes etc in music and voice are far below 10kHz, and the only real 5-10kHz content is in things like hihats and cymbals etc the "hssss" sounds. You can bump the HF content in audo preprocessing, so after encoding to 8kHz or 11kHz 8bit sound the music still sounds quite reasonable, although it's not HiFi quality (but then neither are most MP3s).

And as I said voice will sound good at 8KHz even with very little preprocessing needed.
 
Last edited:

Latest threads

Back
Top