Back to PCAM+ page.
Back to Chapter 8
RS232 Interface with 8051 MicrocontrollerRS232 is a serial communication protocol commonly used to communicate between PC and various electronic equipment. These days, USB is more commonly used and has almost replaced the RS232 serial ports on every electronic system.
However, the simple microcontrollers like 89S52 or most 8-bit AVRs and PICs have no direct USB facility built in. These microcontrollers depend on Serial-to-USB converters, such as the FT232RL from FTDI. This IC connects to a PC via USB and on other side connects to a legacy microcontroller over TTL-level serial lines.
The 89S52 has one TTL serial port (5V) built in.
Pin P3.0 acts as Rx (receive) and pin P3.1 acts as Tx (transmit) for the serial communication.
The PCAM+ kit has one RS232 serial port interface section (Section 4) located on the top right corner. Locate the RX and TX pins from this section.
The application we'll use here does following:
So both the PCAM+ LCD and your PC terminal program will show the characters you type.
- 1. Waits for characters to be received from the PC
- 2. Displays the received character on the 16×2 LCD
- 3. Echoes the same character to the PC
In addition to the PCAM+ kit, you will need an RS232 M-F serial cable to connect the PCAM+ board to your PC/laptop. If your PC does not have an RS232 port, then you will need an RS232-to-USB converter cable. You also will need a serial terminal program on your PC (not HyperTerminal). There are freeware options you can choose from online.
Make the following connections:
- Connect a serial M-F cable between your PC and the PCAM+ kit's serial port.
- Connect Rx & Tx pins from the serial port section of the PCAM+ kit to the 89S52's P3.0 & P3.1 pins respectively.
- Connect RS, RW, EN pins of the LCD section to P2.0, P2.1, P2.2 of the 89S52.
- Connect pins 0-7 of the LCD to the respective P0 pins of the 89S52.
Some details about baud rate calculationSpeed of serial communication is called baud rate. The speed, i.e. baud rate, is set with the help of Timer1. In other words, when using serial communication, Timer1 is unavailable for other use; Timer1 gets dedicated to serial communication when Serial Communication is enabled.
You must set appropriate values to TMOD, TCON, TL1, TH1 registers to configure baud rate, and write appropriate values for SCON, SBUF, PCON.7 to enable Serial Communication. Timer1 is used in 8-bit auto reload mode, thus TMOD value will be 00100000b. The value of TL1 increments every machine cycle. The value of TH1 acts as the "reload" value for TL1. In other words, TL1 starts incrementing not from zero but from the same value as saved in TH1. If TH1 has a value 100, then after TL1 overflows, it will re-start with 100 again.
The formula for TH1 value calculation is as follows:
TH1 = 256 − ( (Crystal Frequency/384) / Baud Rate )This application will communicate at 2400 baud rate. The crystal frequency is 11.0592 MHz, i.e. 11059200 Hz. Thus we calculate 256−((11059200/384)/2400)=244. The hex value of 244 is F4, so we will set TH1 to 0F4H.
SCON value will be 01000000b. Bit 6 of SCON is set to enable 8-bit UART mode of Serial Communication.
The serial communication starts only after the TR1 bit is set HIGH.
Set REN bit to enable serial Receive.
Here is the program developed to enter into 8051IDE:
baudnum EQU 0f4h
EN EQU P2.2 ;change as per your connections
RS EQU P2.0 ;change as per your connections
RW EQU P2.1 ;change as per your connections
PDATA EQU P0 ;change as per your connections
SJMP MAIN ;Loop till character is not received
MOV A,SBUF ;Received char is stored in SBUF, make a copy of it
MOV R7,A ;Copy backup of received char
CLR RI LCALL WRITE_TEXT
;Now transmitting back the received char
MOV A,R7 ;transmit back from backup
Transmit: ;Wait till transmit is not over.
;NOTE: Copy character to be displayed in A
Once you have assembled the code and programmed the 89S52, power up the PCAM+ board. The LCD should display "Waiting" message.
Set the terminal program on your PC (again, not HyperTerminal!) to 2400 Baud Rate, and select the correct COM Port Number for your serial port.
Send some characters from the PC's terminal program. The PCAM+ LCD will display them as they are received, and will send them back to your PC where your terminal program will show them as received also.
Proceed to Chapter 10
Back to PCAM+ page.