Monday, November 19, 2007

Sending Data From The PC to a Microcontroller

In this article we are going to learn how to send data to a microcontroller and make the microcontroller respond to the data. For simplicity we are just going to send data to turn on and off an LED which is connected at port P2.0.

So lets start off with designing a communications protocol. From VB we will send an ASCII 255, as a synch byte, and the on/off state (0 or 1). The microcontroller will wait for two (2) bytes of data. After these two bytes of data are received, it will then either switch on/off the LED

Visual Basic at PC side
To get started open Visual Basic.
Start a new Standard EXE.
Next go to the Project | Components... menu
Check the Microsoft Comm control 6.0.
Click OK.
Next double-click on the yellow phone in the toolbar to add the MSComm control to your form.


Now add two option buttons from the tool bar name as "opton" and "optoff", change the caption to ON and OFF for both buttopns.
Now add a command button named cmdsend from the tool bar, and chage the caption to "SEND"

On To The Code
Now that the form is set up and ready to go, we need to start adding our code to the project. The user will select a pin state from option button, and then click cmdSend to send the data to the microcontroller. So first of all we need to set up the MSComm control, and select one of the pin states to start with. So lets add the following code to our form...



Private Sub Form_Load()
On Error Resume Next
'use comm port 1
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data bits, 1 stop bit
MSComm1.Settings = "9600,N,8,1"

' Disable DTR
MSComm1.DTREnable = False

'open the port
MSComm1.PortOpen = True
End Sub

Now we just need to add the code to send the data. When the user presses cmdSend, we need to do three things.

Get the pin state...
Send the data...

Now lets put it all together and send the data when we press the cmdSend button...



Private Sub cmdsend_Click()
Dim LED As Long

' Get LED State
If opton.Value = True Then
LED = 0
Else
LED = 1
End If

' Send Out Data
MSComm1.Output = Chr$(255) & Chr$(LED)

End Sub
So we sent out the synch byte (255),followed by the LED state.

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

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

HERE:JNB RI,HERE ;wait for character to come in
MOV A,SBUF ;get data in A
CJNE A,#0FFH,DOWN ;chk if the char is synch byte ie.offh or 255
;if not jump to clear RI
CLR RI ;get ready to get next byte
WAIT:JNB RI,WAIT ;wait for character to come in
MOV A,SBUF ;get data in A
CJNE A,#00H,NXT ;chk if the char is 0
CLR P2.0 ;switch on LED
SJMP DOWN ;jump to clear RI
NXT:CJNE A,#01H,DOWN ;chk if the char is 1
SETB P2.0 ;switch off LED
DOWN:CLR RI ;get ready to get next byte
SJMP HERE ;keep getting the data
Now run the VB project, select either ON or OFF, and press send. You should see the LED turn ON when you send a ON command, and turn OFF when you send a OFF command.

Visit http://www.8051projects.info for more details


.