Society of Robots - Robot Forum
Software => Software => Topic started 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).
#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.
-
have you connected the Vref somewhere???
It's a common source of such problems???
-
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
-
@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.
-
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.
-
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....
-
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.
-
Indeed....
You have two registers for two ADCs
check it out....
-
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.
-
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?
-
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.
-
What does a voltmeter read on the output of the detector?
Kirk
-
From 0.7V to 4V, depending on the light. At the moment, it's about 3 V.
-
"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
-
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.
-
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.