Author Topic: ADC always shows 255  (Read 2763 times)

0 Members and 1 Guest are viewing this topic.

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
ADC always shows 255
« on: February 13, 2009, 08:30:57 AM »
Hi
I've been working on a program, that turns on a led, when it gets dark.

For an unknown reason, the adc always returns 255.

I'm using an ATTiny26. The sensor is connected to PA1 (ADC1), the output LED to PA2.
There are also LEDs for debugging, connected to PB0-PB6 (the avr restarts if I connect it to PB7).

Code: [Select]
#include <avr/io.h>

int main(){
DDRA = 0b00000100;
DDRB = 0xff;


  ADCSR |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128
ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading

ADMUX |= (1 << MUX0); // Set ADMUX to sample ADC1

ADCSR |= (1 << ADFR);  // Set ADC to Free-Running Mode
ADCSR |= (1 << ADEN);  // Enable ADC
ADCSR |= (1 << ADSC);  // Start A2D Conversions

unsigned char sensor=0;

while(1){
sensor = ADCH;
PORTB = sensor;

if(sensor > 122){
PORTA=0x04;
} else {
PORTA=0x00;
}

}

}

Thanks for your help.
« Last Edit: February 13, 2009, 10:47:44 AM by DemonDomen »

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: ADC always shows 255
« Reply #1 on: February 13, 2009, 10:51:01 AM »
have you connected the Vref somewhere???
It's a common source of such problems???
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: ADC always shows 255
« Reply #2 on: February 13, 2009, 11:14:57 AM »
Does the adc continuously read? if not then youve only told the adc module to read once. everytime you copy the register then you are only copying the initial reading.


try putting

ADCSR |= (1 << ADSC);  // Start A2D Conversions

inside the while loop

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #3 on: February 13, 2009, 04:09:56 PM »
@TrickyNekro: Yes, it's connected to Vcc.

@paulstreat: I will try that in the morning (it's 11 pm local time) and tell you if it works.
« Last Edit: February 13, 2009, 04:13:08 PM by DemonDomen »

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #4 on: February 14, 2009, 02:40:06 AM »
Does the adc continuously read? if not then youve only told the adc module to read once. everytime you copy the register then you are only copying the initial reading.


try putting

ADCSR |= (1 << ADSC);  // Start A2D Conversions

inside the while loop

Tried it, but it still doesn't work. I think it is reading continuously.

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: ADC always shows 255
« Reply #5 on: February 14, 2009, 03:38:44 AM »
mmmmm, Try setting PortB as input....
This with other words....

DDRB = 0x00

Also try testing every ADC pin with a pot....
I'm having a though right now....
Just check if the ATMEGA26 has two ADCs....
One 8 channel and one 2 channel....
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #6 on: February 14, 2009, 04:28:13 AM »
I'm using the ATTiny26. It does have a 7 channel adc and a 4 channel adc, but I don't see any problem with that.

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: ADC always shows 255
« Reply #7 on: February 14, 2009, 04:51:49 AM »
Indeed....
You have two registers for two ADCs
check it out....
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #8 on: February 14, 2009, 05:09:04 AM »
Tried them all out. I also tried setting the whole PORTB as input. There isn't any change. Where ever I try, I still get the same output.

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: ADC always shows 255
« Reply #9 on: February 14, 2009, 05:13:48 AM »
Lets try to rule out the hardware causes first...

Presumably you are using a photo resistor. Do you have this running through a voltage divider circuit?

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #10 on: February 14, 2009, 05:35:24 AM »
Yes, first the current goes through a resistor, then through the photo resisotor and connect to ground. The link between the resistor and photo resistor is connected to the avr.

Offline Kirk

  • Jr. Member
  • **
  • Posts: 44
  • Helpful? 2
Re: ADC always shows 255
« Reply #11 on: February 14, 2009, 07:58:18 AM »
What does a voltmeter read on the output of the detector?
Kirk

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #12 on: February 14, 2009, 08:28:59 AM »
From 0.7V to 4V, depending on the light. At the moment, it's about 3 V.

Offline Kirk

  • Jr. Member
  • **
  • Posts: 44
  • Helpful? 2
Re: ADC always shows 255
« Reply #13 on: February 14, 2009, 06:54:57 PM »
"ADMUX |= (1 << MUX0); // Set ADMUX to sample ADC1"

I did not check the data sheet,  but are you sure that you are reading the correct channel?
This is a mistake that I have made.
Kirk

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #14 on: February 15, 2009, 03:56:25 AM »
Well, I checked it again and it looks correct. The MUX pins have to be set to 0001 for ADC1.
Could anyone doublecheck? The main problem is, that I'm probably wrong somewhere.

Offline DemonDomenTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC always shows 255
« Reply #15 on: February 15, 2009, 11:47:25 AM »
Ok, I solved it. I was checking the datasheet and noticed, that the voltage reference is set to AREF. Then I checked my schematic and noticed, that power is connected to AVCC. AREF was always left unconnected.

 


Get Your Ad Here