Compact RS232 to TTL converter
Update: There is a new low voltage version of the MAX232 called MAX3232. I updated my converter revently and it works fine with 3V from two batteries now.
When programming microcontrollers debugging is inevitable. Options like JTAG or DebugWIRE exist, but they are usually too expensive for most entry level DIYers. Another simple and controller-independent option is to utilise the USART for debugging. It's integrated into nearly all of today's microcontrollers. The only problem here is to convert the two serial signals RXD and TXD from the controller levels, usually TTL, to the EIA-232 levels as found in the serial port of every PC. Fortunately there is the MAX232, which converts signals between both levels.
This article describes how to build an extremely compact converter circuit built around the MAX232. With a size of just 14 x 17 mm (0.55 x 0.67 inch) the PCB is small enough to fit into a D-SUB plug housing as shown in the title picture above. When designing a new AVR circuit you just have to connect the converter to the AVR and you're ready to communicate in both directions.
The circuit is shown in the images below. It's a standard application of the MAX232, the only point worth mentioning is that the thin SO16 version of the MAX232 was used in the PCB layout. There are alternative versions of the original Maxim chip, e.g. from TI (also named MAX232) or ST called ST232 (my favourite). They are usually cheaper then the Maxim equivalents. Some older MAX232 versions need 1 uF capacitors instead of 100 nF required by the newer types. All five caps on the PCB are 0805 SMD packages. The prototype PCB shown in the first picture is mirrored because of a stupid fault, however all files provided for download below are correct.
At this point I'd like to mention that soldering SMD parts is not as difficult as it might seem. It doesn't require any special motor skills. All you need is a soldering iron with a sharp tip and thin solder!
Example code
The source code below can be used to test the proper functioning of the converter. It sends the string 'hello' to the serial port and then echos back all characters send to the controller. It is based on the code snippets given in the ATmega8 data sheet. The USART settings in the code below are:
- 38400 baud/s data rate
- 8 data bits
- 1 stop bit
- no parity bits
#define F_CPU 8000000dontforgettochange #define BAUD 38400 #define MYUBRR F_CPU/16/BAUD-1 #include <stdlib.h> #include <avr/io.h> void USART_Init( unsigned int ubrr) { /* Set baud rate */ UBRRH = (unsigned char)(ubrr>>8); UBRRL = (unsigned char)ubrr; /* Enable receiver and transmitter */ UCSRB = (1<<RXEN)|(1<<TXEN); /* Set frame format: 8data, 2stop bit */ UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0); } void USART_Transmit( unsigned char data ) { /* Wait for empty transmit buffer */ while ( !( UCSRA & (1<<UDRE)) ) ; /* Put data into buffer, sends the data */ UDR = data; } unsigned char USART_Receive( void ) { /* Wait for data to be received */ while ( !(UCSRA & (1<<RXC)) ) ; /* Get and return received data from buffer */ return UDR; } int main(void) { unsigned char data; USART_Init ( MYUBRR ); USART_Transmit('h'); USART_Transmit('e'); USART_Transmit('l'); USART_Transmit('l'); USART_Transmit('o'); for(;;) { data = USART_Receive(); USART_Transmit(data); } }
There is a variety of terminal programs for serial communication. HyperTerminal is included in Windows, HTerm or Br@y++ Terminal are good alternatives. Don't forget to deactivate all protocols (flow control: none) when sending data to the controller.
Download
All files necessary to build the converter are provided below. If the given PCB layout doesn't fit into your plug, use the Eagle files to rearrange the parts as necessary.
rs232plug_layout.pdf - PCB layout
rs232plug.sch - Eeagle schematics
rs232plug.brd - Eeagle board
rs232.c - Example C code
Happy communicating!
Links
http://www.st.com/stonline/products/literature/ds/6420/st232c.pdf
ST232 data sheet
http://en.wikipedia.org/wiki/RS-232
Article about RS-232 in Wikipedia
http://www.der-hammer.info/terminal/
HTerm - a nice and compact terminal program
http://braypp.googlepages.com/terminal
Br@y++ Terminal - another terminal program
PHP Script Throwaway Email Throwaway Email
can you tell me the output voltage of the rs232 tx,rx
http://www.tronisoft.com/cat_rs232tottl.php
or USB to TTL cables
http://www.tronisoft.com/cat_usbtottl.php
USB to TTL is great for debugging from PICs or ATMEL devices if your Laptop does not have a serial port.
Joe
Can be seen at http://www.eercz.com
regards