Welcome to www.agpgames.com ---- HANGZHOU KinCony ELECTRONICS CO.,LTD.
About Us Contact Us          Our Patents Our Articles        Forum      SiteMap    Chinese Version

Chapter 1. Overview Chapter 2. Emulation Guide Chapter 3. Build Your First Project Chapter 4. In System Programming
Chapter 5. USB Interface Installation Chapter 6. LED Display Application Chapter 9. FAQ Chapter 10. System Configuration and Service
 
Chapter 7. Basic Experiments
7.1. Experiment with LED 7.2. Experiment with Flowing LEDs 7.3. Experiment with Keys 7.4. Experiment with Beeper
7.5. Experiment with Relay 7.6. Experiment with LED Display 7.7. Experiment with Serial Port  
 
8.1. Experiment with Matrix Key 8.2. Experiment with Stepping Motor 8.3. Experiment with 1-Wire Digital Thermometer DS18B20
8.4. Experiment with 24C01 EEPROM 8.5. Experiment with 93C46 Serial EEPROM 8.6. Experiment with DS1302 Real-time Clock
8.7. Experiment with 8-bit Serial AD Converter ADC0832 8.8. Experiment with 1602 Character LCD Module 8.9. Experiment with 12864 Graphic LCD
8.10. Experiment with Infra-red Remote Controller 8.11. Experiment with Wireless Encoding Module Chapter 11. User Feedback

Chapter 7. Basic Experiments

7.7. Experiment with Serial Port

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,0x99,0x92,0x82,0xf8,0x80,0x90};

 

unsigned char dat; //define the global variable

 

void Init_Com(void) //initialize the serial port

{

TMOD = 0x20;

PCON = 0x00;

SCON = 0x50;      

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

}

}



COPYRIGHT2003--2008 HANGZHOU KinCony ELECTRONICS CO.,LTD. All Rights Reserved

Add:Rm 11-3-702, Pujing Jiayuan, Gongshu District, Hangzhou, Zhejiang, China.
Tel:+86-0571-87615070,Fax:+86-0571-87615070 E-mail: hificat@163.com  ICQ:164806453  Webmaster:Hificat