0% found this document useful (0 votes)
14 views

Contoh Aplikasi Avr: Menghidupkan Led (Byte) - Output

The document contains several Arduino/AVR code examples for controlling LEDs and reading button inputs using an ATmega16 microcontroller. The examples include: 1) Turning on LEDs individually by byte and bit using ports; 2) Blinking and running LED patterns using arrays; 3) Reading button presses individually by byte and bit and storing in ports; 4) Using macros and bit definitions to simplify button reading code. The document also contains LCD display code examples including standard text display, reading button inputs, and a counter program.

Uploaded by

Muhammad Syafi'i
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Contoh Aplikasi Avr: Menghidupkan Led (Byte) - Output

The document contains several Arduino/AVR code examples for controlling LEDs and reading button inputs using an ATmega16 microcontroller. The examples include: 1) Turning on LEDs individually by byte and bit using ports; 2) Blinking and running LED patterns using arrays; 3) Reading button presses individually by byte and bit and storing in ports; 4) Using macros and bit definitions to simplify button reading code. The document also contains LCD display code examples including standard text display, reading button inputs, and a counter program.

Uploaded by

Muhammad Syafi'i
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CONTOH APLIKASI AVR

Menghidupkan Led (Byte) - Output


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>
//#include <mega8535.h>
void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0xFF;

ACSR=0x80;
while (1)
{ PORTA=0xF0; PORTB=0x0F; PORTC=0xC3; PORTD=0b10101010; };
}

Menghidupkan Led (Bit) - Output


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>
#include <delay.h>

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0xFF;
ACSR=0x80;
PORTA.7=0; PORTB.7=1; PORTC=0xF0;

while (1)
{
PORTA.7=~PORTA.7; PORTB.7=~PORTB.7; PORTC=~PORTC;
delay_ms(1000);
};
}

Menghidupkan Led Kombinasi (Byte) - Output


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>
#include <delay.h>

void Flip_Flop()
{char i;
for (i=0;i<3;i++)
{ PORTB=0xF0; delay_ms(500); PORTB=0x0F; delay_ms(500); }
PORTA=0xFF;
}
void Running_Left()
{char i;
PORTB=0xFE; // nilai awal PORTB=0xfe atau bit ke-0 berlogika "0"
delay_ms(50);
for (i=0;i<8;i++)
{ PORTB=PORTB<<1; // PORTB digeser 1 bit sehingga bit ke-0 mengisi bit ke-1
delay_ms(250);
}
PORTB=0xFF;
}
void Running_Right()
{char i;
PORTB=0x7F; // nilai awal PORTC=0x7f atau bit ke-7 berlogika "0"
delay_ms(50);
for (i=0;i<8;i++)
{ PORTB=PORTB>>1; // PORTC digeser 1 bit sehingga bit ke-7 mengisi bit ke-6
delay_ms(250);
}
PORTB=0xFF;
}

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0xFF;

ACSR=0x80;
while (1)
{ Flip_Flop(); Running_Left(); Running_Right(); };
}

Menghidupkan Led Kombinasi (Array) - Output


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>
#include <delay.h>

void led_wyper(char ulang) // Led Wyper


{ while (ulang--)
{ char led[7]={0b01110111,0b10111011,0b11011101,0b11101110,0b11011101,0b10111011,0b01110111};
char i;
for (i=0;i<7;i++)
{ PORTC=led[i]; delay_ms(100); }
}
}

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0x7E; DDRB=0xFF;
PORTC=0x77; DDRC=0xFF;
PORTD=0xFF; DDRD=0xFF;

ACSR=0x80;
while (1)
{
char led[7]={0x7E,0xBD,0xDB,0xE7,0xDB,0xBD,0x7E};
char i;
for (i=0;i<7;i++)
{ PORTB=led[i]; delay_ms(100); }

led_wyper(3);
};
}

Membaca Penekanan Tombol (Byte) - Input


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0x00; //Input dgn R pull-up

ACSR=0x80;
while (1)
{ PORTA=PIND; PORTB=~PIND; };
}

Membaca Penekanan Tombol (Bit) - Input


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0x00; //Input dgn R pull-up

ACSR=0x80;
while (1)
{
if (!PIND.0){PORTA.0=0;}
if (!PIND.1){PORTA.0=1;}
if (!PIND.2){PORTA.2=0;}
if (!PIND.3){PORTA.2=1;}
if (!PIND.4){PORTA.4=0;}
if (!PIND.5){PORTA.4=1;}
if (!PIND.6){PORTA.6=0;}
if (!PIND.7){PORTA.6=1;}
};
}

Membaca Penekanan Tombol Alternatif (Byte) - Input


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>

#define Lampu PORTA


