Society of Robots - Robot Forum

Software => Software => Topic started by: DemonDomen on February 13, 2009, 08:30:57 AM

Title: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: TrickyNekro on February 13, 2009, 10:51:01 AM
have you connected the Vref somewhere???
It's a common source of such problems???
Title: Re: ADC always shows 255
Post by: paulstreats 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
Title: Re: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: TrickyNekro 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....
Title: Re: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: TrickyNekro on February 14, 2009, 04:51:49 AM
Indeed....
You have two registers for two ADCs
check it out....
Title: Re: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: paulstreats 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?
Title: Re: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: Kirk on February 14, 2009, 07:58:18 AM
What does a voltmeter read on the output of the detector?
Kirk
Title: Re: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: Kirk 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
Title: Re: ADC always shows 255
Post by: DemonDomen 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.
Title: Re: ADC always shows 255
Post by: DemonDomen 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.