e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Serial Port Programming in Assembly Module No: CS/ES/12 Quadrant 1 e-text

Similar documents
CoE3DJ4 Digital Systems Design. Chapter 5: Serial Port Operation

MCS-51 Serial Port A T 8 9 C 5 2 1

Interrupt Programming: Interrupts vs. Polling Method:

8051 Serial Communication

ELEG3923 Microprocessor Ch.10 Serial Port Programming

8051 Timers and Serial Port

8051SERIAL PORT PROGRAMMING

8051 Serial Port. EE4380 Fall02 Class 10. Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

Mod-3: Interrupts,Timer operation,serial communication 1

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

Microcontroller and Embedded Systems:

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text

1. Pin diagram of 8051 and ports

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

Timer-1 can be run using the internal clock, fosc/12 (timer mode) or from any external source via pin T1 (P3.5) (Counter mode).

Serial communication

Experiment# 8: Photo-Interrupter Control

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Serial Port Communication Module No: CS/ES/11 Quadrant 1 e-text

UNIT IV MICROCONTROLLER

WINTER 14 EXAMINATION

EE6502- MICROPROCESSOR AND MICROCONTROLLER

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: 8051 Architecture Module No: CS/ES/5 Quadrant 1 e-text

INTERRUPTS PROGRAMMING

Introduction To MCS-51

Serial I-O for Dinesh K. Sharma Electrical Engineering Department I.I.T. Bombay Mumbai (version 14/10/07)

1. LCD (Liquid Crystal Display)interface

CoE3DJ4 Digital Systems Design. Chapter 6: Interrupts

Microcontroller and Applications

ELEG3923 Microprocessor Ch.9 Timer Programming

Vidyalankar T.E. Sem. V [ETRX] Microprocessors and Microcontrollers I Prelim Question Paper Solution

Embedded Controller Programming

The 8051 microcontroller has two 16-bit timers/counters called T0 and T1.

Chapter 6 Interrupts. (I. Scott Mackenzie) By: Masud-ul-Hasan

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Summer 2016 EXAMINATIONS.

SRL0 Serial Port Unit

8051 Timers. Class 7 EE4380 Fall Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

CHAPTER TIMER PROGRAMMING

How to use the PSoC based 16C450 Replacement

Lecture 10. Serial Communication


EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 4 The 8051 Architecture

Rev. No. History Issue Date Remark

ISSI. IS89C51 CMOS SINGLE CHIP 8-BIT MICROCONTROLLER with 4-Kbytes of FLASH ISSI IS89C51 NOVEMBER 1998 FEATURES GENERAL DESCRIPTION

CPEG300 Embedded System Design. Lecture 8 Timer

Chapter C2051 Architecture and Serial Communication Link

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

The Timers/Counters The Serial Interface The Interrupt System Reset P0.0-P0.7 P2.0-P2.7. Port 2 Drivers. Port 2 Latch

MODEL ANSWER SUMMER 17 EXAMINATION Subject Title: Microcontroller Subject Code:

Q.1. A) Attempt any THREE of the following:

8051 Microcontroller

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

8051 MICROCONTROLLER

SUMMER 13 EXAMINATION

CPEG300 Embedded System Design. Lecture 6 Interrupt System

2. Write an 8051 program to generate a square wave of 25 khz at pin P2.3 using XTAL = 12 MHz. Solution:

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI UNIT- IV

CHAPTER 11 INTERRUPTS PROGRAMMING

APPLICATION NOTE 600 Ultra High Speed Microcontroller Hardware Enhances Serial Port Capability

Chapter 09. Programming in Assembly


User-defined Download Application Note

CS 320. Computer Architecture Core Architecture

Department of Electronics and Instrumentation Engineering Question Bank

8051 Single Board Monitor Programming. Minmon - Yeralan & Ahluwalia. PaulMon1 & PaulMon2 - Paul Stoffregen

UNIT THE 8051 INSTRUCTION SET AND PROGRAMMING

8XC51RA RB RC Hardware Description

The Final Word on 8051 Microcontroller

MODEL ANSWER SUBJECT- MICROCONTROLLER(12187) CLASS-EJ5E CLASS TEST-02 Q1.)Attempt any THREE of the following.

8051 Microcontroller

Q 1 a) Attempt any THREE of the following: 12 TMOD.7 TMOD.6 TMOD.5 TMOD.4 TMOD.3 TMOD.2 TMOD.1 TMOD.0 GATE C/T M1 M0 GATE C/T M1 M0

