You are on page 1of 14

I.

Landasan teori

Salah satu metode komunikasi data pada mikrokontroller adalah dengan menggunakan lebar pulsa.
Setiap data yang dikirim, memiliki lebar pulsa yang berbeda-beda.

Illustrasi:

= data ‘0’

50% 50%

= data ‘1’

51% 49%

Dst.

II. Kode program

a. Kode pengirim

/*****************************************************

This program was produced by the

CodeWizardAVR V2.03.4 Standard

Automatic Program Generator

© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :

Version :

Date : 05/01/2011

Author :

Company :

Comments:
Chip type : ATmega8535

Program type : Application

Clock frequency : 11,059200 MHz

Memory model : Small

External RAM size : 0

Data Stack size : 128

*****************************************************/

#include <mega8535.h>

#include <stdio.h>

#include <delay.h>

unsigned char data;

// Declare your global variables here

void satu()

data = 1;

void dua()

data = 2;

void tiga()

data = 3;

void empat()

data = 4;

void lima()

{
data = 5;

void enam()

data = 6;

void tujuh()

data = 7;

void delapan()

data = 8;

void sembilan()

data = 9;

void bintang()

data = 30;

void nol()

data = 0;

void pagar()

data = 34;

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=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 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=0xF0;

// 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: 172,800 kHz

// Mode: Phase correct PWM top=FFh

// OC0 output: Non-Inverted PWM


TCCR0=0x63;

TCNT0=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: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

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: Off

// INT1: Off

// INT2: Off

MCUCR=0x00;

MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization

TIMSK=0x00;

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

ACSR=0x80;

SFIOR=0x00;

PORTC = 255;

while (1)

mulai:PORTC.4 =0;

PORTC.5 =1;

PORTC.6 =1;

PORTC.7 =1;

if (PINC.1 == 0)

satu();

//tampil();

}
if (PINC.2 == 0)

dua();

//tampil();

if (PINC.3 == 0)

tiga();

//tampil();

PORTC.4 =1;

PORTC.5 =0;

PORTC.6 =1;

PORTC.7 =1;

if (PINC.1 == 0)

empat();

//tampil();

if (PINC.2 == 0)

lima();

//tampil();

if (PINC.3 == 0)

enam();

//tampil();

PORTC.4 =1;
PORTC.5 =1;

PORTC.6 =0;

PORTC.7 =1;

if (PINC.1 == 0)

tujuh();

//tampil();

if (PINC.2 == 0)

delapan();

//tampil();

if (PINC.3 == 0)

sembilan();

//tampil();

PORTC.4 =1;

PORTC.5 =1;

PORTC.6 =1;

PORTC.7 =0;

if (PINC.1 == 0)

bintang();

//tampil();

if (PINC.2 == 0)

//nol();
//tampil();

if (PINC.3 == 0)

pagar();

//tampil();

OCR0=data;

};

b. Kode penerima

/*****************************************************

This program was produced by the

CodeWizardAVR V2.03.4 Standard

Automatic Program Generator

© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :

Version :

Date : 05/01/2011

Author :

Company :

Comments:

Chip type : ATmega32

Program type : Application

Clock frequency : 11,059200 MHz

Memory model : Small

External RAM size : 0

Data Stack size : 512


*****************************************************/

#include <mega32.h>

#include <delay.h>

#include <stdio.h>

// Alphanumeric LCD Module functions

#asm

.equ __lcd_port=0x15 ;PORTC

#endasm

#include <lcd.h>

unsigned char buff[33];

unsigned int pwm;

// Declare your global variables here

void count_up_kompas(void)

//TCCR1A=0b00000000;

TCCR1B=0b00000011;

unsigned int baca_pwm(void)

unsigned int count_value;

do

/*nothing or wait until PD6 low*/

while((PIND.3)!=0);

do

/*nothing or wait until PD6 high*/

}
while((PIND.3)==0);

TCNT1=0;

count_up_kompas();

do

/*nothing or wait until PD6 low*/

while((PIND.3)!=0);

count_value=TCNT1;

return count_value;

void main(void)

// Declare your local variables here

//19233

// 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=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=0xFF;

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=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=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: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

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: Off

// INT1: Off

// INT2: Off

MCUCR=0x00;

MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization

TIMSK=0x00;

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

ACSR=0x80;

SFIOR=0x00;
// LCD module initialization

lcd_init(16);

lcd_clear();

lcd_gotoxy(0,0);

pwm=0;

while (1)

// Place your code here

lcd_clear();

lcd_gotoxy(0,0);

pwm=baca_pwm();

sprintf(buff,"data: %d",pwm/2);

//sprintf(buff,"data: %d",PINA);

lcd_puts(buff);

//lcd_putsf("test");

delay_ms(50);

};

You might also like