Sie sind auf Seite 1von 20

Blink LED

#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=1 State0=1
PORTD=0xFF;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of

// Input Capture Interrupt: Of


// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
PORTD=0xFF;
//delay_ms(1000);
PORTD=0x00;
//delay_ms(1000);
};
}
Shift Right
#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
int temp;
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization

// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;

OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
temp=1<<7;
diberi logika 1
PORTD=~temp;
delay_ms(1000);

//bit ke-7

while (1)
{
// Place your code here
delay_ms(1000);
temp>>=1;
//geser ke
kanan satu kali
PORTD=~temp;
if(temp==0)
{
temp=1<<7;
//bit ke-7
diberi logika 1 kenbali
}

};

Baca tulis Data ke port


#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization

// Func7=In Func6=In Func5=In


Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=P State6=P State5=P
State4=P State3=P State2=P
State1=P State0=P
PORTC=0xFF;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=1 State0=1
PORTD=0xFF;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
//persiapan
#asm("nop");
while (1)
{
// Place your code here
if(PINC.0==0)
{
PORTD=0;
//led nyala
delay_ms(500);
}
if(PINC.1==0)
{
PORTD=0;
//led nyala
delay_ms(500);
PORTD=255;
//led
padam
delay_ms(500);
PORTD=0;
//led nyala
delay_ms(500);
}
PORTD=255;
padam

//led

};

Menampilkan karakter pada


seven segment
#include <mega8535.h>
#include <delay.h>
// Declare your global variables
here
void main(void)
{

// Declare your local variables


here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=1 State0=1
PORTC=0xFF;
DDRC=0xFF;
// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=1 State0=1
PORTD=0xFF;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;

TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;

void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;

// Port C initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=1 State0=1
PORTC=0xFF;
DDRC=0xFF;

while (1)
{
// Place your code here
PORTD.7=1;
//menampilkan
karakter '0' pada 7segmen kiri
PORTD.6=0;
PORTC=0b11000000; //'0'
delay_ms(10);
//delay 10 ms

// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=1 State0=1
PORTD=0xFF;
DDRD=0xFF;

PORTD.7=0;
PORTD.6=1;
//menampilkan
karakter '1' pada 1 segmen
kanan
PORTC=0b11111001; //'1'
delay_ms(10);
//delay 10 ms

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

};
}
Up counter seven segment
#include <mega8535.h>
#include <delay.h>
// Declare your global variables
here
unsigned char bil[10] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x
92,0x82,0xf8,0x80,0x90} ;
int kiri,kanan,i;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of

TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
kiri=0;
kanan=0;
for(kiri=0;kiri<10;kiri++)
//puluhan
{
for(kanan=0;kanan<10;kanan
++)
//satuan
{
for(i=0;i<50;i++)
{
PORTD.7=0;
PORTD.6=1;
//menampilkan
bilangan pada segmen kiri
PORTC=bil[kiri];
delay_ms(5);
//delay 5 ms
PORTD.7=1;
//menampilkan
bilangan pada segmen kanan
PORTD.6=0;
PORTC=bil[kanan];
delay_ms(5);
//delay 10 ms

}
}

}
}

};

Menampilkan karakter
pada matrik led
#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
unsigned char i, col ;
unsigned char nol[8] =
{0x00, 0x00, 0x3c, 0x3c, 0x3c,
0x3c, 0x00, 0x00} ;
// Input/Output Ports
initialization
// Port A initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
i=0;
for (col = 1; col < 128 ;
col=col*2)
{
PORTA = col ;

PORTC = nol[i];
i=i+1;
delay_ms(1);

};

Putaran motor stepper CW


#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization

// Clock source: System Clock


// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
PORTD=0x01;
//CW
delay_ms(500);
PORTD=0x02;
delay_ms(500);
PORTD=0x04;
delay_ms(500);
PORTD=0x08;
delay_ms(500);
};
}
Putaran motor stepper CCW
#include <mega16.h>

#include <delay.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of

