0% found this document useful (0 votes)
7 views7 pages

Mikrocontroller USART Communication Code

The document contains code for 3 programs that simulate communication between a microcontroller transmitter and receiver. Program 1 controls LEDs based on button input. Program 2 controls multiple LED patterns based on button input. Program 3 is the same as Program 2 but uses macros to simplify the button input checks. The programs set up USART serial communication at 9600 baud and use characters sent from the transmitter to control LED output pins on the receiver.

Uploaded by

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

Mikrocontroller USART Communication Code

The document contains code for 3 programs that simulate communication between a microcontroller transmitter and receiver. Program 1 controls LEDs based on button input. Program 2 controls multiple LED patterns based on button input. Program 3 is the same as Program 2 but uses macros to simplify the button input checks. The programs set up USART serial communication at 9600 baud and use characters sent from the transmitter to control LED output pins on the receiver.

Uploaded by

AtabikLuthfi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PERCOBAAN 1

// PROGRAM UNTUK MIKROKONTROLER PENGIRIM


#include <mega128.h>
#include <stdio.h>
void main(void)
{
PORTA=0x00;
DDRA= 0X00;// SEMUA JADI INPUT
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: Off
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;
while (1)
{
// Place your code here
if(PINA.0==0)
putchar('8');
if(PINA.1==0)
putchar('9');
}
}

// PROGRAM UNTUK MIKROKONTROLER PENERIMA


#include <mega128.h>
#include <stdio.h>
void main(void)
{
int a;
PORTA=0x00;
DDRA=0xFF; //semua bit porta sbg output
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: Off
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x10;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;
PORTA = 0x00; //mula-mula led mati
while (1)
{
// Place your code here
a=getchar();
if(a=='8')
PORTA.0=1;
else
PORTA.0=0;
};
}

PROGRAM 2
// PROGRAM UNTUK MIKROKONTROLER PENGIRIM
#include <mega128.h>
#include <stdio.h>
void main(void)
{
PORTD=0x00;
DDRD=0x00;
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: Off
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;
UCSR1B=0x00;

while (1)
{
if(PIND.0==1)
putchar('z');
if(PIND.1==1)
putchar('y');
if(PIND.2==1)
putchar('x');
if(PIND.3==1)
putchar('v');
}
}

// PROGRAM UNTUK MIKROKONTROLER PENERIMA


#include <mega128.h>
#include <stdio.h>
void main(void)
{
int tabik;
PORTB=0x00;
DDRB=0xFF;
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: Off
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x10;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;

while (1)
{
tabik=getchar();
if(tabik=='z')
PORTB.0=1;
if(tabik=='y')
PORTB.1=PORTB.2=1;
if(tabik=='x')
PORTB.3=PORTB.4=PORTB.5=1;
if(tabik=='v')
{
PORTB.0=PORTB.2=PORTB.4=PORTB.6=1;
PORTB.1=PORTB.3=PORTB.5=PORTB.7=0;
}
}
}

PROGRAM 3
// PROGRAM UNTUK MIKROKONTROLER PENGIRIM
#include <mega128.h>
#include <stdio.h>
#define PB1 PIND.0
#define PB2 PIND.1
#define PB3 PIND.2
#define PB4 PIND.3
void main(void)
{
PORTD=0x00;
DDRD=0x00;
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: Off
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;
UCSR1B=0x00;

while (1)
{
if(PB1==1)
putchar('z');
if(PB2==1)
putchar('y');
if(PB3==1)
putchar('x');
if(PB4==1)
putchar('v');
}
}

// PROGRAM UNTUK MIKROKONTROLER PENERIMA


#include <mega128.h>
#include <stdio.h>
#define INDIKATOR1 PORTB.0
#define INDIKATOR2 PORTB.1
#define INDIKATOR3 PORTB.2
#define INDIKATOR4 PORTB.3
#define INDIKATOR5 PORTB.4
#define INDIKATOR6 PORTB.5
#define INDIKATOR7 PORTB.6
#define INDIKATOR8 PORTB.7
void main(void)
{
int tabik;
PORTB=0x00;
DDRB=0xFF;
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: Off
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x10;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;

while (1)
{
tabik=getchar();
if(tabik=='z')
INDIKATOR1=1;
if(tabik=='y')
INDIKATOR2= INDIKATOR3=1;
if(tabik=='x')
INDIKATOR4=INDIKATOR5= INDIKATOR6=1;
if(tabik=='v')
{
INDIKATOR1=INDIKATOR3=INDIKATOR5=INDIKATOR7=1;
INDIKATOR2=INDIKATOR4=INDIKATOR6=INDIKATOR8=0;
}
}
}

Rangkaian Simulasi :
Program 1 :

Program 2 :

Program 3 :

You might also like