Unit-I. 1. INTRODUCTION TO MICROCONTROLLERS. Micro controller, types, selection of a microcontroller and applications

8051 Microcontrollers

MICROCONTROLLER BASED WATER LEVEL CONTROL SYSTEM

VRS540-4kB Flash, 128B RAM, 25~40MHz, 8-Bit MCU

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interfacing External Devices using Embedded C Module No: CS/ES/22

Principle and Interface Techniques of Microcontroller

~: Simple Programs in 8051 assembly language :~

VRS550-8kB Flash, 256B RAM, 25~40MHz, 8-Bit MCU VRS560-16kB Flash, 256B RAM, 40MHz, 8-Bit MCU

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

Interrupts. EE4380 Fall 2001 Class 9. Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

MCS -51 Programmer s Guide and Instruction Set

UNIT MICROCONTROLLER AND ITS PROGRAMMING

Maxim > Design Support > Technical Documents > Application Notes > Microcontrollers > APP 4465

Hardware Setups for Communication with a DS1267

8051 Microcontroller memory Organization and its Applications


MODEL ANSWER SUMMER 17 EXAMINATION Subject Title: Microcontroller and Applications Subject Code:

THE 8051 MICROCONTROLLER Simple comparison: Pentium vs. 8051

INTEGRATED CIRCUITS DATA SHEET. P89C738; P89C739 8-bit microcontrollers Dec 15. Product specification File under Integrated Circuits, IC20

FACULTY OF ENGINEERING LAB SHEET

Module I. Microcontroller can be classified on the basis of their bits processed like 8bit MC, 16bit MC.

Migrating from the 8XC251Sx to the 8XC251Tx

EC Microprocessor and Microcontroller

Microcontrollers. Fig. 1 gives a comparison of a microprocessor system and a microcontroller system.

Fig 1. Block diagram of a microcomputer

Chapter Addressing Modes

MICROPROCESSOR LABORATORY MANUAL

80C51 family programmer s guide and instruction set. 80C51 Family. PROGRAMMER S GUIDE AND INSTRUCTION SET Memory Organization. Philips Semiconductors

W77E058A Data Sheet 8-BIT MICROCONTROLLER. Table of Contents-

Three criteria in Choosing a Microcontroller

Transcription:

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Serial Port Programming in Assembly Module No: CS/ES/12 Quadrant 1 e-text In this lecture, serial communication control register will be discussed in detail. Configuration of different Baud rates in 8051 and assembly program for serial port communication will be discussed. In this section, the serial communication registers of the 8051 is discussed and also, how to program them to transfer and receive data serially is shown. 1. Serial port programming in assembly Since IBM PC/compatible computers are so widely used to communicate with 8051-based systems, serial communications of the 8051 with the COM port of the PC will be emphasized. To allow data transfer between the PC and an 8051 system without any error, we must make sure that the baud rate of the 8051 system matches the baud rate of the PC s COM port. 1.1 Baud rate in the 8051 The 8051 transfers and receives data serially at many different baud rates. Serial communications of the 8051 is established with PC through the COM port. It must make sure that the baud rate of the 8051 system matches the baud rate of the PC's COM port/ any system to be interfaced. The baud rate in the 8051 is programmable. This is done with the help of Timer. When used for serial port, the frequency of timer tick is determined by (XTAL/12)/32 and 1 bit is transmitted for each timer period (the time duration from timer start to timer expiry). The Relationship between the crystal frequency and the baud rate in the 8051 is that the 8051 divides the crystal frequency by 12 to get the machine cycle frequency which is shown in figure1. Here the oscillator is XTAL = 11.0592 MHz, the machine cycle frequency is 921.6 khz. 8051's UART divides the machine cycle frequency of 921.6 khz by 32 once more before it is used by Timer 1 to set the baud rate. 921.6 khz divided by 32 gives 28,800 Hz. Timer 1 must be programmed in mode 2, that is 8-bit, auto-reload.

Figure 1.Oscillator Frequency Baud rate supported by Pentium / IBM 486 PC are 1.1.1 Calculation of baud rate: In serial communication if data transferred with a baud rate of 9600 and XTAL used is 11.0592 then following is the steps followed to find the TH1 value to be loaded. Clock frequency of timer clock: f = (11.0592 MHz / 12)/32 = 28,800Hz Time period of each clock tick: T0 = 1/f = 1/28800 Duration of timer : n*t0 (n is the number of clock ticks) 9600 baud ->duration of 1 symbol: 1/9600 1/9600 = n*t0 = n*1/28800 n = f/9600 = 28800/9600 = 3 ->TH1 =-3 Similarly, for baud 2400 n = f/2400 = 12 ->TH1 = -12 Example: set baud rate at 9600 MOV TMOD, #20H ; timer 1,mode 2(auto reload) MOV TH1, #-3 ; To set 9600 baud rate SETB TR1 ; start timer 1

