Till now all the experiments are related to I/O port. To connect the MCU to PC, serial port is a must to learn.
This experiment shows how the MCU receives data from PC and then display on LED displays.


The Fundamental of Serial Port
As we know, 8051 MCU is equipped with a full duplex serial communication port. However, computer’s serial port works on RS-232 level, while MCU’s is TTL level. Thus, there must be a level converter between RS-232 and TTL. Here we use MAX232 chip to convert the two types of levels.
The MAX232 device consists of two line drivers, two line receivers, and a dual charge-pump circuit with ±15-kV ESD protection pin to pin (serial-port connection pins, including GND). The device meets the requirements of TIA/EIA-232-F and provides the electrical interface between an asynchronous communication controller and the serial-port connector. The charge pump and four small external capacitors allow operation from a single 5-V supply. The device operates at data signaling rates up to 120 kbit/s and a maximum of 30-V/µs driver output slew rate.


The simplest way to use MAX232 is like the below diagram.

To read the data from MCU on PC, a windows software is needed, the SComAssistant V2.1.

In SComAssistant, parameters like serial port number, Baudrate, check-sum can all be set. It is very important to make sure the settings between PC and MCU are the same.
Circuit Design

Software Design
#include “reg51.h” //include the 8051 register definition header file
//common-anode display code table
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0×99,0×92,0×82,0xf8,0×80,0×90};
unsigned char dat; //define the global variable
void Init_Com(void) //initialize the serial port
{
TMOD = 0×20;
PCON = 0×00;
SCON = 0×50;
TH1 = 0xFd; //baud rate: 9600bps
TL1 = 0xFd;
TR1 = 1;
}
void delay(void) //LED display delay
{
int k;
for(k=0;k<600;k++);
}
serial()interrupt 4 using 1 //interrupt service routine
{
if(RI)
RI=0; //clear the receiving flag
dat=SBUF; //receive data
}
void display(int k) //LED display function
{
P2=0xfe; //digital select
P0=tab[k/1000]; //display the 4th number
delay(); //delay
P2=0xfd; //digital select
P0=tab[k%1000/100]; //display the 3rd number
delay(); //delay
P2=0xfb; //digital select
P0=tab[k%100/10]; //display the 2nd number
delay(); //delay
P2=0xf7; //digital select
P0=tab[k%10]; //display the 1st number
delay(); //delay
P2=0xff; //digital select
}
void main() //main function
{
P2=0xff; //initialize I/O port
P0=0xff; //initialize I/O port
EA=1; //enable all interrupts
ES=1; //enable serial interrupt
Init_Com(); //invoke serial port initialization
while(1) //loop
{
display(dat); //invoke display function
}
}