shubham_k
Newbie level 3
- Joined
- Jan 1, 2013
- Messages
- 4
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,326
Hi,
I am making a project where I have to sense the light in the bot.
The analog output from the LDR must be converted to the digital form for the microcontroller to read.
Without using a comparator,I can do that.But for the same, I have to apply ADC conversion in my program.
I am posting the program.Plz check where's the error and why I am not getting the output.
I am using ATMega8 microcontroller.I am using PC1,PC2 and PC4 ports for sensor outputs.
These are also the ADC ports.This is the reason I am using these C ports.
PC0 to PC 5 are the ADC ports in ATMega8.
I am using 3 LDR sensors.
I am making a project where I have to sense the light in the bot.
The analog output from the LDR must be converted to the digital form for the microcontroller to read.
Without using a comparator,I can do that.But for the same, I have to apply ADC conversion in my program.
I am posting the program.Plz check where's the error and why I am not getting the output.
I am using ATMega8 microcontroller.I am using PC1,PC2 and PC4 ports for sensor outputs.
These are also the ADC ports.This is the reason I am using these C ports.
PC0 to PC 5 are the ADC ports in ATMega8.
I am using 3 LDR sensors.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 //there are 5 adc ports in atmega8( PC0 to PC5) //I am using PC1,PC2 and PC4 for connecting the LDR sensors #include <avr/io.h> #include<util/delay.h> int a[3]; //Array to store the sensor voltages int main(void) { DDRC=0b0000000; //Set port A to input DDRB=0b00011110; //Set port B to output while(1) { ADMUX=0b00101000; int i; for(i=1;i<5;i++) //Read values of the inputs // PC1-centre,PC 2-left, PC4-right { ADCSRA=0b00011111; while(ADCSRA&(1<<ADSC)); ADMUX++; a[i]=ADCH; //Values stored in ADCH transferred to the array } if(a[2]>a[4] && a[2]>a[1]) //left sensor highest PORTB=0b00000010; //Turn left else if(a[4]<a[1] && a[1]>a[2]) //Centre sensor highest PORTB=0b00010010; //Go straight else if(a[4]==a[1]) //Any two sensors getting same value, then go straight PORTB=0b00010010; else if(a[1]==a[2]) PORTB=0b00010010; else if(a[4]>a[1] && a[4]>a[2]) //Right sensor highest PORTB=0b00010000; //Turn right } return 0; }
Last edited by a moderator: