Society of Robots - Robot Forum

Software => Software => Topic started by: ytz on November 12, 2009, 03:49:39 PM

Title: 50$ Robot atmega328p a2d conversion
Post by: ytz on November 12, 2009, 03:49:39 PM
Hi, I'm new to this stuff and trying to build the 50$ Robot.

I'm using ATmega328P and Serial Dongle Programmer.



Before building the board I hooked everything up to a breadboard to see if everything would work so far and because the servos haven't arrived yet I made a small change in Photovore_v1.c to have the led go on or off depending on which photoresistor recieved more light. What happened was that the led just stayed on permenantly.

After trying all kinds of things it seems somehow starting the a2d conversion makes the program start from the beginning (or maybe resets the controller?). Is this possible?
What could I be doing wrong?

Here is the last code I used and pictures of the circuit.

The leds were supposed to go on one at a time, then together, and stay on. What I get is a continuous blinking back and forth.
The problem seems to be caused by the line:

sbi(ADCSRA, ADSC); // start conversion




Code: [Select]
#include "SoR_Utils.h" //includes all the technical stuff


int main(void)
{
int sensor_left=0;//left photoresistor
int sensor_right=0;//right photoresistor


configure_ports();
a2dInit();
// a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
// a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage
/**************************************************/

/*********ADD YOUR CODE BELOW THIS LINE **********/
LED_off();
LEDy_on();
delay_cycles(11500);
LEDy_off();
LED_on();
delay_cycles(11500);
LED_off();
delay_cycles(11500);


//moved a2dconvert10bit to here to help debug
unsigned char ch = 4;
volatile unsigned char a2dCompleteFlag = FALSE; // clear conversion complete flag

outb(ADMUX, (inb(ADMUX) & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK)); // set channel
cbi(ADCSRA, ADIF); // clear hardware "conversion complete" flag
cbi(PRR, PRADC);
sbi(ADCSRA, ADSC); // start conversion

LEDy_on(); //lighting up both leds then delaying - to find the problematic line
LED_on();
delay_cycles(11500);

//while(!a2dCompleteFlag); // wait until conversion complete
// while( bit_is_clear(ADCSRA, ADIF) ); // wait until conversion complete
while( bit_is_set(ADCSRA, ADSC) ); // wait until conversion complete

// CAUTION: MUST READ ADCL BEFORE ADCH!!!
unsigned short a2dConvert10bit = (inb(ADCL) | (inb(ADCH)<<8)); // read ADC (full 10 bits);

unsigned char a2dConvert8bit= a2dConvert10bit>>2; // return ADC MSB byte

        sensor_left=a2dConvert8bit;


while(1)
{

delay_cycles(22500);
}
/*********ADD YOUR CODE ABOVE THIS LINE **********/

return 0;
}




(http://www.societyofrobots.com/robotforum/index.php?action=dlattach;topic=9587.0;attach=3979;image)

(http://www.societyofrobots.com/robotforum/index.php?action=dlattach;topic=9587.0;attach=3981;image)
Title: Re: 50$ Robot atmega328p a2d conversion
Post by: Webbot on November 12, 2009, 10:27:24 PM
After trying all kinds of things it seems somehow starting the a2d conversion makes the program start from the beginning (or maybe resets the controller?). Is this possible?
What could I be doing wrong?

Yes this is possible - it is normally caused by an interrupt which doesn't have a matching entry in the hardware interrupt vector list.
So it may be that your processor is signalling an 'ADC complete' interrupt but there is no entry in the vector list - in which case the default is to start the program from the beginning again.
Title: Re: 50$ Robot atmega328p a2d conversion
Post by: Admin on November 14, 2009, 10:15:31 PM
Any reason you aren't using the default $50 Robot code? I mean, there is no need to bother/understand the ADC code . . . :P
Title: Re: 50$ Robot atmega328p a2d conversion
Post by: ytz on November 23, 2009, 02:44:39 PM
Any reason you aren't using the default $50 Robot code? I mean, there is no need to bother/understand the ADC code . . . :P

I did start with the default code, making only the minor changes referred to on the "Microcontroller Upgrading" page. Once I realized that I wasn't receiving any response from my photoresistor I started to modify the code to try to find out exactly what was causing the problem.