Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

multi channel ADC reading problem

Status
Not open for further replies.

praveenofpersia

Newbie level 2
Joined
Sep 7, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,307
Is there any problem with the following program???
Iam trying to read IR sensor values using ATMega16's ADC
but the values displayed on the LCD doesn't seem to be the values from the sensors..

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<avr/io.h>
#include<avr/delay.h>
#include "lcd.h"
 
 
void adc_init()
{
  ADCSRA|=(1<<ADEN)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);//prescalar of 128
 
   ADMUX|=(1<<REFS0)|(1<<ADLAR);
 
}
 
uint16_t adc_value(uint8_t ch)
{
ADMUX&=~0X0F;
ch&=0x07;
ADMUX|=ch;
ADCSRA|=(1<<ADSC);
while(ADCSRA&_BV(ADSC));
return (ADCH);
}
 
int main()
{
 uint16_t pa0,pa1,pa2,pa3,pa4,pa5,pa6,pa7;
InitLCD(0);
LCDClear();
adc_init();
_delay_ms(50);
while(1)
{
pa0=adc_value(0);
pa1=adc_value(1);
pa2=adc_value(2);
pa3=adc_value(3);
pa4=adc_value(4);
pa5=adc_value(5);
pa6=adc_value(6);
pa7=adc_value(7);
 
LCDGotoXY(0,0);
LCDWriteInt(pa0,3);
_delay_ms(2500);
LCDGotoXY(4,0);
LCDWriteInt(pa1,3);
_delay_ms(2500);
LCDGotoXY(8,0);
LCDWriteInt(pa2,3);
_delay_ms(2500);
LCDGotoXY(12,0);
LCDWriteInt(pa3,3);
_delay_ms(2500);
LCDGotoXY(0,1);
LCDWriteInt(pa4,3);
_delay_ms(2500);
LCDGotoXY(4,1);
LCDWriteInt(pa5,3);
_delay_ms(2500);
LCDGotoXY(8,1);
LCDWriteInt(pa6,3);
_delay_ms(2500);
LCDGotoXY(12,1);
LCDWriteInt(pa7,3);
_delay_ms(2500);
 
_delay_ms(50);
 
 
}
return 1;
}



The values seems to be fluctuating in all channels(the values on each channel seems to start from 000 then increase from there to 255 Confused I don't think it's displaying the sensor values ), even when a white/black paper is placed in front of a IR TX RX pair ... Sad


also please help me in interpreting the values..
 

If you read multiple analog input, maybe you need to put delay after selecting the ADC channel.
Because, the ADC is only have 1, the the input is multiplexing. So, you need to give delay for multiplexing time.
 

if you check page 211 of the datasheet http://www.atmel.com/atmel/acrobat/doc2466.pdf it says
When switching to a differential gain channel, the first conversion result may have a poor accuracy due to the required settling time for the automatic offset cancellation circuitry.
The user should preferably disregard the first conversion result.

EDIT wrong reference because it refers to the differential mode but you still need a delay after changing channel so try the code below

you should be fine if you add a delay after changing the channel

Code:
uint16_t adc_value(uint8_t ch)
{
ADMUX&=~0X0F;
ch&=0x07;
ADMUX|=ch;
[COLOR="#FF0000"]// Delay for the ADC input voltage
_delay_us(10);[/COLOR]
ADCSRA|=(1<<ADSC);
while(ADCSRA&_BV(ADSC));
return (ADCH);
}

Alex
 
Last edited:

I replaced the first few lines of the infinite while loop with the following which incorporates delays...
while(1)
{
for(unsigned short int i=0;i<=7;i++)
{
for(unsigned short int j=1;j<=22;j++)
{ _delay_ms(1);
pa+=adc_value(i);
}
pa/=22;
}

but still the problem isn't solved....
please help..
 
Last edited:

you don't need so high delay, a 10us delay is fine, if this didn't fix the problem then there is something else wrong.
Can you post your schematic?
Are you sure about the output of IR, I would suggest you to use a 5K trimer and feed the ADC from the wiper with a known voltage and check if the result is correct, try with 2-3 different voltages, if this is corrent then try with the IR.
Also have you measured the voltage at the ADC pin with a multimeter (in the circuit with IR), what was the reading?

Alex
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top