#define Switch ~PIND

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0x00; //Input

ACSR=0x80;
while (1)
{ Lampu = Switch; };
}

Membaca Penekanan Tombol Alternatif (Bit) - Input


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0x00; //Input dgn R pull-up

ACSR=0x80;
while (1)
{
if (PIND.7==1){PORTB=0x00;}
else { PORTB=0xFF;}
};
}

Membaca Penekanan Tombol (Bit) - Input


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>

#define led PORTA


#define nyala 0x00
#define padam 0xFF
#define tombol1 PIND.0
#define tombol2 PIND.1
#define tombol3 PIND.2
#define tombol4 PIND.3

void main(void)
{
PORTA=0xFF; DDRA=0xFF; // Output
PORTB=0xFF; DDRB=0xFF;
PORTC=0xFF; DDRC=0xFF;
PORTD=0xFF; DDRD=0x00; //Input dgn R pull-up
ACSR=0x80;
while (1)
{
if (tombol1 ==0) //jika switch S0 ditekan maka
led=nyala;
if (tombol2 ==0) //jika switch S1 ditekan maka
led=padam;
if (tombol3 ==0) //jika switch S2 ditekan maka
led=0xAA;
if (tombol4 ==0) //jika switch S0 ditekan maka
led=0x55;
};
}
Program LCD Standart
/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>
#include <delay.h>

// Fungsi Modul LCD


#asm
// .equ __lcd_port=0x1B ;PORTA = LCD
// .equ __lcd_port=0x18 ;PORTB = LCD
// .equ __lcd_port=0x15 ;PORTC = LCD
.equ __lcd_port=0x12 ;PORTD = LCD
#endasm
#include <lcd.h>
#include <stdio.h>

void main(void)
{
PORTA=0xFF; DDRA=0xFF;
PORTB=0xFF; DDRB=0x00;
PORTC=0x00; DDRC=0x00;
PORTD=0x00; DDRD=0x00;

ACSR=0x80;
lcd_init(16); //Inisialisasi LCD

while (1)
{
lcd_clear();
lcd_putsf("abcdefghijklmnop");
lcd_gotoxy(0,1);
lcd_putsf("ABCDEFGHIJKLMNOP");
delay_ms(1000);
lcd_gotoxy(0,0);
lcd_putsf("1234567890abcdef");
lcd_gotoxy(0,1);
lcd_putsf("qwerty1234qwerty");
delay_ms(1000);
};
}

Program LCD dengan Tombol + Led


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>
#include <delay.h>

// Fungsi Modul LCD


#asm
// .equ __lcd_port=0x1B ;PORTA = LCD
// .equ __lcd_port=0x18 ;PORTB = LCD
// .equ __lcd_port=0x15 ;PORTC = LCD
.equ __lcd_port=0x12 ;PORTD = LCD
#endasm
#include <lcd.h>
#include <stdio.h>

void main(void)
{
PORTA=0xFF; DDRA=0xFF;
PORTB=0xFF; DDRB=0x00;
PORTC=0x00; DDRC=0x00;
PORTD=0x00; DDRD=0x00;
ACSR=0x80;
lcd_init(16); //Inisialisasi LCD
lcd_clear();

while (1)
{
lcd_gotoxy(0,0);
lcd_putsf("abcdefghijklmnop");
lcd_gotoxy(0,1);
lcd_putsf("ABCDEFGHIJKLMNOP");
PORTA=PINB;
};
}

Program LCD dengan Tombol Counter up & down


/*****************************************************
Chip type : ATmega16 (11,059200 MHz)
*****************************************************/
#include <mega16.h>
#include <delay.h>

#define SW_up ~PINB.6


#define SW_down ~PINB.7

int data;

// Fungsi Modul LCD


#asm
// .equ __lcd_port=0x1B ;PORTA = LCD
// .equ __lcd_port=0x18 ;PORTB = LCD
// .equ __lcd_port=0x15 ;PORTC = LCD
.equ __lcd_port=0x12 ;PORTD = LCD
#endasm
#include <lcd.h>
#include <stdio.h>

void main(void)
{
PORTA=0xFF; DDRA=0xFF;
PORTB=0xFF; DDRB=0x00;
PORTC=0x00; DDRC=0x00;
PORTD=0x00; DDRD=0x00;

ACSR=0x80;
Data=0;
lcd_init(16); //Inisialisasi LCD
lcd_clear();
lcd_putsf("NILAI COUNTER");
lcd_gotoxy(0,1);
lcd_putsf(" [ ] ");

while (1)
{
if (SW_up){data--;delay_ms(500);if(data<0){data=999;}}
if (SW_down){data++;delay_ms(500);if(data>999){data=0;}}
};
}

You might also like