Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <mega16.

h>
#include <delay.h>
#include <alcd.h>
#include <stdlib.h>
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
void main(void)
{
int a,b,c,d,LDR;
char nilai[4],min[3],t[3],maks[4];
lcd_init(16);
// ADC initialization
// ADC Clock frequency: 86.400 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA7;
SFIOR&=0x1F;
PORTB=0x00;
DDRB=0xff;
while(1)
{

b=0;
c=0;
d=1000;
for (a=1;a<11;a++)
{
lcd_gotoxy(11,0);
itoa(a,t);
lcd_puts(t);
LDR=read_adc(0);
delay_ms(1000);
b=LDR+b;
if(c<LDR)
{c=LDR;}
if (d>LDR){d=LDR;}
}

lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("LDR:");
b=b/10;
itoa(b,nilai);
lcd_gotoxy(5,0);
lcd_puts(nilai);
lcd_gotoxy(0,1);
lcd_puts("maks:");
itoa(c,maks);
lcd_gotoxy(5,1);
lcd_puts(maks);
delay_ms(300);
lcd_gotoxy(9,1);
lcd_puts("min:");
itoa(d,min);
lcd_gotoxy(13,1);
lcd_puts(min);
delay_ms(1000);
}
}
/*lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("LDR: ");
LDR=read_adc(0);
itoa(LDR,nilai);
lcd_gotoxy(5,0);
lcd_puts(nilai);
if (LDR<200)
{
PORTB=0b00001001;
lcd_gotoxy(0,1);
lcd_putsf("minimum(gelap)");
}
else if (LDR>200&LDR<400)
{
PORTB=0b00000010;
lcd_gotoxy(0,1);
lcd_putsf("medium(redup)");
}
else
{
PORTB=0b00000100;
lcd_gotoxy(0,1);
lcd_putsf("maximum(terang)");
};
delay_ms(1000);
}
}

You might also like