Vous êtes sur la page 1sur 5

Microcontroller Interfacing Lab

Lab 08: Serial Communication between PC & Microcontroller

Lab 08: Serial Communication between PC & Microcontroller


Objective:
1- To understand the USART of 8051 and its interfacing with PC 2- Send a character to Microcontroller through serial port and receive it back.

MAX 232 Level Converter IC


The serial port of computer sends/receives data serially at logic levels between -12 to +12V. Microcontroller works at logic levels between 0 to 5V. So we need a RS-232 to TTL and TTL to RS-232 converter and this is done by RS-232 Level Converter called MAX-232. Pin # 1 2 3 4 5 6 7 8 9 Signal CD RxD TxD DTR SG DSR RTS CTS RI Description Carrier Detect Receive Data Transmit Data Data Terminal Ready Signal Ground Data Set Ready Request to Send Clear to Send Ring Indicator

DB9 Male (Front View)

DB9 Female (Front View) Serial Control (SCON) SFR (Bit Addressable)
Bit# 7 6 5 4 3 2 1 0 Name SM0 SM1 SM2 REN TB8 RB8 TI RI Description Serial port mode bit 0 Serial port mode bit 1 Multiprocessor Communications Enable (explained later) Receiver Enable. This bit must be set in order to receive characters Transmit bit 8. The 9th bit to transmit in mode 2 and 3 Receive bit 8. The 9th bit received in mode 2 and 3 Transmit Flag. Set when a byte has been completely transmitted Receive Flag. Set when a byte has been completely received Page 27

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Microcontroller Interfacing Lab Mode 0 1 2 3 SM0 0 0 1 1 SM1 0 1 0 1

Lab 08: Serial Communication between PC & Microcontroller Explanation 8-bit Shift Register 8-bit UART 9-bit UART 9-bit UART Baud Rate Oscillator / 12 Set by Timer 1 (*) Oscillator / 64 (*) Set by Timer 1 (*)

Following table shows some standard baud rates used for serial communication. Right most column shows the value of higher byte of timer 1, to generate the respective baud rate. Baud Rate 9600 4800 2400 1200 Crystal Freq 11.0592 MHz 11.0592 MHz 11.0592 MHz 11.0592 MHz TH1 decimal -3 -6 -12 -24 TH1 Hex FD FA F4 E8

To calculate the value of TH1 for a required baud rate, following formula can also be used:

We can double the Baud Rate by setting high the SMOD (LSB, D7) bit of PCON register. When 8051 is powered up, SMOD bit is zero so Baud Rate is generated on normal speed. Steps to receive a character through UART of 8051: 1. Configure SCON SFR to Mode 1 and enable the reception through REN bit. 2. Configure Timer 1 to 8 bit auto-reload mode by assigning appropriate value to TMOD SFR 3. Load TH1 and TL1 with a value given in theTH1 Hex column of above table for a required baud rate generation. 4. Set ES and EA bits in IE SFR to enable serial port and global interrupts 5. Start the Timer 1 by setting TR1 bit of TCON register. 6. Reset the RI bit of SCON SFR 7. Check for RI flag. 8. When RI goes up, copy the received byte from SBUF register and move it to a safe location. 9. Now reset the RI flag to enable reception of another byte.

Steps to transmit a character from UART of 8051: 1. There is no need to configure the serial port again from step 1 to 5. These steps are required to be configured only one time. 2. Reset TI flag of SCON SFR 3. Put the character in SBUF for start of transmission. When a character is copied into SBUF, transmission is started immediately. 4. Wait until TI flag is raised, which indicates that complete byte has been transmitted. 5. Now reset the TI flag and repeat the step 3 to transmit another byte. Steps to configure the PC for serial communication using RS232: Latest PCs / Laptops does not contain the serial ports. For serial communication on these PCs, we can use the USB to Serial converter which are easily available in the market. All you need to do is

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Page 28

Microcontroller Interfacing Lab

Lab 08: Serial Communication between PC & Microcontroller

to attach the USB to serial converter into the serial port of your PC and the other end of converter cable can be used as serial interface. Generally, Hyper Terminal is the software used for serial communication which is not available in the Windows 7 and Windows 8. You can download the free Hyper Terminal from: http://files.digitizor.com/wp-content/uploads/2009/08/hyperterminal1.zip Make sure you keep the files hypertrm.dll and hypertrm.exe in the same folder. Now you can launch the HyperTerminal client by double clicking the hypertrm.exe. After running this file, Location Information window will appear. You can cancel this windows and proceed. Next, Connection Description windows will appear:

Enter new connection name and press OK. Now, Connect To window will appear. Here, you should select COM1 (or your available COM Port number) in Connect Using field and press OK

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Page 29

Microcontroller Interfacing Lab

Lab 08: Serial Communication between PC & Microcontroller

In the following window, select the baud rate (Bits per second), Data bits, Parity, Stop Bit and Flow control as per the settings done while programming the USART of 8051. Normally, we keep the flow control None. After pressing OK, your connection would be established. Now you can transmit or receive from PC to microcontroller or from microcontroller to PC.

At this stage when you will type a letter on the hyper terminal, it will not be shown. You can see your typed character by navigation to the following setting: File -> Properties -> Setting -> ASCII Setup -> and there you can enable the option of Echo typed characters locally by checking the check box.

Figure 1 Circuit Diagram

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Page 30

Microcontroller Interfacing Lab

Lab 08: Serial Communication between PC & Microcontroller

C Code for serial communication using USART of 8051:


/*========================================================================================= This program receives a character from serial port, displays the ASCII of received character to Port 2. Increments the same character and transmits the result to serial port andPort1. =========================================================================================*/ //XTALfrequency11.0592MHz //Baudrate=9600 #include<at89x51.h> unsignedcharRecv; voidISR_serial(void)interrupt4 { if(RI==1) //Ifacharacterisreceivedbyserialport { Recv=SBUF; //movereceivedcharacterfromserialbuffer P2=Recv; //SendreceivedcharactertoPort2 SBUF=Recv+1; //IncrementreceivedcharacterandTransmitonserialport P1=Recv+1; //DisplayincrementedcharacteronPort1 RI=0; //ClearReceiveflag TI=0; //ClearTransmitflag } } voidmain(void) { TMOD=0x20; //8BitautoreloadmodeonTimer1 TH1=0xFD; //InitializeTH1 for9600Baudrate TL1=0xFD; // InitializeTL1 for9600Baudrate SCON=0x50; //Mode1,8bit,throughTimer1,EnableReception ES=1; //EnableserialInterrupt EA=1; //EnableGlobalInterrupt TR1=1; //StartTime1 TI=0; //ClearTransmitFlag RI=0; //ClearReceiveFlag P1=0; //Initializedport1 P2=0; //Initializedport2 while(1); //Foreverloop }

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Page 31

Vous aimerez peut-être aussi