Simulasi Sistem Kendali Robot Monitoring Menggunakan Komunikasi Radio Berbasis Mikrokontroller Atmega 16
LAMPIRAN A
Universitas Sumatera Utara
Program CODEVISIONAVR
Untuk program mikrokontroler pada Robot
/*****************************************************
Date
: 8/24/2014
Chip type
: ATmega16
Program type
: Application
AVR Core Clock frequency: 11.059200 MHz
Memory model
: Small
External RAM size
: 0
Data Stack size
: 256
*****************************************************/
#include
#include
#define buzzer PORTA.4
char data,buffer[33];
unsigned int data_gas,gas,data_suhu;
float suhu;
// Alphanumeric LCD functions
#include
// Standard Input/Output functions
#include
#define ADC_VREF_TYPE 0x40
// 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;
}
Universitas Sumatera Utara
// Declare your global variables here
//=========================================================
//Program FUNGSI Pergerakan / MANUVER ROBOT
//=========================================================
void maju()
{
PORTB.4=1; PORTB.5=0; // kiri maju
PORTB.6=0; PORTB.7=1; // kanan maju
}
void mundur()
{
PORTB.4=0; PORTB.5=1; // kiri mundur
PORTB.6=1; PORTB.7=0; // kanan mundur
}
void kanan()
{
PORTB.4=1; PORTB.5=0; // kiri maju
PORTB.6=1; PORTB.7=0; // kanan mundur
}
void kiri()
{
PORTB.4=0; PORTB.5=1; // kiri mundur
PORTB.6=0; PORTB.7=1; // kanan maju
}
void stop()
{
PORTB.4=0; PORTB.5=0; // kiri mundur
PORTB.6=0; PORTB.7=0; // kanan maju
}
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=0x10;
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out
Func2=Out Func1=Out Func0=Out
Universitas Sumatera Utara
// 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=P State2=T
State1=T State0=T
PORTC=0x08;
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=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 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;
Universitas Sumatera Utara
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// 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;
// 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: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AVCC pin
Universitas Sumatera Utara
// ADC Auto Trigger Source: ADC Stopped
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD
menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 16
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("ROBOT PENDETEKSI");
lcd_gotoxy(0,1);
lcd_putsf(" -SUHU&GAS LPG- ");
delay_ms(2000);
lcd_clear();
while (1)
{
// Place your code here
//=========================================================
//Program untuk menampilkan data GAS dan SUHU
//=========================================================
data_suhu=read_adc(0);
data_gas=read_adc(2);
gas=data_gas/10;
suhu=((float)data_suhu*500)/1023;
data_suhu=(int)suhu;
lcd_gotoxy(0,1);
sprintf(buffer,"T=%2d%cC,G=%3d ppm",data_suhu,0xdf,gas);
lcd_puts(buffer);
printf("#T=%2d%cC,G=%3d ppm @",data_suhu,0xdf,gas);
Universitas Sumatera Utara
if(suhu>45) {buzzer=1; delay_ms(50); lcd_gotoxy(0,0);
lcd_putsf("Situasi BAHAYA ");}
else {lcd_gotoxy(0,0); lcd_putsf("Situasi AMAN
");}
buzzer=0; delay_ms(50);
//=========================================================
//Program untuk PERGERAKAN ROBOT
//=========================================================
if((UCSRA&0x80)==0x80)
{
data=getchar();
if
(data=='a') {maju(); lcd_gotoxy(0,0);
-ROBOT MAJU-- ");}
else if(data=='b') {mundur(); lcd_gotoxy(0,0);
ROBOT MUNDUR- ");}
else if(data=='c') {kanan(); lcd_gotoxy(0,0);
ROBOT NGANAN- ");}
else if(data=='d') {kiri(); lcd_gotoxy(0,0);
-ROBOT KIRI-- ");}
else {stop(); }
lcd_putsf(" lcd_putsf(" lcd_putsf(" lcd_putsf(" -
}
}
}
Program VISUAL BASIC 2010
Untuk Tampilan Interface pada Komputer
Imports System.Runtime.InteropServices
PublicClassForm1
Dim serial AsByte
Dim myport AsArray
Dim a AsByte = 0
Dim b AsByte = 0
Dim c AsByte = 0
Dim d AsByte = 0
Dim serial_input AsString
Dim cek AsString
Dim flag AsString
Dim data_jam AsInteger
Dim data_menit AsInteger
DelegateSubSetTextCallback(ByVal [text] AsString)
Const WM_CAP_START = &H400S
Const WS_CHILD = &H40000000
Const WS_VISIBLE = &H10000000
Universitas Sumatera Utara
Const
Const
Const
Const
Const
WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
WM_CAP_EDIT_COPY = WM_CAP_START + 30
WM_CAP_SEQUENCE = WM_CAP_START + 62
WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
Const WM_CAP_SET_SCALE = WM_CAP_START + 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Const
Const
Const
Const
SWP_NOMOVE = &H2S
SWP_NOSIZE = 1
SWP_NOZORDER = &H4S
HWND_BOTTOM = 1
DeclareFunction capGetDriverDescriptionA Lib"avicap32.dll" _
(ByVal wDriverIndex AsShort, _
ByVal lpszName AsString, ByVal cbName AsInteger, ByVal
lpszVer AsString, _
ByVal cbVer AsInteger) AsBoolean
DeclareFunction capCreateCaptureWindowA Lib"avicap32.dll" _
(ByVal lpszWindowName AsString, ByVal dwStyle
AsInteger, _
ByVal x AsInteger, ByVal y AsInteger, ByVal nWidth
AsInteger, _
ByVal nHeight AsShort, ByVal hWnd AsInteger, _
ByVal nID AsInteger) AsInteger
DeclareFunction SendMessage Lib"user32"Alias"SendMessageA" _
(ByVal hwnd AsInteger, ByVal Msg AsInteger, ByVal
wParam AsInteger, _
ByVal lParam AsObject)
AsInteger
DeclareFunction SetWindowPos Lib"user32"Alias"SetWindowPos"
_
(ByVal hwnd AsInteger, _
ByVal hWndInsertAfter AsInteger, ByVal x AsInteger, ByVal y
AsInteger, _
ByVal cx AsInteger, ByValcyAsInteger, ByVal wFlags
AsInteger) AsInteger
DeclareFunction DestroyWindow Lib"user32" (ByVal hndw
AsInteger) AsBoolean
Universitas Sumatera Utara
Dim VideoSource AsInteger
Dim hWnd AsInteger
PrivateSub Button_CONNECT_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button_CONNECT.Click
If serial = 0 Then
SerialPort1.ReceivedBytesThreshold = 19
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = ComboBox2.Text
SerialPort1.Open()
Button_CONNECT.Text = "&Disconnect"
serial = 1
b_maju.Enabled = True
b_mundur.Enabled = True
b_kanan.Enabled = True
b_kiri.Enabled = True
ElseIf serial = 1 Then
SerialPort1.Close()
Button_CONNECT.Text = "&Connect SERIAL"
serial = 0
b_maju.Enabled = False
b_mundur.Enabled = False
b_kanan.Enabled = False
b_kiri.Enabled = False
EndIf
EndSub
PrivateSub Form1_Load(ByVal sender AsObject, ByVal e As
System.EventArgs) HandlesMe.Load
myport = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myport)
Timer1.Enabled = True
ListBox1.Items.Clear()
Dim DriverName AsString = Space(80)
Dim DriverVersion AsString = Space(80)
For i AsInteger = 0 To 9
IfcapGetDriverDescriptionA(i, DriverName, 80, DriverVersion,
80) Then
ListBox1.Items.Add(DriverName.Trim)
EndIf
Next
Universitas Sumatera Utara
b_maju.BackColor = Color.LightGray
b_mundur.BackColor = Color.LightGray
b_kanan.BackColor = Color.LightGray
b_kiri.BackColor = Color.LightGray
b_maju.Enabled = False
b_mundur.Enabled = False
b_kanan.Enabled = False
b_kiri.Enabled = False
EndSub
PrivateSub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
VideoSource = ListBox1.SelectedIndex
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
EndSub
PrivateSub start_cam_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles start_cam.Click
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
VideoSource = ListBox1.SelectedIndex
hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or
WS_CHILD, 0, 0, 0, _
0, PictureBox1.Handle.ToInt32, 0)
IfSendMessage(hWnd, WM_CAP_DRIVER_CONNECT, VideoSource, 0)
Then
SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
PictureBox1.Width, PictureBox1.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
DestroyWindow(hWnd)
EndIf
EndSub
PrivateSub Button_EXIT_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button_EXIT.Click
Close()
End
IfSerialPort1.IsOpen() = TrueThen
SerialPort1.Close()
EndIf
Universitas Sumatera Utara
EndSub
PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Timer1.Tick
jam.Text = Format(Now, "hh:mm:ss") &" WIB"
tanggal.Text = Format(Now, "ddd, dd-MMM-yyyy")
If serial = 1 Then
If a = 1 Then
SerialPort1.Write("a")
ElseIf b = 1 Then
SerialPort1.Write("b")
ElseIf c = 1 Then
SerialPort1.Write("c")
ElseIf d = 1 Then
SerialPort1.Write("d")
Else : SerialPort1.Write(" ")
EndIf
EndIf
EndSub
PrivateSub SerialPort1_DataReceived(ByVal sender AsObject,
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
EndSub
PrivateSubReceivedText(ByVal [text] AsString) 'input from
ReadExisting
IfMe.lbl_serial.InvokeRequired Then
Dim x AsNewSetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, NewObject() {(text)})
Else
Me.serial_input = [text]
lbl_serial.Text = serial_input
cek = Mid(serial_input, 1, 1)
If cek = "#"Then
Label2.Text = Mid(serial_input, 4, 2) &"
Celcius "
Label3.Text = Mid(serial_input, 11, 3) &"
ppm "
If Label2.Text > 45 Then
Label4.Text = "SITUASI BAHAYA"
Else
Label4.Text = "SITUASI AMAN"
EndIf
[text] = ""
Universitas Sumatera Utara
serial_input = ""
Else
flag = 1
[text] = ""
serial_input = ""
lbl_serial.Text = ""
EndIf
EndIf
EndSub
PrivateSub b_kanan_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_kanan.MouseDown
b_kanan.BackColor = Color.Green
c = 1
EndSub
PrivateSub b_kiri_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_kiri.MouseDown
b_kiri.BackColor = Color.Green
d = 1
EndSub
PrivateSub b_maju_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_maju.MouseDown
b_maju.BackColor = Color.Green
a = 1
EndSub
PrivateSub b_mundur_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_mundur.MouseDown
b_mundur.BackColor = Color.Green
b = 1
EndSub
PrivateSub b_kanan_MouseUp(ByVal sender AsObject, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles b_kanan.MouseUp
b_kanan.BackColor = Color.LightGray
c = 0
EndSub
Universitas Sumatera Utara
PrivateSub b_kiri_MouseUp(ByVal sender AsObject, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles b_kiri.MouseUp
b_kiri.BackColor = Color.LightGray
d = 0
EndSub
PrivateSub b_maju_MouseUp(ByVal sender AsObject, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles b_maju.MouseUp
b_maju.BackColor = Color.LightGray
a = 0
EndSub
PrivateSub b_mundur_MouseUp(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_mundur.MouseUp
b_mundur.BackColor = Color.LightGray
b = 0
EndSub
EndClass
Universitas Sumatera Utara
LAMPIRAN B
Program Pada Mikrokontroler
Universitas Sumatera Utara
Chip type
: ATmega16
Program type
: Application
AVR Core Clock frequency: 11.059200 MHz
Memory model
: Small
External RAM size
: 0
Data Stack size
: 256
*****************************************************/
#include
#include
#include
unsigned char buffer[33];
int NO,CO;
// Alphanumeric LCD functions
#include
// Standard Input/Output functions
#include
#define ADC_VREF_TYPE 0x40
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
Universitas Sumatera Utara
// 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;
}
// 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
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
//
Func7=In
Func6=In
Func5=In
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
Universitas Sumatera Utara
PORTB=0x00;
DDRB=0x00;
// Port C initialization
//
Func7=In
Func6=In
Func5=In
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
//
Func7=In
Func6=In
Func5=In
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
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: 1200
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x02;
UBRRL=0x3F;
Universitas Sumatera Utara
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: ADC Stopped
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric
LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 16
lcd_init(16);
while (1)
{
// Place your code here
NO=(1024-read_adc(0))-430;
CO=(1024-read_adc(1))-120;
lcd_gotoxy(0,0);
Universitas Sumatera Utara
lcd_putsf(" KUALITAS UDARA ");
lcd_gotoxy(0,1);
sprintf(buffer,"CO=%4d NO2=%4d",CO,NO);
lcd_puts(buffer);
printf("Data Serial #$%3d?$%3d?@\n",CO,NO);
putchar(13);
delay_ms(200);
}}
Universitas Sumatera Utara
LAMPIRAN C
Program Pada Visual Basic
Universitas Sumatera Utara
Option Explicit
Dim SerialInput As String
Dim i As Integer
Dim a As Integer
Dim flag As Integer
Dim data(1000) As String
Dim data_serial As String
Dim DataSerial As String, PaketData As String
Dim ADC1 As String
Dim ADC2 As String
Private Sub Command1_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen =
False
End
End Sub
Private Sub Form_Load()
MSComm1.RThreshold = 40
MSComm1.Settings = "1200,N,8,1"
MSComm1.CommPort = 3
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen
False
=
True
Then
MSComm1.PortOpen
=
Universitas Sumatera Utara
End Sub
Private Sub MSComm1_OnComm()
Dim startdata As Integer
Dim enddata As Integer
Dim acceptdata As String
Dim valid As String
MSComm1.RThreshold = 40
SerialInput = SerialInput & MSComm1.Input
If InStr(SerialInput, "$") > 1 Then
startdata = InStr(SerialInput, "#")
enddata = InStr(SerialInput, "@")
If startdata + 11 = enddata Then
acceptdata
=
Mid(SerialInput,
startdata,
enddata)
valid = Left(acceptdata, 12)
Label2.Caption = valid
CO.Caption = Mid(valid, 3, 3)
NO.Caption = Mid(valid, 8, 3)
End If
MSComm1.RThreshold = 6
End If
SerialInput = ""
'End If
End Sub
Universitas Sumatera Utara
Universitas Sumatera Utara
Program CODEVISIONAVR
Untuk program mikrokontroler pada Robot
/*****************************************************
Date
: 8/24/2014
Chip type
: ATmega16
Program type
: Application
AVR Core Clock frequency: 11.059200 MHz
Memory model
: Small
External RAM size
: 0
Data Stack size
: 256
*****************************************************/
#include
#include
#define buzzer PORTA.4
char data,buffer[33];
unsigned int data_gas,gas,data_suhu;
float suhu;
// Alphanumeric LCD functions
#include
// Standard Input/Output functions
#include
#define ADC_VREF_TYPE 0x40
// 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;
}
Universitas Sumatera Utara
// Declare your global variables here
//=========================================================
//Program FUNGSI Pergerakan / MANUVER ROBOT
//=========================================================
void maju()
{
PORTB.4=1; PORTB.5=0; // kiri maju
PORTB.6=0; PORTB.7=1; // kanan maju
}
void mundur()
{
PORTB.4=0; PORTB.5=1; // kiri mundur
PORTB.6=1; PORTB.7=0; // kanan mundur
}
void kanan()
{
PORTB.4=1; PORTB.5=0; // kiri maju
PORTB.6=1; PORTB.7=0; // kanan mundur
}
void kiri()
{
PORTB.4=0; PORTB.5=1; // kiri mundur
PORTB.6=0; PORTB.7=1; // kanan maju
}
void stop()
{
PORTB.4=0; PORTB.5=0; // kiri mundur
PORTB.6=0; PORTB.7=0; // kanan maju
}
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=0x10;
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out
Func2=Out Func1=Out Func0=Out
Universitas Sumatera Utara
// 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=P State2=T
State1=T State0=T
PORTC=0x08;
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=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 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;
Universitas Sumatera Utara
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// 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;
// 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: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AVCC pin
Universitas Sumatera Utara
// ADC Auto Trigger Source: ADC Stopped
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD
menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 16
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("ROBOT PENDETEKSI");
lcd_gotoxy(0,1);
lcd_putsf(" -SUHU&GAS LPG- ");
delay_ms(2000);
lcd_clear();
while (1)
{
// Place your code here
//=========================================================
//Program untuk menampilkan data GAS dan SUHU
//=========================================================
data_suhu=read_adc(0);
data_gas=read_adc(2);
gas=data_gas/10;
suhu=((float)data_suhu*500)/1023;
data_suhu=(int)suhu;
lcd_gotoxy(0,1);
sprintf(buffer,"T=%2d%cC,G=%3d ppm",data_suhu,0xdf,gas);
lcd_puts(buffer);
printf("#T=%2d%cC,G=%3d ppm @",data_suhu,0xdf,gas);
Universitas Sumatera Utara
if(suhu>45) {buzzer=1; delay_ms(50); lcd_gotoxy(0,0);
lcd_putsf("Situasi BAHAYA ");}
else {lcd_gotoxy(0,0); lcd_putsf("Situasi AMAN
");}
buzzer=0; delay_ms(50);
//=========================================================
//Program untuk PERGERAKAN ROBOT
//=========================================================
if((UCSRA&0x80)==0x80)
{
data=getchar();
if
(data=='a') {maju(); lcd_gotoxy(0,0);
-ROBOT MAJU-- ");}
else if(data=='b') {mundur(); lcd_gotoxy(0,0);
ROBOT MUNDUR- ");}
else if(data=='c') {kanan(); lcd_gotoxy(0,0);
ROBOT NGANAN- ");}
else if(data=='d') {kiri(); lcd_gotoxy(0,0);
-ROBOT KIRI-- ");}
else {stop(); }
lcd_putsf(" lcd_putsf(" lcd_putsf(" lcd_putsf(" -
}
}
}
Program VISUAL BASIC 2010
Untuk Tampilan Interface pada Komputer
Imports System.Runtime.InteropServices
PublicClassForm1
Dim serial AsByte
Dim myport AsArray
Dim a AsByte = 0
Dim b AsByte = 0
Dim c AsByte = 0
Dim d AsByte = 0
Dim serial_input AsString
Dim cek AsString
Dim flag AsString
Dim data_jam AsInteger
Dim data_menit AsInteger
DelegateSubSetTextCallback(ByVal [text] AsString)
Const WM_CAP_START = &H400S
Const WS_CHILD = &H40000000
Const WS_VISIBLE = &H10000000
Universitas Sumatera Utara
Const
Const
Const
Const
Const
WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
WM_CAP_EDIT_COPY = WM_CAP_START + 30
WM_CAP_SEQUENCE = WM_CAP_START + 62
WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
Const WM_CAP_SET_SCALE = WM_CAP_START + 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Const
Const
Const
Const
SWP_NOMOVE = &H2S
SWP_NOSIZE = 1
SWP_NOZORDER = &H4S
HWND_BOTTOM = 1
DeclareFunction capGetDriverDescriptionA Lib"avicap32.dll" _
(ByVal wDriverIndex AsShort, _
ByVal lpszName AsString, ByVal cbName AsInteger, ByVal
lpszVer AsString, _
ByVal cbVer AsInteger) AsBoolean
DeclareFunction capCreateCaptureWindowA Lib"avicap32.dll" _
(ByVal lpszWindowName AsString, ByVal dwStyle
AsInteger, _
ByVal x AsInteger, ByVal y AsInteger, ByVal nWidth
AsInteger, _
ByVal nHeight AsShort, ByVal hWnd AsInteger, _
ByVal nID AsInteger) AsInteger
DeclareFunction SendMessage Lib"user32"Alias"SendMessageA" _
(ByVal hwnd AsInteger, ByVal Msg AsInteger, ByVal
wParam AsInteger, _
ByVal lParam AsObject)
AsInteger
DeclareFunction SetWindowPos Lib"user32"Alias"SetWindowPos"
_
(ByVal hwnd AsInteger, _
ByVal hWndInsertAfter AsInteger, ByVal x AsInteger, ByVal y
AsInteger, _
ByVal cx AsInteger, ByValcyAsInteger, ByVal wFlags
AsInteger) AsInteger
DeclareFunction DestroyWindow Lib"user32" (ByVal hndw
AsInteger) AsBoolean
Universitas Sumatera Utara
Dim VideoSource AsInteger
Dim hWnd AsInteger
PrivateSub Button_CONNECT_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button_CONNECT.Click
If serial = 0 Then
SerialPort1.ReceivedBytesThreshold = 19
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = ComboBox2.Text
SerialPort1.Open()
Button_CONNECT.Text = "&Disconnect"
serial = 1
b_maju.Enabled = True
b_mundur.Enabled = True
b_kanan.Enabled = True
b_kiri.Enabled = True
ElseIf serial = 1 Then
SerialPort1.Close()
Button_CONNECT.Text = "&Connect SERIAL"
serial = 0
b_maju.Enabled = False
b_mundur.Enabled = False
b_kanan.Enabled = False
b_kiri.Enabled = False
EndIf
EndSub
PrivateSub Form1_Load(ByVal sender AsObject, ByVal e As
System.EventArgs) HandlesMe.Load
myport = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myport)
Timer1.Enabled = True
ListBox1.Items.Clear()
Dim DriverName AsString = Space(80)
Dim DriverVersion AsString = Space(80)
For i AsInteger = 0 To 9
IfcapGetDriverDescriptionA(i, DriverName, 80, DriverVersion,
80) Then
ListBox1.Items.Add(DriverName.Trim)
EndIf
Next
Universitas Sumatera Utara
b_maju.BackColor = Color.LightGray
b_mundur.BackColor = Color.LightGray
b_kanan.BackColor = Color.LightGray
b_kiri.BackColor = Color.LightGray
b_maju.Enabled = False
b_mundur.Enabled = False
b_kanan.Enabled = False
b_kiri.Enabled = False
EndSub
PrivateSub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
VideoSource = ListBox1.SelectedIndex
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
EndSub
PrivateSub start_cam_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles start_cam.Click
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
VideoSource = ListBox1.SelectedIndex
hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or
WS_CHILD, 0, 0, 0, _
0, PictureBox1.Handle.ToInt32, 0)
IfSendMessage(hWnd, WM_CAP_DRIVER_CONNECT, VideoSource, 0)
Then
SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
PictureBox1.Width, PictureBox1.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
DestroyWindow(hWnd)
EndIf
EndSub
PrivateSub Button_EXIT_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button_EXIT.Click
Close()
End
IfSerialPort1.IsOpen() = TrueThen
SerialPort1.Close()
EndIf
Universitas Sumatera Utara
EndSub
PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Timer1.Tick
jam.Text = Format(Now, "hh:mm:ss") &" WIB"
tanggal.Text = Format(Now, "ddd, dd-MMM-yyyy")
If serial = 1 Then
If a = 1 Then
SerialPort1.Write("a")
ElseIf b = 1 Then
SerialPort1.Write("b")
ElseIf c = 1 Then
SerialPort1.Write("c")
ElseIf d = 1 Then
SerialPort1.Write("d")
Else : SerialPort1.Write(" ")
EndIf
EndIf
EndSub
PrivateSub SerialPort1_DataReceived(ByVal sender AsObject,
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
EndSub
PrivateSubReceivedText(ByVal [text] AsString) 'input from
ReadExisting
IfMe.lbl_serial.InvokeRequired Then
Dim x AsNewSetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, NewObject() {(text)})
Else
Me.serial_input = [text]
lbl_serial.Text = serial_input
cek = Mid(serial_input, 1, 1)
If cek = "#"Then
Label2.Text = Mid(serial_input, 4, 2) &"
Celcius "
Label3.Text = Mid(serial_input, 11, 3) &"
ppm "
If Label2.Text > 45 Then
Label4.Text = "SITUASI BAHAYA"
Else
Label4.Text = "SITUASI AMAN"
EndIf
[text] = ""
Universitas Sumatera Utara
serial_input = ""
Else
flag = 1
[text] = ""
serial_input = ""
lbl_serial.Text = ""
EndIf
EndIf
EndSub
PrivateSub b_kanan_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_kanan.MouseDown
b_kanan.BackColor = Color.Green
c = 1
EndSub
PrivateSub b_kiri_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_kiri.MouseDown
b_kiri.BackColor = Color.Green
d = 1
EndSub
PrivateSub b_maju_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_maju.MouseDown
b_maju.BackColor = Color.Green
a = 1
EndSub
PrivateSub b_mundur_MouseDown(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_mundur.MouseDown
b_mundur.BackColor = Color.Green
b = 1
EndSub
PrivateSub b_kanan_MouseUp(ByVal sender AsObject, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles b_kanan.MouseUp
b_kanan.BackColor = Color.LightGray
c = 0
EndSub
Universitas Sumatera Utara
PrivateSub b_kiri_MouseUp(ByVal sender AsObject, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles b_kiri.MouseUp
b_kiri.BackColor = Color.LightGray
d = 0
EndSub
PrivateSub b_maju_MouseUp(ByVal sender AsObject, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles b_maju.MouseUp
b_maju.BackColor = Color.LightGray
a = 0
EndSub
PrivateSub b_mundur_MouseUp(ByVal sender AsObject, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
b_mundur.MouseUp
b_mundur.BackColor = Color.LightGray
b = 0
EndSub
EndClass
Universitas Sumatera Utara
LAMPIRAN B
Program Pada Mikrokontroler
Universitas Sumatera Utara
Chip type
: ATmega16
Program type
: Application
AVR Core Clock frequency: 11.059200 MHz
Memory model
: Small
External RAM size
: 0
Data Stack size
: 256
*****************************************************/
#include
#include
#include
unsigned char buffer[33];
int NO,CO;
// Alphanumeric LCD functions
#include
// Standard Input/Output functions
#include
#define ADC_VREF_TYPE 0x40
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
Universitas Sumatera Utara
// 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;
}
// 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
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
//
Func7=In
Func6=In
Func5=In
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
Universitas Sumatera Utara
PORTB=0x00;
DDRB=0x00;
// Port C initialization
//
Func7=In
Func6=In
Func5=In
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
//
Func7=In
Func6=In
Func5=In
Func2=In Func1=In Func0=In
Func4=In
Func3=In
//
State7=T
State6=T
State5=T
State2=T State1=T State0=T
State4=T
State3=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
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: 1200
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x02;
UBRRL=0x3F;
Universitas Sumatera Utara
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: ADC Stopped
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric
LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 16
lcd_init(16);
while (1)
{
// Place your code here
NO=(1024-read_adc(0))-430;
CO=(1024-read_adc(1))-120;
lcd_gotoxy(0,0);
Universitas Sumatera Utara
lcd_putsf(" KUALITAS UDARA ");
lcd_gotoxy(0,1);
sprintf(buffer,"CO=%4d NO2=%4d",CO,NO);
lcd_puts(buffer);
printf("Data Serial #$%3d?$%3d?@\n",CO,NO);
putchar(13);
delay_ms(200);
}}
Universitas Sumatera Utara
LAMPIRAN C
Program Pada Visual Basic
Universitas Sumatera Utara
Option Explicit
Dim SerialInput As String
Dim i As Integer
Dim a As Integer
Dim flag As Integer
Dim data(1000) As String
Dim data_serial As String
Dim DataSerial As String, PaketData As String
Dim ADC1 As String
Dim ADC2 As String
Private Sub Command1_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen =
False
End
End Sub
Private Sub Form_Load()
MSComm1.RThreshold = 40
MSComm1.Settings = "1200,N,8,1"
MSComm1.CommPort = 3
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen
False
=
True
Then
MSComm1.PortOpen
=
Universitas Sumatera Utara
End Sub
Private Sub MSComm1_OnComm()
Dim startdata As Integer
Dim enddata As Integer
Dim acceptdata As String
Dim valid As String
MSComm1.RThreshold = 40
SerialInput = SerialInput & MSComm1.Input
If InStr(SerialInput, "$") > 1 Then
startdata = InStr(SerialInput, "#")
enddata = InStr(SerialInput, "@")
If startdata + 11 = enddata Then
acceptdata
=
Mid(SerialInput,
startdata,
enddata)
valid = Left(acceptdata, 12)
Label2.Caption = valid
CO.Caption = Mid(valid, 3, 3)
NO.Caption = Mid(valid, 8, 3)
End If
MSComm1.RThreshold = 6
End If
SerialInput = ""
'End If
End Sub
Universitas Sumatera Utara