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 CCS Technical Support

sampling audio output using PIC16f690

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



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

sampling audio output using PIC16f690
PostPosted: Sat Oct 20, 2012 7:12 am     Reply with quote

Hi,

I'm trying to sample the audio jack (3.5mm) of my iPhone/PC using my PIC.
I'm using AN4 as input to my ADC module.
The iPhone's output signal is a pure sine wave around 1K freq.
The results are printed out using UART tool.

However, I'm witnessing strange phenomena in doing so:

1. pic is able to sample iPhone's output, however I'm getting too many zeros. I think that those zeros might be since the iPhone's audio output voltage is negative?
2. same thing happens with the tablet.
3. when connecting the pic to the PC, I get a bunch of zeros. non-stop.
and I'm unable to sample it at all...
Code:

#include <16F690.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT,NOMCLR
#use delay(clock=8000000)
#use rs232(baud=9600,xmit=pin_A0,rcv=pin_A1)
 
 

void main() {
int i=0;
int16 arr[25];
int16 adc_value;
setup_comparator(NC_NC_NC_NC);
setup_adc_ports(sAN4);
set_adc_channel(4);
setup_adc(ADC_CLOCK_DIV_16);
delay_us(20);
 
while (TRUE)
{
   delay_us(20);
   adc_value=READ_ADC();         //READ FROM ADC PORT
   DELAY_US(4);
   //output_low(PIN_C0);
   arr[i] = adc_value;
   i++;
   if (i==25) {
      for (int k=0 ; k<25 ; k++)
      printf("%ld ",arr[k]);
      i=0;
   }
}
}

Any suggestions why this fails?
Is it because the output voltage coming out of any music player has a negative part? (just like the negative part of a pure sine wave).

Thanks alot!
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Oct 20, 2012 1:07 pm     Reply with quote

Use an oscilloscope. It will answer all your questions.

If you have a problem with negative voltage signals you will have to provide an offset.

It's been covered here loads of times.

Mike
danielz85



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

you mean
PostPosted: Sat Oct 20, 2012 1:22 pm     Reply with quote

Thanks.
You mean that I have to do that in hardware ?

Is there any other way to get negative sign to the pic, such as with the comparator, instead of using ADC ?

Our goal is to get low frequency (10-20khz) signal and print it.

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 20, 2012 2:22 pm     Reply with quote

Quote:

Is it because the output voltage coming out of any music player has a
negative part? (just like the negative part of a pure sine wave).

Your question is about level shifting of an analog input signal. The basic
name for this topic is signal conditioning.

Look at the voltage divider circuit in Figure 3 in the link below. This shows
one way to level shift an analog input signal for a PIC:
http://www.analog.com/library/analogDialogue/archives/33-01/chip/
You may need different resistor values (referring to the ratio). You
should use much smaller values, due to the requirements of the PIC's A/D input circuit. And finally, there are other ways to do this.

This thread has a link to an Opamp circuit:
http://www.ccsinfo.com/forum/viewtopic.php?t=29494
danielz85



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

thanks
PostPosted: Sat Oct 20, 2012 2:34 pm     Reply with quote

Thanks, I will do that.
But what about the frequency ?
Can I sample an input of a sinus wave in the frequency of 18khz ?
It seems that I don't have enough samples, and i don't know if this is because the line has a negative values(which gives me so many zero's), or that i don't sample fast enough, or that i can't even sample that frequency ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 20, 2012 2:46 pm     Reply with quote

Scroll down in this thread to Ttelmah's first post. He discusses sampling
an analog signal with the PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=48991
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Oct 20, 2012 5:04 pm     Reply with quote

Quote:
Can I sample an input of a sinus wave in the frequency of 18kHz ?
Do you mean sampling at 18k samples / second ?

OR

Taking enough samples of an 18kHz sinus to be able to reconstruct something approaching the original?

Mike
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sat Oct 20, 2012 10:34 pm     Reply with quote

As others have indicated, the A/d can't handle negative voltages without some sort of level shifting - check out the data sheet for the processor and it will tell you what input voltage range is valid for the A/D input (and typically you want some protection against spikes and excursions outside the specified range - that needs to be done in the hardware). Another issue you are going to run into - if you are trying to send the samples out at 9600 baud, that is approximately 1 ms per character (1 k characters/second) - there is no way you could keep up with anything over 1khz (and that would only be one sample per cycle which is not overly useful). If you wanted to be able to have say, 10 samples per cycle, at 9600 baud, the best you could possibly do is about 100hz. (unless you use some sort of buffer and only capture for a short period then spit the data out again). You need to first look at the signal with an 'scope so you know what you are working with then design the front end to match that with the requirements of the A/D converter in the PIC.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Oct 21, 2012 11:19 am     Reply with quote

Your duplicate thread on this subject has been locked.

Asking the same questions again helps nobody.

The other respondents and I are trying to help you.

We want you to answer OUR questions.

We can't give answers to your questions because you have not provided enough information.

We are asking for more details so that we can decide whether what you are trying to do is possible (or not), or if you should be persuing some other approach.

When we get the all the necessary data, you'll be freely given good advice.

Mike
danielz85



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

Thanks for the replies!!
PostPosted: Mon Oct 22, 2012 1:54 am     Reply with quote

After having read your replies, I've decided not to work with ADC.
Thanks again,
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