Home Parallel Port Serial Port Serial Info Parallel Info USB .NET Webboard I/O Today API Project Link

 Microcontroller interfacing in visual basic programming.

8051 Microcontroller interfacing Project Chapter 2

Thai version Click here
   From chapter lastest that i had have present article about communicate with Microcontroller Serial Port .

    8051 Microcontroller interfacing with Serial Port Project

    About this part will be present article for PC programming in communicate with micocontroller
That i prefer Visual Basic which will be not over explain in programming detail as you can learning in Visual Basic programming for communicate to Serial Port thus as link below

How to communicate to Serial Port in Visual Basic programming.

Project Method

   We are going to create a serial port setting value form for control command which send to microcontroller serial port . Also if event is pressed each a control button that it will be send command to controller for perform with defind condition such as send character "1" is perform micocrontroller for send value to parallel port "P1" after that will be await to receive return value from micocrontroller in this is "C" for confirm the perform function finished.



Create Form
    We are going to design with using VB Control which name is MSComm You should be defind in Custom Control which go to menu    Project--->Companents After that select item name is MSComm that will be show a yellow telephone icon.
Drag the icon from tool dialog and drop it to form of your project.
After that drag and drop Frame control for Communication and Control I/O Microcontroller part

    Regrading arrange all control , I had have put explanation with in picture


Picture 1 : Form designed

Function programming of this program

First listing is command button for Setting Communication PC Serial Port

Private Sub Command8_Click()
   On Error GoTo Errlabel
' Defind value Setting is ="Baud Rate,Parity(if not using put "N"),Data bit,Stop bit"  Ex. 9600,N,8,1 

   MSComm1.Settings = Text1.Text   ' As we are going to put value Setting in Textbox                 

   MSComm1.CommPort = Combo1.ListIndex + 1   ' To defind Com port
   MSComm1.RThreshold = 1  
'Setted value to use Event-driven when has data coming
   MSComm1.PortOpen = True  
' Open Com port for availability
   MSComm1.InputLen = 0
    ' Defind to read all data in Buffer of MSComm
Exit Sub
' For error checking of program
If Err.Number = 8002 Then MsgBox "Select com Port", vbInformation, "8051 Control I/O"
End Sub

Control button set for send value out to 8051 microcontroller

' Command in sending code to microcontroller for control each LED
Private Sub Command1_Click()
MSComm1.Output = "1"   
' Send out Number 1 is Ascii =31(Hex) to on LED1
valLED1 = Not valLED1   
' Defind value for check pressing button LED1
End Sub

Private Sub Command2_Click()
MSComm1.Output = "2"   
' Send out Number 1 is Ascii =32(Hex) to on LED2
valLED2 = Not valLED2   
' Defind value for check pressing button LED2
End Sub

Private Sub Command3_Click()
MSComm1.Output = "3"  
' Send out Number 1 is Ascii =33(Hex) to on LED3
valLED3 = Not valLED3  
' Defind value for check pressing button LED3
End Sub

Private Sub Command4_Click()
MSComm1.Output = "4"   
' Send out Number 1 is Ascii =34(Hex) to on LED4
valLED4 = Not valLED4   
' Defind value for check pressing button LED4
End Sub

Private Sub Command5_Click()
MSComm1.Output = "5"   
' Send out Number 1 out is Ascii =35(Hex) to on LED5
valLED5 = Not valLED5   
' Defind value for check pressing button LED5
End Sub

Private Sub Command6_Click()
MSComm1.Output = "6"  
' Send out Number 1 is Ascii =36(Hex) to on LED6
valLED6 = Not valLED6  
' Defind value for check pressing button LED6
End Sub

Private Sub Command7_Click()
MSComm1.Output = "7"   
' Send out Number 1 out is Ascii =37(Hex) to on LED7
valLED7 = Not valLED7    ' กำหนดค่าเพื่อตรวจสอบการกดปุ่ม LED7
End Sub

Private Sub Command9_Click()
MSComm1.Output = "0"  
' Send out Number 0 is Ascii =30(Hex) to off all LED
valLED = Not valLED       
' Defind value for check pressing button ED0
End Sub

Receive value from microcontroller function with in MSComm1_OnComm()

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent  
' Occur event Comm-Event
      Case comEvReceive  
' When it has receive from Com Port
      Dim Buffer As Variant   
' Variable for save data when receive occurrence
      Buffer = MSComm1.Input   
' Save data in variable
      Label2.Caption = "Return = " & Buffer
' In case when microcontroller send data coming to confirm perform function finished .
' check value that receive ,Is it "C" ? and valLED1-7 of each button
' For change color of circle to red(meaning is ON LED)and If it is white(meaning is off LED)
On Error GoTo Errlabel
      If Buffer = "C" Then
      If valLED = True Then
            For x = 0 To 7 Step 1
                  Shape1(x).FillColor = &HFFFFFF
            Shape1(0).FillColor = &HFF&
            valLED1 = False
            valLED2 = False
            valLED3 = False
            valLED4 = False
            valLED5 = False
            valLED6 = False
            valLED7 = False
            Command9.Caption = "ON ALL"
' If button remain "ON ALL" ,It can not control up to press button to "OFF ALL"
Exit Sub
          Shape1(0).FillColor = &HFFFFFF
          Command9.Caption = "OFF ALL"
      End If
      If valLED1 = True Then
            Shape1(1).FillColor = &HFF&
            Shape1(1).FillColor = &HFFFFFF
      End If
      If valLED2 = True Then
            Shape1(2).FillColor = &HFF&
            Shape1(2).FillColor = &HFFFFFF
      End If
      If valLED3 = True Then
            Shape1(3).FillColor = &HFF&
            Shape1(3).FillColor = &HFFFFFF
      End If
      If valLED4 = True Then
            Shape1(4).FillColor = &HFF&
            Shape1(4).FillColor = &HFFFFFF
      End If
      If valLED5 = True Then
            Shape1(5).FillColor = &HFF&
            Shape1(5).FillColor = &HFFFFFF
      End If
      If valLED6 = True Then
            Shape1(6).FillColor = &HFF&
            Shape1(6).FillColor = &HFFFFFF
      End If
      If valLED7 = True Then
            Shape1(7).FillColor = &HFF&
            Shape1(7).FillColor = &HFFFFFF
      End If
   End If
End Select
            Exit Sub
End Sub


Test Program( Use same program for simulate to send value)

    When you did finished in compile file to EXE if you would like to test program but it has not connent to 8051 microcontroller that can do it with open the program 2 windows at the same time that i had create "Test Send Complete " button for test return value which send to program as informed function of microcontroller successful
Function step is show thus as picture below


Step for program testing picture

About Hardware,Circuit,Interface for this project can you click here


  Benefit for you from article
   1) You can understand in 8051 Microcontroller basic interfacing
   2) Understand command and function in PC programming to control microcontroller
   3) You can programming in computer to control electrical devices or apply it to control more device.br>
   document Referrent

   1. 8051 Tutorial Serial Communication

   2. http://www.smartdata.com.au/8051/serial.txt 


Copyright (c) 2000 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com