You are on page 1of 3

// ALDI SUYANA

// DIMAS RADITYA

1006771554
1006771573

#include <mega16.h>
#include <delay.h>
#include <alcd.h>
#include <stdlib.h>
#define motor_CW PORTD.0
#define motor_CCW PORTD.1
int x;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
x=x+1;
if (x==1)
{
motor_CW=0;
motor_CCW=0;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("counter 1 aktif");
PORTB=0b00001001;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
}
else if (x==2)
{
motor_CW=0;
motor_CCW=0;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("counter 2 aktif");
PORTB=0b00001010;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
PORTB=0b00001010;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
}
else if (x==3)
{
motor_CW=0;
motor_CCW=0;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("counter 3 aktif");
PORTB=0b00001100;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
PORTB=0b00001100;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
PORTB=0b00001100;
delay_ms(1000);

PORTB=0x00;
delay_ms(1000);
x=0;
};
/*motor_CW=0;
motor_CCW=0;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("interupt aktif");
PORTB=0xff;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
PORTB=0xff;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);*/
}
#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);
x=0;
PORTB=0x00;
DDRB=0xff;
PORTD=0b00000100;
DDRD=0b00000011;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x40;

// 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;
// Global enable interrupts
#asm("sei")
while (1)
{
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)
{
motor_CW=0;
motor_CCW=0;
PORTB=0b00001001;
lcd_gotoxy(0,1);
lcd_putsf("minimum(gelap)");
}
else if (LDR>400&LDR<600)
{
PORTB=0b00000010;
motor_CW=1;
motor_CCW=0;
lcd_gotoxy(0,1);
lcd_putsf("medium(redup)");
}
else
{
PORTB=0b00000100;
motor_CW=0;
motor_CCW=1;
lcd_gotoxy(0,1);
lcd_putsf("maximum(terang)");
};
delay_ms(1000);
}
}

You might also like