Pada kesempatan yang berbahagia kali ini saya akan menjelaskan mengenai sebuah simulasi dari 2 buah mikrokontroller yang saling berhubungan menggunakan 2 buah serial UART yang mana masing masing mikrokontroller bertindak sebagai master dan slave. jadi saat tombol ditekan pada mikro 1 maka led yang berada pada mikro 2 akan menyala begitu juga jika tombol yang ada pada mikro 2 ditekan maka led pada mikro 1 akan menyala. kemudian pada mikro 1 terdapat sensor yang mana nilai sensor ditampilkan ke 2 buah mikrokontroller yaitu mikro 1 dan mikro 2. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Skema Alat
b. Program Mikro 1
$regfile = "m128def.dat"
$crystal = 12000000
$baud = 9600
Config Lcdpin = Pin , Rs = Portd.0 , E = Portd.1 , Db4 = Portd.6
Config Lcdpin = Pin , Db5 = Portd.7 , Db6 = Portd.4 , Db7 = Portd.5
Config Lcd = 16 * 2
Config Com1 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Com2 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "com1:" For Binary As #1
Open "Com2:" For Binary As #2
Cls
Cursor Off
On Urxc Getchar
Dim Wtime As Word
Dim Inchar As String * 1
Dim Inchar2 As Word
Dim A As Byte
Enable Interrupts
Enable Urxc
Config Pinb.0 = Output
Config Pinb.1 = Output
Config Pine.2 = Input
Config Pine.3 = Input
Config Pine.4 = Input
Config Pine.5 = Input
Config Pine.6 = Input
Porte.2 = 1
Porte.3 = 1
Porte.4 = 1
Porte.5 = 1
Porte.6 = 1
Do
A = Waitkey(#2)
Upperline
Lcd A
If Pine.2 = 1 And Pine.3 = 1 And Pine.4 = 1 And Pine.5 = 1 And Pine.6 = 1 Then
Print #1 , "9"
End If
If Pine.2 = 0 And Pine.3 = 1 And Pine.4 = 1 And Pine.5 = 1 And Pine.6 = 1 Then
Print #1 , "0"
End If
If Pine.2 = 0 And Pine.3 = 0 Then
Print #1 , "1"
End If
If Pine.2 = 0 And Pine.4 = 0 Then
Print #1 , "2"
End If
If Pine.2 = 0 And Pine.5 = 0 Then
Print #1 , "3"
End If
If Pine.2 = 0 And Pine.6 = 0 Then
Print #1 , "4"
End If
If Pine.2 = 1 And Pine.3 = 0 Then
Print #1 , "5"
End If
If Pine.2 = 1 And Pine.4 = 0 Then
Print #1 , "6"
End If
If Pine.2 = 1 And Pine.5 = 0 Then
Print #1 , "7"
End If
If Pine.2 = 1 And Pine.6 = 0 Then
Print #1 , "8"
End If
Loop
Getchar:
Inchar = Inkey(#1)
Select Case Inchar
Case "1" :
Portb.0 = 1
Portb.1 = 1
Case "2" :
Portb.0 = 0
Portb.1 = 1
Case "3" :
Portb.0 = 1
Portb.1 = 0
Case "4" :
Portb.0 = 0
Portb.1 = 0
End Select
Return
End
c. Program Mikro 2
$regfile = "m128def.dat"
$crystal = 12000000
$baud = 9600
Config Lcdpin = Pin , Rs = Porte.2 , E = Portd.7 , Db4 = Porte.3
Config Lcdpin = Pin , Db5 = Porte.4 , Db6 = Porte.5 , Db7 = Porte.6
Config Lcd = 16 * 2
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Config Com1 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Com2 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "com1:" For Binary As #1
Open "Com2:" For Binary As #2
On Urxc Getchar
Dim Dataadc As Word
Dim Wtime As Word
Dim Inchar As String * 1
Enable Interrupts
Enable Urxc
Cls
Cursor Off
Config Pinb.2 = Output
Config Pinb.3 = Output
Config Pinb.5 = Output
Config Pinb.6 = Output
Config Pinb.7 = Output
Config Pinf.0 = Input
Config Pinf.1 = Input
Portf.0 = 1
Portf.1 = 1
Start Adc
Do
Dataadc = Getadc(3)
Locate 1 , 1
Lcd "ADC="
Locate 2 , 1
Lcd Dataadc
Printbin #2 , Dataadc
If Pinf.0 = 0 And Pinf.1 = 0 Then
Print "1"
End If
If Pinf.0 = 0 And Pinf.1 = 1 Then
Print "2"
End If
If Pinf.0 = 1 And Pinf.1 = 0 Then
Print "3"
End If
If Pinf.0 = 1 And Pinf.1 = 1 Then
Print "4"
End If
Loop
Getchar:
Inchar = Inkey(#1)
Select Case Inchar
Case "1" :
Portb.2 = 1
Portb.3 = 1
Portb.5 = 0
Portb.6 = 0
Portb.7 = 0
Case "2" :
Portb.2 = 1
Portb.3 = 0
Portb.5 = 1
Portb.6 = 0
Portb.7 = 0
Case "3" :
Portb.2 = 1
Portb.3 = 0
Portb.5 = 0
Portb.6 = 1
Portb.7 = 0
Case "4" :
Portb.2 = 1
Portb.3 = 0
Portb.5 = 0
Portb.6 = 0
Portb.7 = 1
Case "5" :
Portb.2 = 0
Portb.3 = 1
Portb.5 = 0
Portb.6 = 0
Portb.7 = 0
Case "6" :
Portb.2 = 0
Portb.3 = 0
Portb.5 = 1
Portb.6 = 0
Portb.7 = 0
Case "7" :
Portb.2 = 0
Portb.3 = 0
Portb.5 = 0
Portb.6 = 1
Portb.7 = 0
Case "8" :
Portb.2 = 0
Portb.3 = 0
Portb.5 = 0
Portb.6 = 0
Portb.7 = 1
Case "9" :
Portb.2 = 0
Portb.3 = 0
Portb.5 = 0
Portb.6 = 0
Portb.7 = 0
Case "0" :
Portb.2 = 1
Portb.3 = 0
Portb.5 = 0
Portb.6 = 0
Portb.7 = 0
End Select
Return
End
d. VIDEO HASILNYA