Tag Cloud

rf receiver rx3310a with 8051 ac motor speed control z85c30 schematic for a mixed data acquisition board circuit for moving message display usb based device controller ultrasonic sensor with microcontroller pc device control 8051 c serial   device control through bluetooth from symbian os mobiles   led chaser   c program from adc0804   keypad interfacing   dc controlled tone   embedded systems   serial communication   microcontrollers for embedded systems   light sensor   implementing a calculator using peripherals like a keypad and lcd    

Receiving Data From The Microcontroller


In the last article we sent data to a microcontroller and had it respond to the data that we sent...

In this article we are going to learn how to receive data from a microcontroller and make the PC respond. For simplicity we are going to get the Port 0 and send to the PC for us to receive using VB.

Visual Basic at PC side


 To get started open Visual Basic.



On To The VB Code
Now that the form is set up and ready, we need to get a quick understanding of how the MSComm control can receive data from the serial port. There are basically two methods, polling the port and responding to communications events.

Polling the port is done by setting up a timer to check the buffer for data, and if data is there, process it. Polling is better when you have variable length data that starts and ends with header and footer bytes respectively.

The event driven method is designed more for receiving fixed length data. It is also better because you don't waist time polling the buffer for data if none is there. Instead the MSComm control will tell you when data is there by firing the OnComm() event. This event fires just like a Click() event would fire if you clicked on a Command Button, only it is not the users action that fires this event, something must happen at the serial port.

When the OnComm() event is fired you must check the value of the CommEvent property to see what exactly happened. The CommEvent property will contain a different value for each different kind of communication event that occurs.

In this project we are only concerned with the comEvReceive constant which has the value of 2 and is fired when data is available in the buffer.

Now that we have a feel for how the MSComm control will assist us in receiving data, lets first set up the MSComm control.
Copy this commented code to your project...

Private Sub Form_Load()

' Fire Rx Event Every single Bytes
MSComm1.RThreshold = 1

' When Inputting Data, Input 1 Byte at a time
MSComm1.InputLen = 1

' 9600 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"
' Disable DTR
MSComm1.DTREnable = False

' Open COM1
MSComm1.CommPort = 1
MSComm1.PortOpen = True

End Sub


You may notice that there are two new properties in the above code that have yet to be explained in these articles. The first is RThreshold. In this example, setting the RThreshold property equal to 1 tells the MSComm control to fire the comEvReceive event when there are at least one bytes available in the buffer. The second property, InputLen, tells the MSComm control to only give us the first  byte when we request data from the buffer.

With that out of the way, lets take a look at the code that is executed when the OnComm() event is fired. The comments should help you along...

Private Sub MSComm1_OnComm()
Dim Data As String

' If comEvReceive Event then get data and display
If MSComm1.CommEvent = comEvReceive Then
    Data = MSComm1.Input        'get data
    lbldata.caption = Asc(Data1)    'convert to ASCII and display
End If
End Sub


Finally we need to close the comm port when the VB project unloads so...

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False    'Close the COMM port
End Sub


That's all at the PC side

Microcontroller Side


Program the Microcontroller with the following code, it reads the port P0 and sent the data to the PC.

   ORG     00H            ; Reset
    MOV TMOD,#20H        ;enable timer1, mode 2 (auto reload)
    MOV TH1,#0FDH        ;9600 Baud rate
    MOV SCON,#50H        ;8 bit, 1 stop bit, REN enabled
    SETB TR1

    MOV P0,#0FFH        ;make P0 as input port
AGAIN:MOV A,P0            ;Read P0
    MOV SBUF,A            ;send data
HERE:JNB TI,HERE            ;wait for data to be transferred
    CLR TI            ;clear TI for next char
    SJMP AGAIN            ;keep doing it


After Burning up the above Program to the microcontroller  run your VB project, power up the microcontroller, change the values of the switches. and the datas will be displayed on the PC.

Click here to download it...