#include <util/delay.h>
#define ADC_VREF_TYPE 0x60
unsigned char buffer=0;
// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=(adc_input & 0x07) | (ADC_VREF_TYPE & 0xff);
_delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}
int main (void)
{
DDRA=0b00000000;
PORTA=0b00000000;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: None
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
while(1)
{
buffer=read_adc(0);
if(buffer<127){DDRC=1;PORTC=1;}
else if(buffer>127){DDRC=1;PORTC=0;}
}
}
semoga bermanfaat
Tidak ada komentar:
Posting Komentar