ADC reading problem in ATMega128

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!

I'm using ADC channel 1 to read a pin voltage, but my readings are bad, i.e., the ADC reads a value above what should be (reads 2.38V when should be 1.71V).

I tried to change the voltage reference from internal 2.56V to AVCC (3.3V) but the results were exactly the same (2.38V)

I have the ATMega128L at 3.3V and at 7.37MHz. I'm also using jtagice to program and debug. I'm working on linux: avarice and avr-gdb.

Here is the code I'm using:

int main(void)
{
DDRF = 0x00;

ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);    
ADMUX = 1;
ADMUX |= (1<<REFS1) | (1<<REFS0) | (1<<ADLAR);
 
ADCSRA |= (1<<ADSC); 
while ( ADCSRA & (1<<ADSC) ) {
	;
}
 
result = ADCH;
}

My fuse bits are 0xff11ff.

Now the odd thing is that I've tried this same code, same fuse bits same ATMega but at 5V and 6MHz and the reading was ok.

I don't know what else to try...

Any help?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The answer is probably yes but...

When you calculate the voltage did you use 2.56V as the Vref?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since I'm using ADLAR = 1, I'm only reading the ADCH and I've got these results:

Vref = 2.56V -> ADCH = 238 (decimal) (238 / 255 * 2.56 = 2.38V)
Vref = 3.3V -> ADCH = 192 (decimal) which results in almost the same voltage (192 / 255 * 3.3V = 2.48V)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If that indeed is the entire program, then I'd say the conversion results are meaningless--discard the results of the first conversion after starting up the ADC or changing reference.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch:
This is just a test I'm doing, not the real code I'm working on.
I followed your advice and I slightly changed the code to this:

int main(void)
{
DDRF = 0x00;

ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);   
ADMUX = 1;
ADMUX |= (1<<REFS1) | (1<<REFS0) | (1<<ADLAR);
 
ADCSRA |= (1<<ADSC);
while ( ADCSRA & (1<<ADSC) ) {
   ;
}
 
result = ADCH;

ADCSRA |= (1<<ADSC);
while ( ADCSRA & (1<<ADSC) ) {
   ;
}
 
result = ADCH;

} 

I'm not sure if I need to wait some time between successive readings, but I'm assuming it's not necessary. Correct me if I'm wrong, please.

Anyway, the results were exactly the same for both readings.
I put the last reading in a while loop and I got the same values (as posted previously).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe try it at 6mhz/3.3 volts, or 7.3mhz/5 volts to narrow it down a bit?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dksmall:
I'll try that tomorrow and then I'll tell you the results, but even if I get different results the two boards are completely different and it would take some time to check what might be the cause.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

logic_bit wrote:
dksmall:
I'll try that tomorrow and then I'll tell you the results, but even if I get different results the two boards are completely different and it would take some time to check what might be the cause.

I was led to believe you changed the operating voltage and crystals on the same board. Now you indicate one kind of board works, and another kind of board doesn't work. What else is different between these boards? Are you still reading ADC channel 1? Do you know the JTAG port will interefere with the ADC channels 4-7?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm sorry if I misled you :oops:

The PORTF connections are the same onboth boards: I'm reading channel 1 (PF1).
PF0, 2, 3 are not connected and PF4, 5, 6, 7 are connected to the jtag interface.

The other ports have different connections but I think they won't interfere with ADC, at least they are not interfering with anything else.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I'm not sure if I need to wait some time between successive readings, but I'm assuming it's not necessary. Correct me if I'm wrong, please.

No delay is needed, as soon as ADSC goes low and you have read the result you can start another conversion by making ADSC high.

Maybe a stupid question but you don't have pull-ups enabled on ADC input pins, do you?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there anything connected to AREF? Hopefully the recommend capacitor when using the internal reference?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lennart wrote:
No delay is needed, as soon as ADSC goes low and you have read the result you can start another conversion by making ADSC high.
Thanks! As I thought!

Lennart wrote:
Maybe a stupid question but you don't have pull-ups enabled on ADC input pins, do you?
I don't have because I'm not writing to PORTF any value, so default is 0 which means pull-ups turned off if pins are input (DDRF = 0x00).
Anyway, if I had pull-ups on I think I'd get a lower reading, not a higher one...

dksmall wrote:
s there anything connected to AREF? Hopefully the recommend capacitor when using the internal reference?
I've tried both situations on AREF pin:
-with a 100nF capacitor to GND
-not connected
Both results were the same and wrong (the 238).
I don't think the difference would be significant since the capacitor is only to make the reading more imune to noise.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you tried to connect a pot to ADC1 to see if there is a changing value when turning the pot (connected to AVcc at one end and GND at other end)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lennart wrote:
Have you tried to connect a pot to ADC1 to see if there is a changing value when turning the pot (connected to AVcc at one end and GND at other end)
I've been doing some readings and take a look at the results. Curious...

Vcc = 3.3V

Reading: 159 (decimal)
Real voltage (Vreal): 1.14V
Vref real: 2.68V
Vref calculated (Vreal*255/Reading): 1.82V
Vref calculated / Vref real = 0.68V

the other readings:
238, 1.70, 2.68, 1.82, 0.68
128, 1.14, 3.33, 2.27, 0.68
192, 1.70, 3.33, 2.26, 0.68

Isn't it interesting that the relationship between Vref calculated and real is always 0.68?

Can we get a conclusion from these results?

Maybe I'll use this constant and correct the readings I get so I can reach the real voltage...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi logic_bit,
Is that your RTD sensor board and the mico-controller mother board are comman grounded?
if not it will give error like this.....
If, which ever digital values u r getting is double the value than expected then there will be some problem in ur RTD board's wheatstone bridge circuit....
for that just through coding 'Logical Shift Right' the result u got....
There is one more chance of the port you used is not funtioning well..
Change the pin or IC and check with the same crystal speed...

All the Best..
M.SRINIVASAN.

ATMEL--Heart Beat
Nothing Impossible

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just have found the problem.

I guess the signal connected to the ADC had an impedance of ~400kOhm. The datasheet recomend 10kOhm or less.
I was so concentrated on the micro that I forgot to chek the input signal source...

Now it's fixed!

Thank you all anyway!