Personal.X-Istence.com

Bert JW Regeer (畢傑龍)

MAX232 level shifter

While I have been playing with hardware for a while, I still am not entirely sure of myself when I am reading data sheets. I needed a way to level shift standard serial (RS-232) to TTL/CMOS levels. Not to long ago I had ordered a few MAX232's from Ti with the idea that I would be using them to build myself a simple cable so that I could connect my USB to serial cable to my Fonera.

Well, today I finally got my act together and got all the parts I needed, threw them on a quick breadboard and powered it on. No magic smoke, all is well so far, but I don't really want to plug my Fon into it just yet. Those are rather expensive to replace if I made a mistake, although at this point it was rather unlikely. I figured I'd use a Parallax SX28 I had lying around. SX 28 chips are dirt cheap, so even if I did make a mistake and accidently fried one it would not be that big of a deal. So I gave the SX28 the following code:

' =========================================================================
'
'   File......
'   Purpose...
'   Author.... Bert JW Regeer
'              Copyright (c) 2008 Bert JW Regeer
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... bertjw@regeer.org
'   Started... 2009-06-08
'   Updated...
'
' =========================================================================

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------

' Simple program that reads in a byte and outputs it again over serial.
' Kind of like the echo service on Unix computers!

' -------------------------------------------------------------------------
' Conditional Compilation Symbols
' -------------------------------------------------------------------------

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX, BOR42
FREQ            4_000_000
ID              "MAX232"

' -------------------------------------------------------------------------
' I/O Pins
' -------------------------------------------------------------------------

Sout    PIN RC.1 OUTPUT
Sin PIN RC.0 INPUT

' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

Baud    CON "N9600"

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

tmpB1   VAR Byte

' =========================================================================
' INTERRUPT
' =========================================================================

' RETURNINT

' =========================================================================
  PROGRAM Start
' =========================================================================

' -------------------------------------------------------------------------
' Subroutine / Function Declarations
' -------------------------------------------------------------------------

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
    tmpB1 = "a"

Main:
    SERIN Sin, Baud, tmpB1
    SEROUT Sout, Baud, tmpB1
    GOTO Main</pre>

(Template thanks to Jon Williams from EFX-Tek.)

As the code suggests, this is a simple echo, it turns the device into the echo service that used to be commonly found on Unix servers, which basically echoed what you had sent them right back at you, with one twist, this one just echo's each byte whereas with the service you generally had to send a newline.

Realterm with echo shown

As you can see it is working correctly, the first H got gobbled up somewhere along the line, this could either be because I am using the internal clock source for the SX28, which is known to be rather unstable at times, or because I am running this all from a virtualised Windows XP and the timing on the USB to Serial device is not correct.

I have hereby proved that my MAX232 wiring is all correct and functions as expected, and all without any magic smoke.