// Compare A Match Interrupt:


Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code
here
//CCW
PORTD=0x08;
delay_ms(500);
PORTD=0x04;
delay_ms(500);
PORTD=0x02;
delay_ms(500);
PORTD=0x01;
delay_ms(500);

};

Menampilkan karakter
pada LED
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD Module
functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm

#include <lcd.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=P State6=P State5=P
State4=P State3=P State2=P
State1=P State0=P
PORTD=0xFF;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of

// Compare A Match Interrupt:


Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
//Tambahkan program dibawah
ini pada program utama.
lcd_gotoxy(0,0);
lcd_putsf("== WELCOME ==");
delay_ms(1000);
while (1)
{
// Place your code here
// Pada bagian looping
tambahkan program dibawah
ini
if (PIND.1==1){
lcd_gotoxy(0,0);
lcd_putsf("Silahkan Tekan");
PORTA = 0xFF;
//Led Mati
delay_ms(500);
}
else{
lcd_gotoxy(0,0);
lcd_putsf("Tombol1 ditekan ! ");
PORTA = 0x00;
//Led Nyala
delay_ms(500);

}
};
}
Penggunaan Keypad
#include <mega16.h>
// Alphanumeric LCD Module
functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
// Declare your global variables
here
unsigned char key;
unsigned char keypad( );
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=Out Func3=Out
Func2=Out Func1=Out
Func0=Out
// State7=P State6=P State5=P
State4=1 State3=1 State2=1
State1=1 State0=0
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0xFE;
DDRB=0x1F;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0xFF;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
//
//
//
//

Timer/Counter 0 initialization
Clock source: System Clock
Clock value: Timer 0 Stopped
Mode: Normal top=FFh

// OC0 output: Disconnected


TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
while (1)
{
// Place your code here
key=keypad();
PORTC=~key;
/*if(key=='1');
{
lcd_gotoxy(0,0);

lcd_putsf("keypad = 1");
PORTC=~key;
}
if(key=='2');
{
lcd_gotoxy(0,0);
lcd_putsf("keypad = 2");
PORTC=~key;
}
*/

};

unsigned char keypad( )


{
PORTB = 0b11111100;
if(PINB.5==0) return (0x01);
if(PINB.6==0) return (0x02);
if(PINB.7==0) return ('3');
//==================
======
PORTB = 0b11111010;
if(PINB.5==0) return ('4');
if(PINB.6==0) return ('5');
if(PINB.7==0) return ('6');
//==================
======
PORTB = 0b11110110;
if(PINB.5==0) return ('7');
if(PINB.6==0) return ('8');
if(PINB.7==0) return ('9');
//==================
======
PORTB = 0b11101110;
if(PINB.5==0) return ('*');
if(PINB.6==0) return ('0');
if(PINB.7==0) return ('#');
}
Membaca data dari ir
sensor
#include <mega16.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=P State6=P State5=P
State4=P State3=P State2=P
State1=P State0=P
PORTA=0xFF;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out

// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
if(PINA.0==1)
PORTB=0x00; //led nyala
else PORTB=0xFF;
//led padam
}

};

Blink LED menggunakan


Timer1
#include <mega16.h>

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

//variabel global
bit fl_timer;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s)
initialization
// INT0: Of

// Input/Output Ports
initialization
// Port A initialization

// Timer 1 overflow interrupt


