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
#define buzzer PORTA.4
char data,buffer[33];
unsigned int data_gas,gas,data_suhu; float suhu;
// Alphanumeric LCD functions #include <alcd.h>
// Standard Input/Output functions #include <stdio.h>
#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);
// Declare your global variables here
//========================================================= //Program FUNGSI Pergerakan / MANUVER ROBOT
//=========================================================
// 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
// 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;
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
// Analog Comparator initialization // Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// 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: // 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); //Program untuk menampilkan data GAS dan SUHU
//=========================================================
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); lcd_putsf(" --ROBOT MAJU-- ");}
else if(data=='b') {mundur(); lcd_gotoxy(0,0); lcd_putsf(" -ROBOT MUNDUR- ");}
else if(data=='c') {kanan(); lcd_gotoxy(0,0); lcd_putsf(" -ROBOT NGANAN- ");}
else if(data=='d') {kiri(); lcd_gotoxy(0,0); lcd_putsf(" --ROBOT KIRI-- ");}
else {stop(); }
}
} }
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 WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10 Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11 Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
Const WM_CAP_SEQUENCE = WM_CAP_START + 62 Const 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 SWP_NOMOVE = &H2S Const SWP_NOSIZE = 1 Const SWP_NOZORDER = &H4S Const 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, _
<MarshalAs(UnmanagedType.AsAny)>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
Dim VideoSource AsInteger Dim hWnd AsInteger
PrivateSub Button_CONNECT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_CONNECT.Click
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
b_maju.BackColor = Color.LightGray
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, _
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()
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
serial_input = ""
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
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
LAMPIRAN B
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 <mega16.h>
#include <delay.h>
#include <math.h>
unsigned char buffer[33];
int NO,CO;
// Alphanumeric LCD functions
#include <alcd.h>
// Standard Input/Output functions
#include <stdio.h>
#define ADC_VREF_TYPE 0x40
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
// 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 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
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;
// 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;
// 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_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);
LAMPIRAN C
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)
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