Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include<util/delay.h>
- #include<stdlib.h>
- char result[5];
- void init_ADC()
- {
- ADMUX |= 1<<REFS0;
- ADMUX |= 1<<ADLAR;
- ADCSRA |= 1<<ADEN;
- ADCSRA |= 1<<ADIE;
- //ADCSRA |= 1<<ADFR;
- ADCSRA |= 1<<ADPS2; //prescalar factor of 16
- }
- int ReadADC(uint8_t ch)
- {
- //Select ADC Channel ch must be 0-7
- ch=ch&0b00000111;
- ADMUX|=ch;
- //Start Single conversion
- ADCSRA |= (1<<ADSC);
- //Wait for conversion to complete
- while(!(ADCSRA&(1<<ADIF)));
- ADCSRA|=(1<<ADIF);
- return(ADC);
- }
- int main()
- {
- DDRC = 0×00;
- //sei();
- init_ADC();
- int x1,x2,x3,x4;
- DDRB = 0xFF;
- DDRD = 0xFF;
- PORTB= 0x00;
- _delay_ms(100);
- while(1)
- {
- x1=ReadADC(2); //forward PINC2
- x2=ReadADC(3); //right PINC3
- x3=ReadADC(4); //left PINC4
- x4=ReadADC(5); //BACK PINC5
- if(x1<512)
- {
- PORTB = 0X0F;
- PORTD = 0b00010100;
- }
- if(x2<512)
- {
- PORTB = 0X0F;
- PORTD = 0b00100100;
- }
- if(x3<512)
- {
- PORTB = 0X0F;
- PORTD = 0b00011000;
- }
- if(x4<512)
- {
- PORTB = 0X0F;
- PORTD = 0b00101000;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement