justinjohney
Member level 1
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
int adc_result=0;
int ch=0;
int main (void)
{
//set portC as input and enable the pull up resistor
DDRC=0x00;
PORTC=0xFF;
//set up port D 0-5 as output& C 6,7 as input
DDRD=0x3F;
// Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
// Set ADC to Free-Running Mode
ADCSRB &=~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)) ;
// Enable ADC
ADCSRA |= (1 << ADEN);
// Enable ADC Interrupt
//ADCSRA |= (1 << ADIE);
// Enable Global Interrupts
//sei();
// Loop Forever
while(1)
{
// Set ADC reference to AREF
// Left adjust ADC result to allow easy 10 bit reading
//select adc channel 1
ADMUX=0x00;
for(ch=0;ch<3;ch++)
{
// Start A2D Conversions
ADCSRA |= (1 << ADSC);
//Read the adc result
adc_result=ADCW;
_delay_ms(10);
if(adc_result>970)
{
if(ch==0)
{
PORTD1=1;
_delay_ms(50);
if(adc_result>970)
{
PORTB4=1;
}
}
else if(ch==1)
{
PORTD2=1;
_delay_ms(50);
if(adc_result>970)
{
PORTB4=1;
}
}
else(ch==2)
{
PORTD2=1;
_delay_ms(50);
if(adc_result>970)
{
PORTB4=1;
}
}
ADMUX++;
}
}
}
atmega168