CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Sound - A/D and D/A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Joao Alberto
Guest







Sound - A/D and D/A
PostPosted: Wed Oct 25, 2006 12:36 pm     Reply with quote

Hi

I am a beginner in the use of the PIC. I am experiecing some difficulties to use it to make the A/D conversion, then the D/A conversion of the sound from an audio device.
My project consists of connecting the audio exit of an MP3 player (the same one of the earphone) in the RA0 pin of the PIC 16F877. The PIC will have to make an A/D conversion of 10 bits. After, the PIC should do a D/A conversion of 10 bits using PWM. Finally, the RC2(PWM) pin will have to be connected in a regular speaker to reproduce the sound.

IŽd be very thankful if somebody had a PIC code that performs these two functions in the same PIC.
C Turner



Joined: 10 Nov 2003
Posts: 40
Location: Utah

View user's profile Send private message

PostPosted: Wed Oct 25, 2006 2:31 pm     Reply with quote

While I'm not one to "give away the farm" (e.g. post complete, working code - that would prevent one from learning from one's mistakes) I can tell you how it would be done.

- Use a 20 MHz crystal in the HS mode.
- Set timer2 to interrupt at 19.52 kHz (e.g. prescaler = 1, divisor = 255, postscaler = 1)
- Set the A/D clock to clk/32, and use Vss and Vdd for reference.
- Set for A/D channel 0.
- Setup A/D for 16 bits (e.g. left justified)
- Enable PWM.

In the ISR, do these things:

- Get the last A/D conversion. (make sure that you use "read_only")
- Start a new A/D conversion. (make sure that you use the directive that only starts a conversion)
- Put the value that you got into the PWM output. I prefer to use my own functions for this as one isn't always sure what is "inside" a CCS function. To do this, one would take the bottom 2 bits of the A/D conversion (the top 2 bits in the lower byte of the A/D result) and shift them right twice, and then set the next lower 2 bits to maintain the PWM mode. After calculating these values, ONLY THEN would you send them to the PWM hardware: You want to load all of the bits of the PWM register as quickly as possible.

On the hardware:

- Audio will have to be applied to the RA0 pin, but this pin will have to be biased at "mid-supply." If you are running the PIC from 5 volts, this would be at 2.5 volts, and audio would be capacitively-coupled into it.
- The PWM output audio will have to be lowpass filtered. A "reasonable" job of this may be done by simple resistors and capacitors, but a op-amp should be used not only for a "real" lowpass filter (a 3rd order Sallen-Key set to roll off at 4-5 kHz is reasonable) but also to buffer the relatively high-impedance output that would result after filtering.
- The input audio should also be lowpass filtered - and it can be done with exactly the same type of filter as the output audio.

Since the sampling rate is 19.2 kHz in this case, the highest possible input frequency (without terrible distortion/aliasing) will be half of this - but using practical filters, one could expect 4-6 kHz as the high end.

Don't forget that there's no way that your MP3 player is likely to produce 5 volts of audio - which is how much would be needed to fully drive the input of the PIC's A/D converter, so you'd need to make sure that your input had enough gain to produce this amount of signal: With any digital audio sampling, one must always avoid clipping, but also not run the levels too low, or else you aren't taking advantage all of the bits of quantitization that are available: Even a 24 bit audio system is just 8 bits if your audio level is WAYYY to low...

CT
Joao Alberto
Guest







PostPosted: Mon Nov 06, 2006 1:47 pm     Reply with quote

Where can I find code example in C to do this procedure?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 06, 2006 3:06 pm     Reply with quote

This sounds like a student project. Do a web search for suitable
keywords, such as: "audio output" PIC PWM resistor capacitor
Here is one sample project found with that search string:
http://www.ece.ubc.ca/~edc/aurora/nl.html

Change the keywords slightly and search again:
audio PIC PWM resistor capacitor
Result: Here's another page that explains how to do audio output:
http://www.tigoe.net/pcomp/analogout.shtml
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group