Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Visual Basic 6 to PIC MCU (HOW TO)

Status
Not open for further replies.

Peter_wadley

New Member
Hey,

I'd like to write a Visual Basic 6 program that can send information to a PIC and receive information from the PIC.

We are talkin as simple as possible here... bare minimum.. once I am able to make connection .. the possibilities are endless.

I would use the MAX 232 chip.. Dont know exactly how to use the chip though..

Dont know what API Id use with VB6 either..

Any links or code would be very much appreciated.

Thanks
 
Goto ;) Nigels site, or a similar one, for the PIC side of things:

Then use HyperTerminal to check out your PIC project before writing your own 232 routines in VB6. As for VB6, it's not my area of expertise, but I'm sure there are built in routines for acessing the serial ports. Search the help files for COM PORT. A quick Google turned up this:
http://www.thaiio.com/prog-cgi/0002_serial.htm
 
You can use MSCOMM ActiveX control for RS232 communications.

Place one MSCOMM control named MSComm1 and two Textboxes named txtTx and txtRx on a form. Paste the following lines in the forms code.

txtRX will display all the characters received from serial port and whatever you type in txtTX will be sent to your PIC.

Code:
Private Sub Form_Load()
	With MSComm1
		.Settings = "9600,n,8,1"	' Baud=9600, Parity=None, Data-Bits=8, Stop-Bits=1
		.CommPort = 1			' COM1
		.PortOpen = True		' Open the port
	End With
End Sub

Private Sub Form_Unload()
	If MSComm1.PortOpen = True then MSComm1.PortOpen = False
End Sub

Private Sub MSComm1_OnComm()
	If MSComm1.CommEvent = comEvReceive Then
		txtRX.Text = txtRX.Text & MSComm1.Input
	End If
End Sub

Private Sub txtTX_KeyPress(KeyAscii As Integer)
	Mscomm1.Output = Chr(KeyAscii)
End Sub

Feel free to ask if you need any more help.
 
So is the MAXIM 232 the best chip to use? How would I hook it up?
Like this:

As the whether it is the best, that is a matter of opinion. The MAX232A uses smaller capacitors but if you have a MAX232 in the junk box, then you can't beat the price. ;)
 
Peter_wadley said:
Hey,

So is the MAXIM 232 the best chip to use? How would I hook it up?

Ive set up VB6 as you've stated.

So If I were to type 'ABC' into the textbox..

Would the PIC recieve:

100 0001 for A
100 0010 for B
and
100 0011 for C ?

hi Peter,
Assuming you set the VB RS232 comms for say, 9600,n,8,1 [baud rate 9600, no parity, 8 bit data, 1 stop bit]

The bit stream for the "A" character would be
1 Start bit
8 data bits ,,,, ASCII "A" is 0x41 == 01000001
1 Stop bit

ASCII "B" is 0x41,,, "C" is 0x42

You may, on some strings to a terminal also have to send the code for CRLF [carriage return line, line feed]
after the ie: "ABC" 0x0D, 0x0A

This depends upon the program in the receiving PC, some programs look for the CRLF in the string as a line terminator.

Use the ON COMM event in VB, there are examples in the online help files within the VB program.
Use the error trapping routines shown in the help files.

The MAX232 has 2 line drivers[transmitters] and 2 line receivers,
usually you just use one transmitter on the TXD output from the PIC to PC
and one line receiver to the RXD input of the PIC from the PC.

Eric
 
Peter_wadley said:
Hey,

So is the MAXIM 232 the best chip to use? How would I hook it up?

Ive set up VB6 as you've stated.

So If I were to type 'ABC' into the textbox..

Would the PIC recieve:

100 0001 for A
100 0010 for B
and
100 0011 for C ?
Yes, the PIC will receive whatever you type in txtTx. You'll get 0x41 for A, 0x42 for B and 0x43 for C. The start and stop bits are filtered off by the PICs UART module and you need not worry about that.
I forgot to mention, set the Multiline property of txtRx to True.
 
if you will also be sending numerical data, remember mscomm sends ascii codes a byte at a time, so you could use chr$(n) where n is a byte of data to send.
this may be obvious, but shift work can make you a bit dense at times!
 
monkeybiter said:
if you will also be sending numerical data, remember mscomm sends ascii codes a byte at a time, so you could use chr$(n) where n is a byte of data to send.
this may be obvious, but shift work can make you a bit dense at times!
My code already does that :)
 
if i want to send 8 then i have to output chr$(8). your code won't do that.
what i meant was sending numeric data from the pc, rather than echoing keypresses. perhaps i should have been more specific.
 
monkeybiter said:
if i want to send 8 then i have to output chr$(8). your code won't do that.
what i meant was sending numeric data from the pc, rather than echoing keypresses. perhaps i should have been more specific.
Oh.. I got it. You are right. My mistake. :)
 
Ok,

So ive got the max 232 set up as Nigels has..

The program like kinjalgp, THANK YOU!

So..

Is PIN 9 on the MAX the TX

and

PIN 10 is the RX?

When I type in the TX box pin 10 drops about . 05 V.. so Im assuming that its the TX..

Im going to now try to program a 16f84a.. all i have right now.. so that it will talk to VB..

Can someone explain the purpose of the CAPS on the 232?

If this works out I think Im going going to have to automate my entire home :p

Basically, from what i understand.. is when I type a character..

