• 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.

ADC conversion time

Status
Not open for further replies.

avi

New Member
Hi all,
I'm using the PIC18 as an audio transceiver (using the UART).
I have a problem with the sampling rate, and it seems to come from long conversion times of the internal ADC module (216uS => fs=~4.6Khz).
too low sample rate- bad sound and distortions...
I've tried to change the convertion times at the ADCON0/1 and it did't help alot.

I'm using the PIC18LF452 with 20MHz crystal.
According to the data sheet, even if I take the highest Tad, 64*Tosc- I get
Tad=64*(1/20MHz)=3.2uS
Tconv.=12*Tad= 38.4uS ==> 26KHz- but I get only 4.6Khz!!!

I need your kindly help to understand where I'm wrong, and how can I reduce the conversion times of the ADC.
TIA,
Avi.
 

avi

New Member
ADC code

I suggest you post your code, how can people suggest what's wrong without it?
Hi,
It seems that the low sample rate comes from the very long ADC conv. (I'm sure about that..)
the ADC code is a very simple:
"
char ADC(int ch)
{
char t; int i;
if (ch==0) ADCON0=0x85;
if (ch==1) ADCON0=0x0D;
while(GODONE); // check if conv. ended
//for(i=0;i<100;i++); //dalay

t=((ADRESH<<8)+ADRESL)/4;//compress from 10 bit to 8 bit


return t;
}
"
 

Nigel Goodwin

Super Moderator
Most Helpful Member
If the conversion time is so drastically different to the datasheet, then you're doing something VERY wrong - either setting it incorrectly, or running at a much slower clock speed than you thought. Your code doesn't even mention ADCON1?.

BTW, the line

t=((ADRESH<<8)+ADRESL)/4;//compress from 10 bit to 8 bit
is completely superfluous, and only wastes yet more time - you can set ADCON1 to give an eight bit reading in a single register.
 

avi

New Member
If the conversion time is so drastically BTW, the line



is completely superfluous, and only wastes yet more time - you can set ADCON1 to give an eight bit reading in a single register.
You right, I changed it to single register and it's much better now!!
Thanks ;)
 

ella

New Member
How do you sync AD conversion and UART transmit? If you are making it serially you loose some time as well. You can split it this way:
Start A/D
Send previous byte
Wait A/D
Get A/D result
 

avi

New Member
My rate now after changing to single register is : 103KHz ( convertion and transmitting)- by far much better :)

Now I'm trying to get over another problem of a backround noise (like white noise) which is added... I don't know where it comes from.
 

Sceadwian

Banned
The ADC itself probably, if you average multiple samples together it will decrease. If you want high quality low noise audio you're going to have to use dedicated off chip ADC/DAC's and. Also how are you sampling at 103khz I thought the cutoff frequency for an PIC's ADC was like 30khz?
 

avi

New Member
Hi Ella,

Hi,
Whay do you need such a high sample rate for audio? 20KHz is a maximum human ear can accept. So the 40KHz sampling rate is all you need.

The rate now is fine, I rewrite the code- emitting some redundant lines.
now I facing a new problem of added noise:
my analog input range is positive only (0-Vdd), so I'm using a summing Amp. to add dc level to the audio signal before feeding to the PIC.
it seems that this amp. adds noise which amplified and distort the sound.
currently I'm using a regular OP (LM324) and I'v orderd a special audio amplifier from National SC (LME49721) which I hope to be better and reduce the noise.
maybe you/somebody else have some more ideas for me to ?
TNX,
Avi.
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
The minimum TAD is 1.6uS and with a 20MHz clock you need TAD set to 32 TOSC. You also need to allow time for the holding capacitor to charge - the acquisition time. If you are sampling at 103kHz then you are well outside the limits of the chip and this is the cause of your noise. Your total time should be around 30uS - 10uS acquisition time and 20uS sampling time. This assumes you only sample one signal, if you switch ADC channels then you need to have a longer acquisition time.

Mike.
 

avi

New Member
OK,
acquisition rate now is: 25Khz.
I'm using the PIC18LF452 - so I use 64 TOSC.
and I use only one channel now (Mono).
but there is still large backup noise :mad:


Thanks for your help, I really need it.

The minimum TAD is 1.6uS and with a 20MHz clock you need TAD set to 32 TOSC. You also need to allow time for the holding capacitor to charge - the acquisition time. If you are sampling at 103kHz then you are well outside the limits of the chip and this is the cause of your noise. Your total time should be around 30uS - 10uS acquisition time and 20uS sampling time. This assumes you only sample one signal, if you switch ADC channels then you need to have a longer acquisition time.

Mike.
 

avi

New Member
This is the code I cuurently use:

Tx:

ADCON1=0x40; //0x84 Left justified,Fosc/64
TRISC=0; TRISA=0xFF;

SPBRG=20;//baud rate =57600
TXEN=1;// Transmit Enable
CSRC=1;//master mode
TX9=0;//8 bit
SYNC=0;// asynchronous mode
BRGH=1;// high speed
TX9D=0;//parity bit
SPEN=1;//serial port enable
RC0=0;
do
{
ADCON0=0x85; //start new conv.
TXREG=t; // tx previous data
while(GODONE); // check if conv. ended
t=ADRESH;
RC0=!RC0;
}
while(1);
--------------------------------------------
Rx:
TRISD=0; TRISB=0;

SPBRG=20;//baud rate =57600
CSRC=1;//master mode
RX9D=0;//9 bit
SYNC=0;// asynchronous mode
BRGH=1;// high speed
SPEN=1;//serial port enable
SREN=0;// disable syncrony
CREN=1;// Receive enable
PORTD=0;
RB0=1;

do
{
RB0=0; // DAC-WR
//while(!RCIF);// status bit fifo
PORTD=RCREG;

for (d=0;d<4;d++); //delay
RB0=1; //DAC-WR

}
while(1);
 

Pommie

Well-Known Member
Most Helpful Member
If you have your baud rate at 57k then the most you can transmit is that rate divided by ten. This is because the data stream consists of 8 data bits plus start and stop bits. So the most you can transmit via RS232 is around 5k bytes per second. This will cause lots of noise as you will be getting lots of transmission errors. To get anywhere near the rate you require you need to be in excess of 250kBaud.

Mike.
 

Sceadwian

Banned
Uncompressed audio is.... bulky.
 

smanches

New Member
I'm not sure how bad the noise is, but you will also get internal noise from the processor when using PICs. It's documented on their site, and I've experienced it myself. No way at all to avoid it except to use an external ADC.
 

Sceadwian

Banned
AVR's can turn the CPU's off while doing ADC conversions. Not sure how much noise that actually removes.
 

smanches

New Member
PIC's can too, but only until memory is full. Still have to wake up at some point to transmit the data.

Wonder if you could do a completely DMA driven ADC->Memory->UART? Hmmm...
 

Sceadwian

Banned
Sure you could. Would not want to try it =)
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top