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

// ALDI SUYANA

// DIMAS RADITYA

1006771554
1006771573

#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 LDR;
char nilai[4];
lcd_init(16);
PORTB=0x00;
DDRB=0xff;
PORTD=0x00;
DDRD=0xff;
// 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;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Fast PWM top=0xFF
// OC0 output: Non-Inverted PWM
TCCR0=0x69;
TCNT0=0x00;
OCR0=0x00;
while (1)
{
/* PORTD=0b00000001;
OCR0=0xff;
delay_ms(1000);

PORTD=0b00000010;
OCR0=0xff;
delay_ms(1000);
PORTD=0x00;
PORTB=0b00000111;
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<400)
{
PORTB=0b00000001;
PORTD=0b00000001;
OCR0=0xff*0.7;
lcd_gotoxy(0,1);
lcd_putsf("minimum(gelap)");
}
else if (LDR>400&LDR<600)
{
PORTB=0b00000010;
PORTD=0b00000001;
OCR0=0xff*0.85;
lcd_gotoxy(0,1);
lcd_putsf("medium(redup)");
}
else
{
PORTB=0b00000100;
PORTD=0b00000010;
OCR0=0xff;
lcd_gotoxy(0,1);
lcd_putsf("maximum(terang)");
};
delay_ms(1000);
}
}

You might also like