0% found this document useful (0 votes)
21 views1 page

Lat 8

This code is measuring light intensity with an LDR sensor and voltage from a variable resistor. It initializes the ADC, LCD display, and I/O ports. It then enters a loop that reads the ADC channels for the LDR and VR, displays the values on the LCD, and turns an LED on or off depending on which value is higher. This provides a simple light-activated switch controlled by the LDR and VR.

Uploaded by

teknis komp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

Lat 8

This code is measuring light intensity with an LDR sensor and voltage from a variable resistor. It initializes the ADC, LCD display, and I/O ports. It then enters a loop that reads the ADC channels for the LDR and VR, displays the values on the LCD, and turns an LED on or off depending on which value is higher. This provides a simple light-activated switch controlled by the LDR and VR.

Uploaded by

teknis komp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <mega16.

h>
#include <delay.h>
#include <alcd.h>
#include <stdio.h>

int ADC_LDR = 0;
int ADC_VR = 0;
char tampil[32];

#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR))

unsigned int read_adc(unsigned char adc_input)


{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}

void main(void)
{
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) |
(1<<ADPS1) | (0<<ADPS0);
SFIOR=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);

lcd_init(16);
DDRD = 0b00000011; // output ke buzzer & input push button
PORTD = 0b11111100; // set push button level high
DDRB.3 = 1; // set output ke backlight LCD
PORTB.3 = 1; // aktifkan backlight
lcd_clear();
DDRA.7 = 1;
PORTA.7 = 0;
while (1)
{
ADC_LDR = read_adc(0) ;
sprintf(tampil,"ADC LDR = %d", ADC_LDR);
lcd_gotoxy(0,0);
lcd_puts(tampil);
ADC_VR = read_adc(1) ;
sprintf(tampil,"ADC VR = %d", ADC_VR);
lcd_gotoxy(0,1);
lcd_puts(tampil);

if (ADC_LDR > ADC_VR)


PORTA.7 = 1;
else
PORTA.7 = 0;
delay_ms(200);
lcd_clear() ;
}
}

You might also like