Home Recent changes

Week 10

My Atmega328 board will be used in the robot to detect what is around. For this I will use 8 IR sensors by Sharp: GP2D120 (Bought from sparkfun) To read data from them I use ADC on AVR microcontroller. Here is the code:

And the setup in the robot:

rangefinders_setup.png

Terminal output looks something like this [8 bit values from each ADC]:

[176] [067] [234] [245] [190] [089] [132]

[161] [067] [230] [230] [191] [106] [121]

And here is the code for ADC:

uint8_t adc_result = 0; int i;

void tim0_init(void) {

	TCNT0=0;
	OCR0A=99;
	TCCR0A |=(1<<COM0A0)|(1<<WGM01);

}

void tim0_start(void) {

	TCCR0B |=(1<<CS01);

}

void tim0_stop(void) {

	TCCR0B &=~(1<<CS01);
	TIMSK0 &=~(1<<OCIE0A);

}

void adc_init() {

	ADMUX |= (1<<REFS0)|(1<<ADLAR);
	ADCSRA |= (1<<ADPS2)|(1<<ADPS0)|(1<<ADATE)|(1<<ADIE)|(1<<ADEN);
	ADCSRB |= (1<<ADTS1)|(1<<ADTS0);

}

void adc_ch_set(uint8_t ADCchannel) {

	ADMUX = (ADMUX & 0xF0) | (ADCchannel & 0x0F);
    while(!(ADCSRA & (1<<ADIF))); // wait while conversion is performed

}

void adc_start(void) {

	ADCSRA |= (1<<ADSC);

} void adc_disable(void) {

	ADCSRA &= ~((1<<ADEN)|(1<<ADIE));

}

ISR(ADC_vect) {

	TIFR0=(1<<OCF0A);

}

void adc_test() {

	adc_init();
	tim0_init();
	tim0_start();
	adc_start();
	sei();
	while(1)
	{
		  printf("ADC ");
		  adc_ch_set(0);
		  printf("[%03u]", ADCH);
		  adc_ch_set(1);
		  printf("[%03u]", ADCH);
		  adc_ch_set(2);
		  printf("[%03u]", ADCH);
		  adc_ch_set(3);
		  printf("[%03u]", ADCH);
		  adc_ch_set(4);
		  printf("[%03u]", ADCH);
		  //adc_ch_set(5);
		  //printf("[%u]", ADCH);
		  adc_ch_set(6);
		  printf("[%03u]", ADCH);
		  adc_ch_set(7);
		  printf("[%03u]", ADCH);
		  printf("\r\n");
	}

}