Professional Documents
Culture Documents
Bit
Bit
* test3.cpp
*
* Created: 14/10/2020 9:19:04 AM
* Author : Khanh
*/
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
volatile uint16_t adc_0,adc_1;
void ADC_init()
{
ADCSRA|= (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
uint16_t ADC_read(unsigned char chnl){
ADMUX|=(1<<REFS0)|(chnl &0x07);
ADCSRA|= (1<<ADSC);
while((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
_delay_ms(10);
return ADCW;
}
int main(void)
{
DDRA = 0x00;
DDRB = 0xff;
DDRC = 0xFF;
PORTB=0x00;
PORTC=0x00;
ADC_init();
while (1)
{
adc_0=ADC_read(0);
if (adc_0>512)
PORTB= 0xFF;
if(adc_0<=512)
PORTB = 0x00;
_delay_ms(100);
adc_1=ADC_read(1);
if (adc_1>300)
PORTC=0x00;
if (adc_1<=300)
PORTC = 0xFF;
_delay_ms(100);
}
}