1.1.2 Baud rate selection Baud rate is selected by timer1 and when Timer 1 is used to set the baud rate it must be programmed in mode 2 that is 8-bit, auto-reload. To get baud rates compatible with the PC, we must load TH1 with the values shown in Table 1. Table.1 Timer 1 THI register values for different baud rates 1.2 Registers for serial communication 1.2.1 SBUF (serial buffer) register: It is an 8 bit register used solely for serial communication in the 8051.A byte of data to be transferred via the TxD line must be placed in the SBUF register. SBUF holds the byte of data when it is received by the RxD line. It can be accessed like any other register MOV SBUF, #'D' MOV SBUF, A MOV A, SBUF ; load SBUF=44H, ASCII for 'D ; copy accumulator into SBUF ; copy SBUF into accumulator when a byte is written, it is framed with the start and stop bits and transferred serially via the TxD pin. when the bits are received serially via RxD, it is deframed by eliminating the stop and start bits, making a byte out of the data received, and then placing it in the SBUF. 1.2.2 SCON (serial control) register: It is an 8 bit register used to program start bit, stop bit, and data bits of data framing, among other things. Figure 2. SCON register The first two bits are SM0, SM1 which is the serial port mode bits. It is used to specify framing format, how to calculate baud. For example if (SM0, SM1) = (0,1), mode 1: 8-bit data, 1 start bit, 1 stop bit, variable baud rate can be set by timer. The other three modes are rarely used and they are (SM0,SM1) = (0,0), mode 0: fixed baud = XTAL/12,(SM0, SM1) = (1,0), mode 2: 9-bit data, fixed baud,(sm0, SM1) = (1, 1), mode 3: 9-bit data, variable baud. The third bit is used to select the type of processor used for communication. If SM2 is 0 means it is single processor communication. If SM2 is 1, then it is multiprocessor communication. The fourth bit REN is Receive Enable which is used to enable/disable reception. If REN=1,then 8051 will accept incoming data from serial port. If REN=0, then the receiver is disabled. E.g. SETB REN,CLR REN, SETB SCON.4, CLR SCON.4.

The fifth bit is TB8 which is used by modes 2 and 3 for the 8-bit transmission. When mode 1 is used the pin TB8 should be cleared. The sixth bit RB8 is used by modes 2 and 3 for the reception of bit 8. It is used by mode1 to store the stop bit. The seventh bit is TI which is the Transmit Interrupt. When 8051 finishes the transfer of the 8-bit character, it sets TI to ''1'' to indicate that it is ready to transfer the next character. The TI is raised at the beginning of the stop bit. The last bit is the RI which is the receive interrupt. When 8051 receives a character,the UART removes start bit and stop bit. The UART puts the 8-bit character in SBUF. RI is set to 1 to indicate that a new byte is ready to be picked up in SBUF.RI is raised halfway through the stop bit 1.3 Steps to send data serially: 1.Set baud rate by loading TMOD register with the value 20H, this indicating timer 1 in mode 2 (8-bit auto-reload) to set baud rate 2. The TH1 is loaded with proper values to set baud rate for serial data transfer 3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is framed with start and stop bits 4. TR1 is set to 1 to start timer 1 5. TI is cleared by CLR TI instruction 6. The character byte to be transferred serially is written into SBUF register 7. The TI flag bit is monitored with the use of instruction JNB TI,xx to see if the character has been transferred completely 8. To transfer the next byte, go to step 5 1.3.1 Program to transfer letter D serially at 9800baud, continuously: MOV TMOD,#20H MOV TH1, #-3 MOV SCON, #50H SETB TR1 ; start timer 1 AGAIN: MOV SBUF, # D HERE: JNB TI, HERE CLR TI SJMP AGAIN ; timer 1,mode 2(auto reload) ; 9600 baud rate ; 8-bit, 1 stop, REN enabled ; letter D to transfer ; wait for the last bit ;clear TI for next char ; keep sending A 1.3.2 Importance of the TI flag: Check the TI flag bit, we know whether or not 8051 is ready to transfer another byte. TI flag bit is raised by the 8051 after transfer of data. TI flag is cleared by the programmer by instruction like CLR TI. When writing a byte into SBUF, before the TI flag bit is raised, it may lead to loss of a portion of the byte being transferred. 1.4 Steps to receive data serially: 1. Set baud rate by loading TMOD register with the value 20H, this indicating timer 1 in mode 2 (8-bit auto-reload) to set baud rate

