18F MAX232 Example

Developed with the
Swordfish
compiler
I have done an
example earlier utilizing the
DS275, it does the same
job, but does not require any additional components. The
MAX232 requires 4
external capacitors, but is much cheaper than the
DS275. A great benefit is that
the Pins that require capacitors are usually 3 pins apart, allowing easy
installation of the capacitors - even on
protoboards.
The
DS275 is $3.75, where as the
MAX232 is $0.93 (prices
are from Core Electronics). The external
capacitors are very cheap, in fact, they are only $0.07 each for electrolytic.
Connect the
capacitors and
PIC micro to the
MAX232 like the following diagram
*** EDIT;
The 4.7K to 10K
resistor was added as I have found in practical experiences that
the
MAX232 is very noise sensitive, and a pull-up
resistor helps keep data
integrity!

Note the PIC's power supply/oscillator are not shown
Be sure to connect 5V to
Pin 16 and Earth to Pin 15 on the
MAX232. Note that the
shaded parts of
the capacitors are the negative terminals.

As you can see, the
capacitors are no hassle, as they all line up to pins that are 1 or 2 apart.
Make sure you get the polarity right on Pin 2 and 6, they can be misleading.
Something to be aware of
here is that different types of
MAX232 chips require
different
external capacitors. This example covers the
MAX232, but if your using a
MAX232A, then 0.1uF capacitors are required.
Here's a picture of a COM
port on the back of your PC. The following diagram identifies the Pin numbers,

Cut off Pins 6 to 9 on your
PCB
DB9 adaptor (The Pins that go to the PCB - not the ones inside the adaptor).
They are not required. Now your DB9 connector can be used on any
protoboard/breadboard.
Some super/hot glue might be required if your going to be unplugging it all the
time (just a little bit placed under the front part to give it more stability)

If your not sure what Pin
does what on the end of your COM lead - check it first before wiring. Your could
damage your motherboard or
MAX232 chip. Grab a multimeter and throw it on
resistance, now you can be sure you have the right pin on the other end. Making
your own DB9 connectors is just as easy. Grab a
Female
Ribbon Cable Connector and a
Male Cable Connector


The smallest
Ribbon cable
from Futurlec is 10 Core - we only require 9. To overcome this, just snip off a
couple of centimetres on either end of the same wire. Now make sure that you
have the 9 cores lined up (no need to pull them apart - they sit 'as is' in the
adaptor), and then press down firmly on the clamp - you may need to use pliers,
and carefully push them together.
If you have the soldering
skills for
solder cups, then you can use normal hook-up wire, and will only require 3
leads from each adaptor.

HyperTerminal
Every Windows XP comes with
HyperTerminal, but can also be
downloaded
here. HyperTerminal is found in your start menu via, "Start Menu",
"Accessories", "Communications", "HyperTerminal".
Watch
this video
for more information on HyperTerminal
RS232 holds its power by
using a single wire each directional data line. Why is this handy? Well when
your program is bit crouching, you can easily send "Debug" information out with
a simple command (in this case USART.Write(data)). For example, your program
seems to stop and you have no idea why, well if you put a Debug line
before every loop, you could easily identify where your program gets "stuck".
Eg,
USART.Write("Entering Loop 1", 13, 10)
Repeat
Until PORTB.1 = 1
USART.Write("Entering Loop 2", 13, 10)
Repeat
Until PORTB.1 = 0
USART.Write("Finished Looping", 13, 10)
The
,
13, 10 sends an Enter key ASCII code, followed by the Line Feed
ASCII Code. For more information, have a look at the Swordfish plug-in for ASCII
codes.

When your using the USART commands in Swordfish, be sure to include the file
"USART.bas" at the start of your program. Now you have all the USART commands at
your disposal, and to send data to your PC is as easy as;
Device = 18F458
Clock = 20
Include "usart.bas"
SetBaudrate(br19200)
USART.Write("Hello World", 13, 10)
If you want to send
variables to your PC, you first have to convert them into ASCII (string) data.
So be sure to include the file "CONVERT.bas" at the start of your program;
Device = 18F458
Clock = 20
Include "usart.bas"
Include "convert.bas"
Dim Variable_1 As Word
// Start Of Program...
Variable_1 = 1500
SetBaudrate(br19200)
USART.Write("Hello World", 13, 10) // string
USART.Write("Binary : ", BinToStr(Variable_1), 13, 10) // binary number
USART.Write("Hex : ", HexToStr(Variable_1), 13, 10) // hex number
USART.Write("Decimal : ", DecToStr(Variable_1), 13, 10) // decimal number
The PC would receive and
display the data as shown below

Click here to
watch a video demonstration of setting up HyperTerminal on your PC
Where you can get the components;

 | Site Tutorial Index |
|  | 16F PIC Examples |
|  | 18F PIC Examples |
| |  | 7 Segment Displays |
| |  | 7 Segment Displays |
| |  | RS232 and UART |
| |  | Code Snippets |
|  | Handy Tips |