MicroController Pros Home Page
My Account  Cart Contents  Checkout  
  Store » PCAM+ 8051 Tutorial 9 My Account  |  Cart Contents  |  Checkout   
Quick Find
 
Enter keywords to find the product you are looking for in the Quick Find field above

or use
Advanced Search
Categories
Accessory Boards->
8051->
ADI Blackfin
Arduino->
ARM->
Atmel AVR->
Cypress PSoC
Freescale->
FTDI->
Fujitsu
Locktronics
Maxim
Microchip PIC->
MIPS
National Semiconductor
Parallax->
Renesas->
Silicon Labs
ST Microelectronics->
Texas Instruments->
Tibbo->
Zilog
Books->
Displays->
E-Blocks
EEPROM/EPROM/FLASH
Embedded Ethernet->
Embedded Software->
I/O Modules->
Parts & Components->
Pick & Place Tools
Programmable Logic (PLD)
Prototype PCBs->
Robotics
ROM/Flash Emulators
Test & Measurement->
Tutorial CD-ROMs
Universal Programmers->
Wireless->
Information
Intro to Embedded Tools
Embedded News Digest
Useful Resources
Shipping & Returns
Warranty & Liability
Privacy Notice
Conditions of Use
Contact Us
PCAM+ 8051 Tutorial 9: RS232 at 2400 Baud

Back to PCAM+ page.


Back to Chapter 8

RS232 Interface with 8051 Microcontroller

RS232 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:

  • 1. Waits for characters to be received from the PC
  • 2. Displays the received character on the 162 LCD
  • 3. Echoes the same character to the PC
So both the PCAM+ LCD and your PC terminal program will show the characters you type.

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 calculation

Speed 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

;For LCD
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

LCALL INIT_LCD
LCALL CLEAR_LCD

START:
       MOV A,#'W'
       LCALL WRITE_TEXT
       MOV A,#'a'
       LCALL WRITE_TEXT
       MOV A,#'i'
       LCALL WRITE_TEXT
       MOV A,#'t'
       LCALL WRITE_TEXT
       MOV A,#'i'
       LCALL WRITE_TEXT
       MOV A,#'n'
       LCALL WRITE_TEXT
       MOV A,#'g'
       LCALL WRITE_TEXT

MOV PCON,#00h
MOV TMOD,#00100000b
MOV TH1,#baudnum
SETB TR1
MOV SCON,#40h
SETB REN

MAIN:
       JBC RI,RECEIVED_CHAR
       NOP
       SJMP MAIN ;Loop till character is not received
       LCALL CLEAR_LCD

RECEIVED_CHAR:
       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
       MOV     SBUF,A
       ACALL Transmit
LJMP MAIN

Transmit:               ;Wait till transmit is not over.
   JNB TI,Transmit
   CLR TI
   RET

INIT_LCD:
       CLR RS
       MOV PDATA,#00110100b
       SETB EN
       CLR EN
       LCALL WAIT_LCD

       CLR RS
       MOV PDATA,#00111100b
       SETB EN
       CLR EN
       LCALL WAIT_LCD

       CLR RS
       MOV PDATA,#0Ch
       SETB EN
       CLR EN
       LCALL WAIT_LCD

       CLR RS
       MOV PDATA,#06h
       SETB EN
       CLR EN
       LCALL WAIT_LCD
RET

;NOTE: Copy character to be displayed in A
WRITE_TEXT:
       SETB RS
       MOV PDATA,A
       SETB EN
       CLR EN
       LCALL WAIT_LCD
RET

CLEAR_LCD:
       CLR RS
       MOV PDATA,#01h
       SETB EN
       CLR EN
       LCALL WAIT_LCD
RET

WAIT_LCD:
       CLR EN
       CLR RS
       SETB RW
       MOV PDATA,#0FFh
       SETB EN
       MOV A,PDATA
       JB ACC.7,WAIT_LCD
       CLR EN
       CLR RW
RET

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.

Shopping Cart more
0 items
What's New? more
LPC4350 Development Board, USB-H, 2x CAN, RS485, Audio, Ethernet
LPC4350 Development Board, USB-H, 2x CAN, RS485, Audio, Ethernet
US$145.00
Bestsellers
01. 2x10 Pin male header, 2.54mm, 7.5mm plastic spacer
02. 2x5 (10-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
03. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 20cm
04. 2x10 (20-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
05. PIC16F877A-I/P Microcontroller, 40 DIP, 20 MHz
06. USB-to-Serial Cable: 5V TTL, Audio Plug Connector, Reverse RX/TX
07. LCD 4-mux, 7.1 digits x 7 segments, progress bar, arrows
08. Power Supply 3-12V DC, U.S. plug, 6 connection tips
09. 2x3 (6-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
10. PIC16F628A-I/P Microcontroller, 18 DIP, 20 MHz
Specials more
Cirrus Logic EP9302 (ARM9) Board, 2x USB, 2x RS232, Ethernet, SD
Cirrus Logic EP9302 (ARM9) Board, 2x USB, 2x RS232, Ethernet, SD
US$174.99
US$119.00
Reviews more
Flex Sensor 4.5"
crap... Design flaw where the metal terminals meet the plast ..
1 of 5 Stars!
  Friday 18 April, 2014   List of all our Products

Copyright © 2003-2014 MicroController Pros LLC
Powered by osCommerce