service routine
interrupt [TIM1_OVF] void
timer1_ovf_isr(void)
{
// Reinitialize Timer 1 value
TCNT1H=0xD5;
TCNT1L=0xD0;
// Place your code here
if(fl_timer==1)
{
PORTC=0xf;
fl_timer=0;
}
else
{
PORTC=0x00;
fl_timer=1;
}

// Declare your global variables


here
void main(void)
{
// Declare your local variables
here

// Func7=In Func6=In Func5=In


Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x01;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
On
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x05;
TCNT1H=0xD5;
TCNT1L=0xD0;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;

TCNT2=0; //jika sudah compare


match,set awal lagi
if(++kali==10000)
{
PORTD.0=1;
if(++detik==60)
{
PORTD.0=1;
delay_ms(1000);
PORTD.0=0;detik=0;
lcd_clear();

if(++menit==60)
{ menit=0;
lcd_clear();

// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x04;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
fl_timer=0;
while (1)
{
// Place your code here
};
}
Jam digital menggunakan
timer 2
#include <mega16.h>
#include <delay.h>
#include <stdlib.h>

if(++jam==24)
{
jam=0;
lcd_clear();
}

}
kali=0;
itoa(detik,cdetik);
itoa(menit,cmenit);
itoa(jam,cjam);
lcd_gotoxy(9,0);
lcd_putsf(":");
lcd_gotoxy(10,0);
lcd_puts(cdetik);
lcd_gotoxy(6,0);
lcd_putsf(":");
lcd_gotoxy(7,0);
lcd_puts(cmenit);
lcd_gotoxy(4,0);
lcd_puts(cjam);
lcd_gotoxy(0,1);
lcd_putsf("= Jam Digital =");
//delay_ms(500);
PORTD.0=0;
}

// Alphanumeric LCD Module


functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>

// Declare your global variables


here

//variabel global
unsigned int
kali=0,detik=0,menit=0,jam=0
;
unsigned char
cdetik[10],cmenit[10],cjam[10]
;

// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Timer 2 output compare


interrupt service routine
interrupt [TIM2_COMP] void
timer2_comp_isr(void)
{
// Place your code here

void main(void)
{
// Declare your local variables
here

// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In

// State7=T State6=T State5=T


State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0xf;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 1382.400 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x02;
TCNT2=0x00;
OCR2=0x96;
// External Interrupt(s)
initialization
// INT0: Of

// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x80;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
}

};

Counter
#include <mega16.h>
#include <delay.h>
// Timer 1 overflow interrupt
service routine
interrupt [TIM1_OVF] void
timer1_ovf_isr(void)
{
// Reinitialize Timer 1 value
TCNT1H=0xFF;
TCNT1L=0xFA;
// Place your code here
PORTB.2=0;
PORTB.3=0;
delay_ms(500);
PORTB.2=1;
PORTB.3=1;
delay_ms(500);
PORTB.2=0;
PORTB.3=0;
delay_ms(500);
PORTB.2=1;
PORTB.3=1;
delay_ms(500);
}
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In

// State7=T State6=T State5=T


State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=In Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=P State0=1
PORTB=0xFF;
DDRB=0xFD;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: T1 pin Rising
Edge
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Rising Edge
// Timer 1 Overflow Interrupt:
On
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x47;
TCNT1H=0xFF;
TCNT1L=0xFA;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock

// Clock value: Timer 2 Stopped


// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x04;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
}

};

Interrupt external
#include <mega16.h>
#include <delay.h>
// External Interrupt 0 service
routine
interrupt [EXT_INT0] void
ext_int0_isr(void)
{
// Place your code here
PORTB=0xFF;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
PORTB=0xFF;
delay_ms(1000);
PORTB=0x00;
delay_ms(1000);
}
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In

// State7=T State6=T State5=T


State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=P State6=P State5=P
State4=P State3=P State2=P
State1=P State0=P
PORTD=0xFF;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock

// Clock value: Timer 2 Stopped


// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Of
// INT2: Of
GICR|=0x40;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
};
}
Baca tulis data ke eeprom
#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
eeprom unsigned char
status_port;
unsigned char a;
void main(void)
{
// Declare your local variables
here
PORTA=0xFF;
DDRA=0x00;
PORTC=0x00;
DDRC=0xFF;
a=status_port;
PORTC=~a;
while (1)
{
// Place your code here
if(PINA.0==0)
{
a=a+1;
PORTC=~a;
delay_ms(1000);

}
if(PINA.1==0)
{
a=a-1;
PORTC=~a;
delay_ms(1000);
}
status_port=a;

};
}
Komunikasi serial dengan
komputer
#include <mega16.h>
// Standard Input/Output
functions
#include <stdio.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
unsigned char data_rx;
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In

// State7=T State6=T State5=T


State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters:
8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;

