Chào các bạn trong bài đăng này mình sẽ hướng dẫn các bạn cách sử dụng phần mềm Vb 6.0 để giao tiếp với PIC16F877A.
- Đây là ảnh mô phỏng protues.
- Đây là giao diện VB.
- Đây là code VB.
Option Explicit
Dim i As Integer
Private Sub cmd_giamsat_Click() 'GIAM SAT'
MSComm1.Output = "g"
End Sub
Private Sub cmd_off_Click()
MSComm1.Output = "0"
End Sub
Private Sub cmd_on_Click()
MSComm1.Output = "9"
End Sub
Private Sub cmd1_Click()
MSComm1.Output = "1"
End Sub
Private Sub cmd2_Click()
MSComm1.Output = "2"
End Sub
Private Sub cmd3_Click()
MSComm1.Output = "3"
End Sub
Private Sub cmd4_Click()
MSComm1.Output = "4"
End Sub
Private Sub cmd5_Click()
MSComm1.Output = "5"
End Sub
Private Sub cmd6_Click()
MSComm1.Output = "6"
End Sub
Private Sub cmd7_Click()
MSComm1.Output = "7"
End Sub
Private Sub cmd8_Click()
MSComm1.Output = "8"
End Sub
Private Sub Command1_Click()
Text2.Text = " "
End Sub
Private Sub Command2_Click()
Timer2.Enabled = True
End Sub
Private Sub Command3_Click()
MsgBox "Thoat Khoi Chuong Trinh"
MSComm1.Output = "0"
End
End Sub
Private Sub Command4_Click()
Timer2.Enabled = False
End Sub
Private Sub Form_Load()
i = 0
' cai dat thong so cho cong com...." COMFIG FOR COM PORT "
Timer2.Enabled = False
With MSComm1
.CommPort = 1 'cong COM 1
.Settings = "9600,N,8,1" 'Baudrate=9600,Ko KT chan le,frame 8 bit,1 bit stop
.InBufferSize = 1024 'Bo dem 1024 byte thi day
.InputLen = 0 'Doc toan bo gia tri bo dem
.RThreshold = 1 'Cho phep ngat khi su kien Receiver o MSCOMM
.SThreshold = 0 'Ko cho phep ngat khi su kien Send o MMSCOMM
.OutBufferSize = 512
.InputMode = comInputModeText
.PortOpen = True 'Mo cong COM
End With
End Sub
Private Sub MSComm1_OnComm()
Dim StringIn As String
If MSComm1.CommEvent = comEvReceive Then
StringIn = MSComm1.Input
Text2.Text = Asc(StringIn)
End If
End Sub
Private Sub Timer1_Timer(Index As Integer)
Label2.Caption = "DATE: " & Day(Now) & " / " & Month(Now) & " / " & Year(Now) & " TIME: " & Hour(Time) & " : " & Minute(Time) & " : " & Second(Time)
Label1.Left = Label1.Left - 120
If Label1.Left = -2760 Then
Label1.Left = 6360
End If
End Sub
Private Sub Timer2_Timer()
i = i + 1
If i = 1 Then
MSComm1.Output = "1"
ElseIf i = 2 Then
MSComm1.Output = "2"
ElseIf i = 3 Then
MSComm1.Output = "3"
ElseIf i = 4 Then
MSComm1.Output = "4"
ElseIf i = 5 Then
MSComm1.Output = "5"
ElseIf i = 6 Then
MSComm1.Output = "6"
ElseIf i = 7 Then
MSComm1.Output = "7"
ElseIf i = 8 Then
MSComm1.Output = "8"
Else
MSComm1.Output = "0"
i = 0
End If
End Sub
- Đây là ảnh mô phỏng protues.
- Đây là giao diện VB.
- Đây là code VB.
Option Explicit
Dim i As Integer
Private Sub cmd_giamsat_Click() 'GIAM SAT'
MSComm1.Output = "g"
End Sub
Private Sub cmd_off_Click()
MSComm1.Output = "0"
End Sub
Private Sub cmd_on_Click()
MSComm1.Output = "9"
End Sub
Private Sub cmd1_Click()
MSComm1.Output = "1"
End Sub
Private Sub cmd2_Click()
MSComm1.Output = "2"
End Sub
Private Sub cmd3_Click()
MSComm1.Output = "3"
End Sub
Private Sub cmd4_Click()
MSComm1.Output = "4"
End Sub
Private Sub cmd5_Click()
MSComm1.Output = "5"
End Sub
Private Sub cmd6_Click()
MSComm1.Output = "6"
End Sub
Private Sub cmd7_Click()
MSComm1.Output = "7"
End Sub
Private Sub cmd8_Click()
MSComm1.Output = "8"
End Sub
Private Sub Command1_Click()
Text2.Text = " "
End Sub
Private Sub Command2_Click()
Timer2.Enabled = True
End Sub
Private Sub Command3_Click()
MsgBox "Thoat Khoi Chuong Trinh"
MSComm1.Output = "0"
End
End Sub
Private Sub Command4_Click()
Timer2.Enabled = False
End Sub
Private Sub Form_Load()
i = 0
' cai dat thong so cho cong com...." COMFIG FOR COM PORT "
Timer2.Enabled = False
With MSComm1
.CommPort = 1 'cong COM 1
.Settings = "9600,N,8,1" 'Baudrate=9600,Ko KT chan le,frame 8 bit,1 bit stop
.InBufferSize = 1024 'Bo dem 1024 byte thi day
.InputLen = 0 'Doc toan bo gia tri bo dem
.RThreshold = 1 'Cho phep ngat khi su kien Receiver o MSCOMM
.SThreshold = 0 'Ko cho phep ngat khi su kien Send o MMSCOMM
.OutBufferSize = 512
.InputMode = comInputModeText
.PortOpen = True 'Mo cong COM
End With
End Sub
Private Sub MSComm1_OnComm()
Dim StringIn As String
If MSComm1.CommEvent = comEvReceive Then
StringIn = MSComm1.Input
Text2.Text = Asc(StringIn)
End If
End Sub
Private Sub Timer1_Timer(Index As Integer)
Label2.Caption = "DATE: " & Day(Now) & " / " & Month(Now) & " / " & Year(Now) & " TIME: " & Hour(Time) & " : " & Minute(Time) & " : " & Second(Time)
Label1.Left = Label1.Left - 120
If Label1.Left = -2760 Then
Label1.Left = 6360
End If
End Sub
Private Sub Timer2_Timer()
i = i + 1
If i = 1 Then
MSComm1.Output = "1"
ElseIf i = 2 Then
MSComm1.Output = "2"
ElseIf i = 3 Then
MSComm1.Output = "3"
ElseIf i = 4 Then
MSComm1.Output = "4"
ElseIf i = 5 Then
MSComm1.Output = "5"
ElseIf i = 6 Then
MSComm1.Output = "6"
ElseIf i = 7 Then
MSComm1.Output = "7"
ElseIf i = 8 Then
MSComm1.Output = "8"
Else
MSComm1.Output = "0"
i = 0
End If
End Sub
EmoticonEmoticon