The first bit, which lasts 104us is a 0.

This will get the pic into the INT or subroutine..

Then I will capture and place each bit into a variable..

then the stop bit will be a 1..

I would I send.. say an A to VB?

Sorry if this post is jumbled Im in a hurry..

Hope someone can answers these questions!

Thank you!
 
Peter_wadley said:
Can someone explain the purpose of the CAPS on the 232?

The MAX232 have internal charge pumps that convert TTL levels to true RS232 levels. The capacitors are required by the charge pumps to generate those voltages.

For the pin layout, check the datasheet. The figures can help you better understand the configuration of the receivers and transmitters.
 
hmm..

well ive gotten key presses in the TX box to trigger the MICRO..

I cant seem to get the MICRO to send characters to the RX txt box.

I think it might be the caps im using for the 232.. I dont have 10uf caps so I exchanged with 4.7uf.. could this be the problem?

When I send the byte out the voltage on the TX (RS232) line is changing.. so it is doing something.. VB isnt displaying the characters though..

will keep trouble shooting.
 
There could be many reasons for this. First thing may be baud rate. Or may be improper schematic.
Show your Micro's code and your schematic.
 
You can check the MAX232 independantly of the PIC.

If you are using Nigels tutorial schematic.

Remove the PIC.
On the MAX232 junper pins 9 and 10 together.
Use a terminal emulator on the PC.

Now anything you type on the PC will come in on MAX232 pin 8
at RS232 levels. It will exit the MAX232 on pin 9 at TTL levels.
Then go back into the MAX232 on pin 10 as TTL and exit on 7
as RS232 and go back to the PC.

With this setup anything you type on the PC should show up on
the PC screen. If it does your MAX232 and cable are OK.

Remove the jumper and replace the PIC.

EDIT: For this test baud rate and parity settings on the PC do not matter,
but I suggest you use whatever you intend to use with the PIC.
 
Last edited:
Instead of using a MAX232, as serial ports are becoming rare on laptop's you might want to consider implementing a USB to serial chip. e.g FT232R or FT232BM

The drivers for these FTDI chips allows you to communicate to the device like any standard serial COM port. So the code you have already implemented will still work.

Cheers

Stolzie
 
3v0 said:
If you are using Nigels tutorial schematic.

Remove the PIC.
On the MAX232 junper pins 9 and 10 together.
Use a terminal emulator on the PC.

Now anything you type on the PC will come in on MAX232 pin 8
at RS232 levels. It will exit the MAX232 on pin 9 at TTL levels.
Then go back into the MAX232 on pin 10 as TTL and exit on 7
as RS232 and go back to the PC.

With this setup anything you type on the PC should show up on
the PC screen. If it does your MAX232 and cable are OK.

Remove the jumper and replace the PIC.

EDIT: For this test baud rate and parity settings on the PC do not matter,
but I suggest you use whatever you intend to use with the PIC.

mmm, now thats how to troubleshoot!!

Its still not working like this 3v0!

Ok here is what I have going:

Power is 5V

On the DE-9..

Pins 1 & 4 & 6 are connected together.
Pins 7 and 8 are as well.

Pin 5 is going to ground on the MAX circuit.

Pin 2 is going to Pin 7 of the MAX
Pin 3 is going to Pin 8 of the MAX

There is a 47uf by cap on Pin 16 of the MAX.

Between pin 16 and pin 2 is a 22uf cap, + leg is on pin 2

On Pin 6 there is a 22uf cap going to ground, + leg is on ground side.

Between pin 1 and 3 I have (2) 4.7uf caps in parallel, since I dont have (2) 10uf caps. + leg is on pin 1

Between pin 4 and 5 is one 10uf cap, + leg is on pin 4.

and Pin 9 and 10 are jumpered together.

Ground is on Pin 15. Vdd is on 16.

That is exactly how mine is wired.

The VB6 program:

On the Form there is 2 txt boxes.

txtRX and txtTX

I then Copy and pasted this code:

PHP:
Private Sub Form_Load()
	With MSComm1
		.Settings = "9600,n,8,1"	' Baud=9600, Parity=None, Data-Bits=8, Stop-Bits=1
		.CommPort = 1			' COM1
		.PortOpen = True		' Open the port
	End With
End Sub

Private Sub Form_Unload()
	If MSComm1.PortOpen = True then MSComm1.PortOpen = False
End Sub

Private Sub MSComm1_OnComm()
	If MSComm1.CommEvent = comEvReceive Then
		txtRX.Text = txtRX.Text & MSComm1.Input
	End If
End Sub

Private Sub txtTX_KeyPress(KeyAscii As Integer)
	Mscomm1.Output = Chr(KeyAscii)
End Sub

Into the code window.

When I press 'RUN'

I try to type in the TX text box.. nothing shows up on the RX box.

argh.
 
I did not sort through the wiring. A schematic would go a long way here. The text version makes my head hurt. :p

Prior to getting into that lets make sure the PC is doing its job.

Unplug the RS232 cable from the PIC.

Type and you should see nothing.

Short pins 2 and 3 on the RS232 cable (it is still plugged into the PC but not the PIC target). To do this you can makup an DB9 with a wire soldered between 2 and 3.

With the jumper in place you should see what you are typing on the PC.

If this does not work either you did not follow these instructions or the BASIC program has problems. Maybe flow control problems. Others may have talked about that.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top