UBRRL=0x47;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
//Bagian Program Utama Kita
printf("\n PROGRAM TEST
KOMUNIKASI SERIAL");
printf("\n KIRIM DATA DARI
KOMPUTER KE ATMEGA16");
printf("\n TERIMA DATA DAN
KEMBALIKAN DATA KE
KOMPUTER");
printf("\n SILAHKAN TEKAN
SEMBARANG TOMBOL
KEYBOARD");
while (1)
{
// Place your code here
data_rx=getchar();
//terima data dari komputer
printf("\n keyboard:
%c",data_rx);
//kirim
kembali data ke komputer

};

Voltmeter Digital
#include <mega16.h>
#include <stdio.h>
#include <delay.h>
// Alphanumeric LCD Module
functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.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 & 0xf);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to
complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables
here
void main(void)
{

// Declare your local variables


here
char lcd_bufer[33];
float vin;
unsigned int temp;
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;

TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 86.400
kHz
// ADC Voltage Reference: AREF
pin
// ADC Auto Trigger Source:
None
ADMUX=ADC_VREF_TYPE &
0xf;
ADCSRA=0x87;
// LCD module initialization
lcd_init(16);
while (1)
{
// Place your code here
temp=read_adc(0);
vin=((float)temp*0.00488);
//vin=(float(temp)*(5v
olt/1024))
lcd_gotoxy(0,0);
lcd_putsf("== Voltmeter
== ");
lcd_gotoxy(2,1);
sprintf(lcd_bufer,"volt:
%0.0001f V ",vin);
lcd_puts(lcd_bufer);
delay_ms(1000);

};
}
Thermometer digital
#include <mega16.h>
#include <stdio.h>
#include <delay.h>
// Alphanumeric LCD Module
functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.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 & 0xf);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to
complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
char lcd_bufer[33];
float suhu;
unsigned int temp;
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In

// State7=T State6=T State5=T


State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 86.400
kHz
// ADC Voltage Reference: AREF
pin
// ADC Auto Trigger Source:
None
ADMUX=ADC_VREF_TYPE &
0xf;
ADCSRA=0x87;
// LCD module initialization
lcd_init(16);
while (1)
{
// Place your code here
temp=read_adc(0);
suhu=((float)temp*0.488);
//suhu=(float(temp)*1
00*(5volt/1024))
lcd_gotoxy(0,0);
lcd_putsf("== SUHU ==
");
lcd_gotoxy(2,1);
sprintf(lcd_bufer,"Suhu:
%.2f C ",suhu);
lcd_puts(lcd_bufer);
delay_ms(1000);

};

Pengaturan intensitas
cahaya led
#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out

// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=P State6=P State5=P
State4=P State3=P State2=P
State1=P State0=P
PORTC=0xFF;
//mengaktifkan pull up resistor
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted
PWM
TCCR0=0x69;
//Mode Fast PWM
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
PORTB=0b00001000;
100% duty cycle */

/*

while(1)
{
delay_ms(10);
if(PINC==0xfe)
/*switch 0*/
{
if(OCR0<0xf)
OCR0++;
}
if(PINC==0xfd) /*switch 1*/
{
if(OCR0>0)
OCR0--;
}
};

}
Pengaturan intensitas
cahaya led otomatis
#include <mega16.h>
#include <delay.h>
// Timer 0 overflow interrupt
service routine
interrupt [TIM0_OVF] void
timer0_ovf_isr(void)
{
// Place your code here
}
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
unsigned int i;
// Input/Output Ports
initialization
// Port A initialization

// Func7=In Func6=In Func5=In


Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted
PWM
TCCR0=0x69;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;

OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x01;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if(OCR0==0xFF)
{
OCR0=0xFF;
delay_ms(1000);
for (i=0;i<255;i++)
{
OCR0--;
delay_ms(10);
};
OCR0=0x00;
delay_ms(1000);
}
else if (OCR0<0xFF)
{
OCR0++;
delay_ms(10);
}

};

Robot mobil
#include <mega16.h>
#include <delay.h>
// Declare your global variables
here
void main(void)
{

// Declare your local variables


here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Ph. correct PWM
top=00FFh
// OC1A output: Inverted
// OC1B output: Inverted
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0xF1;
TCCR1B=0x01;

TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
//PWM untuk mengatur
kecepatan motor DC
OCR1AL=10;
OCR1BL=10;
while (1)
{
// Place your code here
//Gerakan Robot Mobil
menggunakan Gear
//===============
Robot Maju
==================
//motor kiri CCW (maju) ,
//motor kanan CCW (maju)
PORTB.0=1;
PORTB.1=0;
PORTB.2=1;
PORTB.3=0;
delay_ms(2000);
//selama 2 s

//===============
Robot Mundur
==================
//motor kiri CW (mundur) ,
//motor kanan CW (mundur)
PORTB.0=0;
PORTB.1=1;
PORTB.2=0;

PORTB.3=1;
delay_ms(2000);
//selama 2 s
//===============
Robot Belok Kanan
==================
//belok kanan'motor kanan
stop; motor kiri CCW
PORTB.0=1;
PORTB.1=0;
PORTB.2=1;
PORTB.3=1;
delay_ms(2000);
//===============
Robot Belok Kiri
==================
//belok kiri'motor kiri stop;
motor kanan CCW
PORTB.0=1;
PORTB.1=1;
PORTB.2=1;
PORTB.3=0;
delay_ms(2000);
//===============
Robot Berhenti
==================
PORTB.0=1;
PORTB.1=1;
PORTB.2=1;
PORTB.3=1;
delay_ms(2000);
};
}
Robot line follower
#include <mega16.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
// Input/Output Ports
initialization
// Port A initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=0 State6=0
State5=0 State4=0 State3=0
State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization

// Func7=In Func6=In Func5=In


Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In
Func5=Out Func4=Out
Func3=In Func2=In Func1=In
Func0=In
// State7=T State6=T State5=0
State4=0 State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x30;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Ph. correct PWM
top=00FFh
// OC1A output: Inverted
// OC1B output: Inverted
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0xF1;
TCCR1B=0x01;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
//Tambahkan Program berikut
//PWM untuk mengatur
kecepatan motor DC
OCR1AL=10;
OCR1BL=10;
while (1)
{
// Place your code here
if(PINA.0 && PINA.1==1) //
ada sinyal dari IR sensor kiri
dan IR sensor kanan
{
//===============
Robot Maju
==================
//motor kiri CCW (maju) ,
//motor kanan CCW (maju)
PORTB.0=1;
PORTB.1=0;
PORTB.2=1;
PORTB.3=0;
}
if(PINA.0 == 0)
//tidak ada sinyal dari
IR sensor kiri
{
//===============
Robot Belok Kiri
==================
//belok kiri'motor kiri stop;
motor kanan CCW
PORTB.0=1;
PORTB.1=1;
PORTB.2=1;
PORTB.3=0;
}
if(PINA.1 == 0)
//
tidak ada sinyal dari IR sensor
kanan
{
//===============
Robot Belok Kanan
==================
//belok kanan'motor kanan
stop; motor kiri CCW
PORTB.0=1;
PORTB.1=0;
PORTB.2=1;
PORTB.3=1;
}
};
}
Mengendalikan motor servo
#include <mega16.h>

