well i made this program for the line following bot i set the adc's prescaler at 8 and timers prescaler at 8 and also adc's voltage reference to AVCC (iam not using crystal and my mcu is atmega16 running at 1mhz) now when i loaded program i tested my pwm channels and i was getting around 3.78 v its cool i needed that much but the problem is i did not connect and sensors so the adc's must be getting 0 v reading but according to my program if i get a value more than 98(nearly 1.5v) i should get pwm of 3.78v but without the adc i shouldnt get any pwm but here iam getting ...can u tell me what is the problem?
?
PS:i havent connected aref and gnd with a capacitor do u think that is a problem?? also my adc is in auto trigger mode and when i say motor left2 i mean iam turning more to the left than iam turning in motorleft1.....
for better reference i have pasted my code
/* normal header files included*/
# include<stdio.h>
# include<avr/io.h>
# include<util/delay.h>
//defining pwm values for different level of input adc values left1,left2,right1,right2 specify how much the bot should turnleft2>left1*/
# define motorleft1 127
# define motorleft2 130
# define motorright1 127
# define motorright2 130
# define motorstraight 215
# define threshold 98
void motorleft1turn();
void motorleft2turn();
void motorright1turn();
void motorright2turn();
void motorallstraight();
void timerallinit();
void adcinitialize();
void adcgetvalue();
void directioninputinitialize();
unsigned int analog1,analog2,analog3;
int main(void)
{
timerallinit();
adcinitialize();
directioninputinitialize();
while(1)
{
adcgetvalue();
if(analog1>98 && analog2>98 && analog3>98)
{
if(analog1<threshold && analog2>threshold && analog3>threshold)
{
motorright1turn();
}
if(analog1<threshold && analog2<threshold && analog3>threshold)
{
motorright2turn();
}
if(analog1>threshold && analog2>threshold && analog3<threshold)
{
motorleft1turn();
}
if(analog1>threshold && analog2<threshold && analog3<threshold)
{
motorleft2turn();
}
if(analog1>threshold && analog2>threshold && analog3>threshold)
{
motorallstraight();
}
}
}
return(1);
}
void timerallinit()
{
TCCR0|=_BV(6)|_BV(3)|_BV(5)|_BV(1);
TCCR2|=_BV(6)|_BV(3)|_BV(5)|_BV(1);
DDRD=0x80;
DDRB=0x08;
}
void adcinitialize()
{
ADCSRA|=_BV(7)|_BV(6)|_BV(5)|_BV(1)|_BV(0);
SFIOR=0x00;
ADMUX=0x60;
analog1=ADCH;
}
void adcgetvalue()
{
ADMUX=0x60;
analog1=ADCH;
_delay_ms(5);
ADMUX=0x61;
analog2=ADCH;
_delay_ms(5);
ADMUX=0x63;
analog3=ADCH;
_delay_ms(5);
}
void motorleft1turn()
{
OCR0=motorleft1;
OCR2=motorstraight;
}
void motorleft2turn()
{
OCR0=motorleft2;
_delay_ms(0.5);
OCR2=motorstraight;
}
void motorright1turn()
{
OCR0=motorstraight;
OCR2=motorright1;
}
void motorright2turn()
{
OCR0=motorstraight;
OCR2=motorright2;
_delay_ms(0.5);
}
void motorallstraight()
{
OCR0=motorstraight;
OCR2=motorstraight;
}
void directioninputinitialize()
{
DDRD|=_BV(6)|_BV(5)|_BV(3)|_BV(2);
PORTD|=(1<<5)|(1<<2);
}