You are on page 1of 3

Mircocontroller Interfacing Lab

Lab07: Serial Communication between PC & Microcontroller

Lab 07: Serial Communicationbetween PC & Microcontroller


Objective: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 Mutliprocessor 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

SM0

SM1

Mode

Explanation

Baud Rate
Page |1

Faculty of Engineering & Technology, International Islamic University Islamabad

Mircocontroller Interfacing Lab

Lab07: Serial Communication between PC & Microcontroller

0 0 1 1

0 1 0 1

0 1 2 3

8-bit Shift Register 8-bit UART 9-bit UART 9-bit UART

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:

Steps to receive a character throughUART 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.

Faculty of Engineering & Technology, International Islamic University Islamabad

Page |2

Mircocontroller Interfacing Lab


C1
33pF

Lab07: Serial Communication between PC & Microcontroller

U1 X1
19 XTAL1 11.0592MHz 18 XTAL2 P0.0/AD0 P0.1/AD1 P0.2/AD2 P0.3/AD3 P0.4/AD4 P0.5/AD5 P0.6/AD6 P0.7/AD7 P2.0/A8 P2.1/A9 P2.2/A10 P2.3/A11 P2.4/A12 P2.5/A13 P2.6/A14 P2.7/A15 P3.0/RXD P3.1/TXD P3.2/INT0 P3.3/INT1 P3.4/T0 P3.5/T1 P3.6/WR P3.7/RD 39 38 37 36 35 34 33 32 21 22 23 24 25 26 27 28 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 RED

C5
10uF 1 3 C1T1OUT R1IN T2OUT R2IN VS+ VSC2+ 4 C2MAX232 5 14 13 7 8 2 6

C2
33pF

U2

C6
10uF

C7
10uF

R3
10k

RST

U3
20 19 18 17 16 15 14 13 12 11 11 12 10 9

C1+ T1IN R1OUT T2IN R2OUT

C3
10uF VCC 29 30 31 PSEN ALE EA

AT89C51

MAX232

U4
20 19 18 17 16 15 14 13 12 11 RED 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 AT89C51

J1
1 6 2 7 3 8 4 9 5 CONN-D9F

C4
10uF RXD TXD RTS CTS

Virtual Terminal for Simulation only

Figure 1 Circuit Diagram


/*=========================================================================*/ /*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 and Port 1. */ // XTAL frequency 11.0592MHz // Baud rate = 9600 #include<at89x51.h> unsigned char Recv; voidISR_serial(void) interrupt 4 { if(RI==1) // If a character is received by serial port { Recv = SBUF; // move received character from serial buffer P2 = Recv; // Send received character to Port2 SBUF = Recv+1; // Increment received character and Transmit on serial port P1 = Recv+1; // Display incremented character on Port 1 RI = 0; // Clear Receive flag TI = 0; // Clear Transmit flag } } void main() { TMOD = 0x20; TH1 = 0xFD; TL1 = 0xFD; SCON = 0x50; ES = 1; EA = 1; TR1 = 1; TI = 0; RI = 0; P1 = 0; P2 = 0; while(1); }

// // // // //

// 8 Bit auto-reload mode on Timer 1 // Initialize TH1 for 9600 Baud rate // Initialize TL1 for 9600 Baud rate // Mode 1, 8-bit, through Timer 1, Enable Reception // Enable serial Interrupt // Enable Global Interrupt Start Time 1 Clear Transmit Flag Clear Receive Flag Initialized port 1 with zero Initialized port 2 with zero // Forever loop

Faculty of Engineering & Technology, International Islamic University Islamabad

Page |3

You might also like