#include <delay.h>
// Declare your global variables
here
void main(void)
{
// Declare your local variables
here
int i;
// Input/Output Ports
initialization
// Port A initialization
// Func7=Out Func6=Out
Func5=Out Func4=Out
Func3=Out Func2=Out
Func1=Out Func0=Out
// State7=1 State6=1
State5=1 State4=1 State3=1
State2=1 State1=1 State0=1
PORTA=0xFF;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of

// Compare A Match Interrupt:


Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s)
initialization
// INT0: Of
// INT1: Of
// INT2: Of
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator
initialization
// Analog Comparator: Of
// Analog Comparator Input
Capture by Timer/Counter 1:
Of
ACSR=0x80;
SFIOR=0x00;
while (1)
{
// Place your code here
for(i=0;i<300;i++)
{
//0 derajat
PORTA.0=1;
delay_us(1100);
PORTA.0=0;
delay_us(18900);
}
//delay_ms(1000);
for(i=0;i<300;i++)
{
//90 derajat
PORTA.0=1;
delay_us(1500);
PORTA.0=0;
delay_us(18500);
}
//delay_ms(1000);
for(i=0;i<300;i++)
{

//180 derajat
PORTA.0=1;
delay_us(1900);
PORTA.0=0;
delay_us(18100);
}
delay_ms(1000);
};
}
Menampilkan jam dan
tanggal
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
// I2C Bus functions
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#include <i2c.h>

