serial communication using avr lab 8.pdf

5
Microcontroller Interfacing Lab Lab 08: Serial Communication between PC & Microcontroller DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad Page 27 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 # Signal Description 1 CD Carrier Detect 2 RxD Receive Data 3 TxD Transmit Data 4 DTR Data Terminal Ready 5 SG Signal Ground 6 DSR Data Set Ready 7 RTS Request to Send 8 CTS Clear to Send 9 RI Ring Indicator DB9 Male (Front View) DB9 Female (Front View) Serial Control (SCON) SFR (Bit Addressable) Bit# Name Description 7 SM0 Serial port mode bit 0 6 SM1 Serial port mode bit 1 5 SM2 Multiprocessor Communications Enable (explained later) 4 REN Receiver Enable. This bit must be set in order to receive characters 3 TB8 Transmit bit 8. The 9th bit to transmit in mode 2 and 3 2 RB8 Receive bit 8. The 9th bit received in mode 2 and 3 1 TI Transmit Flag. Set when a byte has been completely transmitted 0 RI Receive Flag. Set when a byte has been completely received

Upload: lovelyosmile253

Post on 08-Feb-2016

61 views

Category:

Documents


2 download

DESCRIPTION

AVR lab for serial communication,

TRANSCRIPT

Page 1: Serial Communication using AVR lab 8.pdf

Microcontroller Interfacing Lab Lab 08: Serial Communication between PC & Microcontroller

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

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 # Signal Description 1 CD Carrier Detect 2 RxD Receive Data 3 TxD Transmit Data 4 DTR Data Terminal Ready 5 SG Signal Ground 6 DSR Data Set Ready 7 RTS Request to Send 8 CTS Clear to Send 9 RI Ring Indicator

DB9 Male (Front View)

DB9 Female (Front View)

Serial Control (SCON) SFR (Bit Addressable)

Bit# Name Description

7 SM0 Serial port mode bit 0

6 SM1 Serial port mode bit 1

5 SM2 Multiprocessor Communications Enable (explained later)

4 REN Receiver Enable. This bit must be set in order to receive characters

3 TB8 Transmit bit 8. The 9th bit to transmit in mode 2 and 3

2 RB8 Receive bit 8. The 9th bit received in mode 2 and 3

1 TI Transmit Flag. Set when a byte has been completely transmitted

0 RI Receive Flag. Set when a byte has been completely received

Page 2: Serial Communication using AVR lab 8.pdf

Microcontroller Interfacing Lab Lab 08: Serial Communication between PC & Microcontroller

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

Mode SM0 SM1 Explanation Baud Rate 0 0 0 8-bit Shift Register Oscillator / 12 1 0 1 8-bit UART Set by Timer 1 (*) 2 1 0 9-bit UART Oscillator / 64 (*) 3 1 1 9-bit UART 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 Crystal Freq TH1 decimal TH1 Hex 9600 11.0592 MHz -3 FD 4800 11.0592 MHz -6 FA 2400 11.0592 MHz -12 F4 1200 11.0592 MHz -24 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

Page 3: Serial Communication using AVR lab 8.pdf

Microcontroller Interfacing Lab Lab 08: Serial Communication between PC & Microcontroller

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

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

Page 4: Serial Communication using AVR lab 8.pdf

Microcontroller Interfacing Lab Lab 08: Serial Communication between PC & Microcontroller

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

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

Page 5: Serial Communication using AVR lab 8.pdf

Microcontroller Interfacing Lab Lab 08: Serial Communication between PC & Microcontroller

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

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 and Port 1.  =========================================================================================*/  // XTAL frequency 11.0592MHz  // Baud rate = 9600  #include<at89x51.h> unsigned char Recv;  void ISR_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(void) {   TMOD = 0x20;    // 8 Bit auto‐reload mode on Timer 1   TH1  = 0xFD;    // Initialize TH1 for 9600 Baud rate   TL1  = 0xFD;    //  Initialize TL1  for 9600 Baud rate   SCON = 0x50;    // Mode 1, 8‐bit, through Timer 1, Enable Reception    ES   = 1;      // Enable serial Interrupt   EA   = 1;      // Enable Global Interrupt   TR1  = 1;      // Start Time 1   TI   = 0;      // Clear Transmit Flag       RI   = 0;      // Clear Receive Flag   P1   = 0;      // Initialized port 1    P2   = 0;      // Initialized port 2     while(1);      // Forever loop }