2. The TH1 is loaded with proper values to set baud rate 3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is framed with start and stop bits 4. TR1 is set to 1 to start timer 1 5. RI is cleared by CLR RI instruction 6. The RI flag bit is monitored with the use of instruction JNB RI,xx to see if an entire character has been received yet 7. When RI is raised, SBUF has the byte; its contents are moved into a safe place 8. To receive next character, go to step 5 1.4.1 Program to receive bytes of data serially, and put them in P2, set the baud rate at 9600, 8-bit data, and 1 stop bit: MOV TMOD, #20H MOV TH1, #-3 MOV SCON, #50H ; timer 1,mode 2(auto reload) ; 9600 baud rate SETB TR1 ; start timer 1 HERE: JNB RI, HERE MOV A, SBUF ; 8-bit, 1 stop, REN enabled ; wait for char to come in ; saving incoming byte in A MOV P2, A ; send to port 1 CLR RI SJMP HERE ; get ready to receive next byte ; keep getting data 1.4.2 Importance of the RI flag bit: It receives the start bit, next bit is the first bit of the character about to be received. When the last bit is received, a byte is formed and placed in SBUF. when stop bit is received, it makes RI = 1 indicating entire character byte has been received and can be read before overwritten by next data. When RI=1, received byte is in the SBUF register, copy SBUF contents to a safe place. After the SBUF contents are copied the RI flag bit must be cleared to 0. 1.5 Increasing the baud rate: Baud rate can be increase by two ways- 1. Increasing frequency of crystal 2. Change bit in PCON register

1.5.1 PCON It is 8-bit register.when 8051 is powered up, SMOD is zero.by setting the SMOD, baud rate can be doubled.if SMOD = 0 (which is its value on reset), the baud rate is 1/64 the oscillator frequency.if SMOD = 1, the baud rate is 1/32 the oscillator frequency. Figure 3. PCON register 1.5.2 Baud rate comparison: Table 2. Comparison of Baud rate Figure 4. XTAL Oscillator 1.6 Program for Sending and receiving data serially: Assume that the 8051 serial port is connected to the COM port of IBM PC, and on the PC, we are using the terminal.exe program to send and receive data serially. P1 and P2 of the 8051 are connected to LEDs and switches, respectively. Write an 8051 program to (a) send to PC the message We Are Ready, (b) receive any data send by PC and put it on LEDs connected to P1,

and (c) get data on switches connected to P2 and send it to PC serially. The program should perform part (a) once, but parts (b) and (c) continuously, use 4800 baud rate. Figure 5. Serial Communication of 8051 ORG 0 MOV P2, #0FFH ; make P2 an input port MOV TMOD,#20H ;timer 1, mode 2 MOV TH1, #0FAH ;4800 baud rate MOV SCON, #50H ; 8-bit, 1 stop, REN enabled SETB TR1 ; start timer 1 MOV DPTR, #MYDATA ; load pointer for message H_1: CLR A MOV A,@A+DPTR ;get the character JZ B_1 ;if last character get out ACALL SEND ;otherwise call transfer INC DPTR ;next one SJMP H_1 ;stay in loop B_1: MOV a,p2 ;read data on P2 ACALL SEND ;transfer it serially ACALL RECV ;get the serial data MOV P1,A ;display it on LEDs SJMP B_1 ;stay in loop indefinitely ; ---------serial data transfer. ACC has the data------------- SEND: MOV SBUF,A ;load the data H_2: JNB TI,H_2 ;stay here until last bit gone CLR TI ; get ready for next char RET ;return to caller

; ----------Receive data serially in ACC---------------- RECV: JNB RI, RECV ; wait here for char MOV A, SBUF ; save it in ACC CLR RI ; get ready for next char RET ; return to caller ; ------------The message--------------- MYDATA: DB We Are Ready, 0 END 1.7 Serial port programming enhancement Normal serial data transfer is wasting of the microcontroller's time to poll the TI and RI flags. In order to reduce time, use interrupts instead of polling. New generations of 8051 microcontroller come with two serial ports. It is possible to program second serial port also. 2. Summary In this lecture configuration of essential register used for serial port programming and the method to configure different baud rate in serial port communication is discussed. Writing assembly codes for 8051 serial port programming is discussed. 3. References 1. The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D.McKinlay.