rtc_set_date(7,1,13);//set
tanggal: 7 januari 2013
while (1)
{
rtc_get_time(&jam,&menit,&de
tik);
rtc_get_date(&tanggal,&bulan,
&tahun);
lcd_gotoxy(0,0);
sprintf(buf,"Jam:%d:%d:
%d ",jam,menit,detik);
lcd_puts(buf);
lcd_gotoxy(0,1);
sprintf(buf,"Tgl:%d:%d:%d
",tanggal,bulan,tahun);
lcd_puts(buf);
delay_ms(100);
lcd_clear();
};
}

// DS1307 Real Time Clock


functions
#include <ds1307.h>

Mengukur jarak
Menggunakan sensor ping
#include <mega16.h>
#include <delay.h>
#include <stdlib.h>

// Alphanumeric LCD Module


functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

// Alphanumeric LCD Module


functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

void main(void)
{
unsigned char
jam,menit,detik,tanggal,bulan,t
ahun,buf[17];
PORTA=0x00;
DDRA=0x00;
PORTC=0x00;
DDRC=0x00;
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock
initialization
// Square wave output on pin
SQW/OUT: Of
// SQW/OUT pin state: 0
rtc_init(0,0,0);
// LCD module initialization
lcd_init(16);
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("Test");
lcd_gotoxy(0,1);
lcd_putsf("Real Time Clock");
delay_ms(5000);
lcd_clear();
rtc_set_time(1,26,50); //set jam
01:26:50

#define sigout PORTB.0


#define sigin PINB.0
#define dirsig DDRB.0
void ambil_data();
unsigned int
data_timer,dataping;
unsigned char jarak[4];
// Timer 1 overflow interrupt
service routine
interrupt [TIM1_OVF] void
timer1_ovf_isr(void)
{
data_timer=0;
}
void main(void)
{
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
//
//
//
//
//
//
//
//

Timer/Counter 1 initialization
Clock source: System Clock
Clock value: 172.800 kHz
Mode: Normal top=FFFFh
OC1A output: Discon.
OC1B output: Discon.
Noise Canceler: Of
Input Capture on Rising Edge

// Timer 1 Overflow Interrupt:


On
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0x00;
TCCR1B=0x43;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x04;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
lcd_gotoxy(0,0);
lcd_putsf("ALAT UKUR");
lcd_gotoxy(0,1);
lcd_putsf("JARAK");
delay_ms(2000);
lcd_clear();
while (1)
{
ambil_data();
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Jarak=");
lcd_gotoxy(7,0);
itoa(dataping,jarak);
lcd_puts(jarak);
delay_ms(500);
};
}
void ambil_data()
{
dirsig=1;
sigout=1;
delay_us(10);
sigout=0;
dirsig=0;
sigout=1;
while(sigin==0);
TCNT1=0;
data_timer=0;
while(sigin==1);
data_timer=TCNT1;
dataping=data_timer/10;
delay_ms(300);
}
Mengatur warna rgb led
#include <mega16.h>

void main(void)
{
// Port B initialization
// Func7=In Func6=In Func5=In
Func4=In Func3=Out Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T
State4=T State3=0 State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x08;
// Port D initialization
// Func7=In Func6=In
Func5=Out Func4=Out
Func3=In Func2=In Func1=In
Func0=In
// State7=T State6=T State5=0
State4=0 State3=T State2=T
State1=T State0=T
PORTD=0x00;
DDRD=0x30;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted
PWM
TCCR0=0x69;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Of
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt:
Of
// Input Capture Interrupt: Of
// Compare A Match Interrupt:
Of
// Compare B Match Interrupt:
Of
TCCR1A=0xA1;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer(s)/Counter(s)
Interrupt(s) initialization
TIMSK=0x00;
//duty cycle untuk warna
merah 50%, biru 50%, dan
hijau 0%
OCR0=0;
//Register OCR0
untuk mengatur warna hijau
OCR1A=128;
//Register
OCR1A untuk mengatur warna
biru

OCR1B=128;
//Register
OCR1B untuk mengatur warna
merah
while (1)
{

};
}
Pengendalian melalui
bluetooth
#include <mega16.h>
#define
#define
#define
#define
#define
#define
#define

RXB8 1
TXB8 0
UPE 2
OVR 3
FE 4
UDRE 5
RXC 7

#define FRAMING_ERROR
(1<<FE)
#define PARITY_ERROR
(1<<UPE)
#define DATA_OVERRUN
(1<<OVR)
#define
DATA_REGISTER_EMPTY
(1<<UDRE)
#define RX_COMPLETE
(1<<RXC)
// USART Receiver bufer
#define RX_BUFFER_SIZE 8
char
rx_bufer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char
rx_wr_index,rx_rd_index,rx_cou
nter;
#else
unsigned int
rx_wr_index,rx_rd_index,rx_cou
nter;
#endif
// This flag is set on USART
Receiver bufer overflow
bit rx_bufer_overflow;
// USART Receiver interrupt
service routine
interrupt [USART_RXC] void
usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR
| PARITY_ERROR |
DATA_OVERRUN))==0)
{
rx_bufer[rx_wr_index]=data;
if (++rx_wr_index ==
RX_BUFFER_SIZE)
rx_wr_index=0;
if (++rx_counter ==
RX_BUFFER_SIZE)
{
rx_counter=0;

rx_bufer_overflow=1;
};
};
if(data=='a')
{
PORTA.0=1;
}
if(data!='a')
{
PORTA.0=0;
}
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the
USART Receiver bufer
#define
_ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_bufer[rx_rd_index];
if (++rx_rd_index ==
RX_BUFFER_SIZE)
rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used#endif
// Standard Input/Output
functions
#include <stdio.h>
// Declare your global variables
here
void main(void)
{
PORTA=0x00;
DDRA=0x01;
PORTD=0x00;
DDRD=0x00;
// USART initialization
// Communication Parameters:
8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
};

}
Mendeteksi cahaya
#include <mega16.h>
void main(void)
{
PORTA=0x00;
DDRA=0x01;
PORTB=0x00;
DDRB=0x00;
while (1)
{
//jika cahaya gelap, LED
menyala
if(PINB.0==1)
{
PORTA.0=1;
}
//jika cahaya terang, LED
padam
if(PINB.0==0)
{
PORTA.0=0;
}
}

};

-Blink LED
-Shift Right LED
-Baca tulis data ke port
-Menampilkan karakter pada
sevensegment
-Up counter seven segment
-Menampilkan karakter pada
matriks led
-putaran motor stepper CW
-putaran motor stepper CWW
-Menampilkan karaktek ke LCD
-Penggunaan Keypad
-Membaca data dari IR sensor
-Blink led menggunakan timer1
-jam digital menggunakan
timer2
-Counter
-interrupt external
-baca tulis data ke eeprom
-komunikasi serial dengan
komputer
-voltmeter digital
-thermometer digital
-pengaturan intensitas cahaya
LED
-pengaturan intensitas cahaya
LED otomatis
-robot mobil
-robot line follower
-mengendalikan motor servo
-Menampilkan jam dan tanggal
dari rtc ds1307
-mengukur jarak menggunakan
sensor ping
-mengatur warna rgb led
-pengendalian melalui
bluetooth
-mendeteksi cahaya

Das könnte Ihnen auch gefallen