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

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

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

8051 Timers and Serial Port

8051 Serial Communication

Interrupt Programming: Interrupts vs. Polling Method:

ELEG3923 Microprocessor Ch.10 Serial Port Programming

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

Serial communication

8051SERIAL PORT PROGRAMMING

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

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

CoE3DJ4 Digital Systems Design. Chapter 6: Interrupts

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

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

SRL0 Serial Port Unit

Serial Communication Prof. James L. Frankel Harvard University. Version of 2:30 PM 6-Oct-2015 Copyright 2015 James L. Frankel. All rights reserved.


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).

EE6502- MICROPROCESSOR AND MICROCONTROLLER

Department of EIE / Pondicherry Engineering College. Timer/Counters. Department of EIE / Pondicherry Engineering College 1

Experiment# 8: Photo-Interrupter Control

Chapter C2051 Architecture and Serial Communication Link

CPEG300 Embedded System Design. Lecture 6 Interrupt System

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

Embedded Controller Programming

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

Interfacing a Hyper Terminal to the Flight 86 Kit

How to use the PSoC based 16C450 Replacement

Lecture 10. Serial Communication

UNIT IV MICROCONTROLLER

CS 320. Computer Architecture Core Architecture

CPEG300 Embedded System Design. Lecture 8 Timer

Rev. No. History Issue Date Remark

Sender Receiver Sender

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

CHAPTER 11 INTERRUPTS PROGRAMMING

8051 Microcontroller

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

Chapter 09. Programming in Assembly

The Final Word on 8051 Microcontroller

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

Migrating from the 8XC251Sx to the 8XC251Tx

WINTER 14 EXAMINATION

CHAPTER TIMER PROGRAMMING

8051 MICROCONTROLLER

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

8051 Microcontroller memory Organization and its Applications

Understanding the basic building blocks of a microcontroller device in general. Knows the terminologies like embedded and external memory devices,

INTERRUPTS PROGRAMMING

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

Microcontroller and Embedded Systems:

EET203 MICROCONTROLLER SYSTEMS DESIGN Serial Port Interfacing

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

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

8-bit Microcontroller with 8K Bytes In-System Programmable Flash AT89S52

Introduction To MCS-51

Microcontroller and Applications

Department of Electronics and Instrumentation Engineering Question Bank

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

THE 8051 MICROCONTROLLER Simple comparison: Pentium vs. 8051

8051 Microcontrollers

ELEG3923 Microprocessor Ch.9 Timer Programming

Hello, and welcome to this presentation of the STM32 Universal Synchronous/Asynchronous Receiver/Transmitter Interface. It covers the main features

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

8XC51RA RB RC Hardware Description

UNIT 5. Microcontrollers. Syllabus

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

8051 I/O and 8051 Interrupts

A. This Errata sheet represents the known bugs, anomalies and work-arounds for the ADuC812 MicroConverter.

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI UNIT- IV

Hello, and welcome to this presentation of the STM32 Low Power Universal Asynchronous Receiver/Transmitter interface. It covers the main features of

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

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

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

EC Microprocessor and Microcontroller

Fig 1. Block diagram of a microcomputer

DATA SHEET. P80CL31; P80CL51 Low voltage 8-bit microcontrollers with UART INTEGRATED CIRCUITS Apr 15

Timers and interrupts

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

Serial Interfacing. Asynchronous Frame

8051 Microcontroller

The 8000 Series Swiss Army Knife Manual


UNIT MICROCONTROLLER AND ITS PROGRAMMING

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

MODULE-1. Short Answer Questions

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

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

Asynchronous Transmission. Asynchronous Serial Communications & UARTS

8051 Microcontroller Interrupts

Embedded Systems and Software. Serial Communication

Embedded Systems and Software


SUMMER 13 EXAMINATION

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

8051 MICROCONTROLLER

Serial Communications

80C451 operation of port 6

Hierarchy of I/O Control Devices

Addressing scheme to address a specific devices on a multi device bus Enable unaddressed devices to automatically ignore all frames

Transcription:

MCS-51 Serial Port AT89C52 1

Introduction to Serial Communications Serial vs. Parallel transfer of data Simplex, Duplex and half-duplex modes Synchronous, Asynchronous UART Universal Asynchronous Receiver/Transmitter. Handles all issues related to the asynchronous transmission of byte sized data. USART Universal Synchronous/Asynchronous Receiver/Transmitter. Data transfer rate Bps, baud. 2

An 8-bit message needs to be framed so that the receiver can detect correctly its beginning and end. Standard framing: Start bit always 0, Stop bit always 1. Optional parity bit Stop bit can be one or two bits The message now becomes: Start bit (1 0), LSB,, MSB, <parity bit>, Stop bit (0 1), <2 nd stop bit (1)> Start 0 1 2 3 4 5 6 7 <P> Stop <St> Time 3

Serial communication standard for small computing systems. Original intent was for communication between small computer. Mostly used for communication with slow peripherals. The cable connecting the PC to the Kit in the lab follows this standard. Defines many signals about 25 however, only three are used in most cases. RxD Received Data TxD Transmitted Data GND Common Ground 4

RS-232 requires non-ttl compatible voltage levels -3 to 25 for 1 and +3 to +25 for 0 Therefore, we need an interface to connect to standard TTL chips. MAX 232 and MAX 233 chips. Accept standard TTL logic levels and produce RS-232 levels. Utilize a normal +5 V supply. 5

MCS-51 has a full-duplex serial port that can be used as a normal serial interface (non-framed) or as an internal UART (framed). This serial port controls the RxD and TxD dual functions for pins P3.0 and P3.1. The MCS-51 serial port is controlled using the SCON SFR (98H). The MCS-51 serial port communicates with the rest of the chip using the SBUF SFR (99H). 6

SBUF is actually two separate registers at the same address. Write-only transmit register. Read-only receive register. Cannot read back what was sent for transmission. The byte to be transmitted on the serial port is written into SBUF. Serial transmission starts immediately. The byte received from the serial port will be stored in SBUF once the last bit is received. This is called double buffering. Received data is buffered in the serial port itself until the full byte is received. This allows a little more time to deal with the previous data before its overwritten with the new one. HOWEVER, the previous data must be read before the new byte completes. Otherwise, the old data will be lost. 7

MSB LSB SM0 SM1 SM2 REN TB8 RB8 TI RI Bit Name Description SCON.7 SM0 Serial Port Mode bit 0 SCON.6 SM1 Serial Port Mode bit 1 SCON.5 SM2 Multiprocessor Communication Enable SCON.4 REN Receive Enable Set to enable reception. CLR to disable reception. SCON.3 TB8 Bit 8 of message to transmit Used to transmit optional parity bit SCON.2 RB8 Bit 8 of received message Receives optional parity bit SCON.1 TI Transmit Interrupt Flag Set when Byte in SBUF is completely transmitted. SCON.0 RI Receive Interrupt Flag Set when a valid byte is received into SBUF 8

Serial Port Mode 9

Mode 0 SM0 = SM1 = 0 Half Duplex Synchronous Operation. Data is sent and received (not simultaneously) using the RxD pin. The TxD pin carries the shift clock during both receiving and transmitting. (Reference clock for synchronization). Data is sent in 8-bit un-framed packets. LSB first. Data rate is set to 1/12 clock frequency. Machine Cycle Frequency. Same as the shift clock. 10

Transmission starts as soon as byte is written into SBUF. During transmission, each bit stays valid on the RxD pin for one complete machine cycle. The shift clock goes low in the middle of the cycle and returns high right before the end. The TI flag is set when the 8 th bit is done transmitting. Machine Cycle Clock Data out Shift Clock 11

Reception is initiated as soon as REN bit is set to 1 and the receive interrupt (RI) bit is cleared. Usually, REN is set at the beginning of the program to initialize the serial port, then RI is cleared to start a data input operation. As soon as RI is cleared, the shift clock will be produced on the TxD pin. At the beginning of the following machine cycle, data will be clocked in from the RxD line. The clocking occurs on the rising edge of the TxD line. After the 8 th clocking cycle, the data is copied to SBUF and the RI bit is set. 12

In mode 1, the 8051 serial port operates an 8-bit UART with variable baud rate. The essential operation of a UART is parallel-to-serial conversion of output data and serial-to-parallel conversion of input data. 10 bits are transmitted on TxD or received on RxD. Start bit, 8 data bits, 1 stop bit. The baud rate is set by the Timer 1 overflow rate. 13

Transmission starts when anything is written into SBUF. The period for each bit is the reciprocal of the baud rate. The transmit interrupt (TI) flag is set as soon as the stop bit appears on TxD. 14

Reception is initiated by a 1-to-0 transition on the RxD line (assuming REN is 1). A divide-by-16 counter is immediately started. The next bit is expected to arrive at the roll-over of this counter. Bits are sampled at the 8 th count of this counter. False Start Bit Detection 8 counts after the 1-to-0 transition, the RxD line is samples again. If it is not 0, then we have a false start bit. The receiver is reset and waits for the next 1-to-0 transition. 15

Assuming that a valid start bit was detected: The start bit is skipped. Eight data bits are clocked into the serial port s register (NOT SBUF). When all eight bits are received: The ninth bit (the stop bit) is clocked into RB8 SBUF is loaded with the right data bits The receiver interrupt flag (RI) is set. The above three steps only occur if RI was 0 to start with. Do not overwrite the previous data if it has not been read. 16

The serial port operates as a 9-bit UART with a fixed baud rate. 11 bits are transmitted: The start bit The 8 data bits from SBUF A 9 th data bit from TB8 The stop bit On reception, the 9 th data bit will be placed in RB8. The baud rate is fixed at either 1/32 or 1/64 of the oscillator frequency. 17

9-Bit UART with Variable Baud Rate. Combination of modes 1 and 2. 18

In Mode 0, the baud rate is fixed at the clock frequency divided by 12. By default, the baud rate in mode 2 is set to 1/64 of the clock frequency. However, bit 7 of the PCON (Power Control) Register known as SMOD doubles the baud rate if it is set to 1. So, if SMOD = 1, the baud rate for mode 2 is 1/32 of the clock frequency. 19

In modes 1 and 3, the baud rate is set by the overflow rate of Timer 1. However, that rate is too high. So, it is divided by 32 (or 16 if SMOD = 1) to generate the real baud rate. You can think about as if Timer 1 will be clocked at XTAL / (12 * 32) = 28,800Hz (when SMOD = 0) or XTAL / (12 * 16) = 57,600Hz (when SMOD = 1) 20

Setting Timer 1 to Generate Baud Rate How do we produce a baud rate of 1200 using an 8051 with a clock frequency of 12 MHz? Baud Rate = 28.8K / count Or 57.6K / count To produce 1200 baud, we need to set timer 1 to count for: 28.8K / 1200 = 24 counts. Set Timer 1 to operate in mode 2 (auto-reload) and set TH1 to 0E8H (-24). 21

1. Program T1 for Mode2 (TMOD 0x20) 2. Load TH1 with the initial value (baud rate dependent) (TH1 FD / FA / F4 / E8) 3. Program SCON for Mode1 (SCON 0x50) 4. Start Timer1 (SETB TR1) 5. Clear TI 6. Load SBUF with the byte to be transferred (SBUF byte) 7. Wait until TI becomes 1 (JNB TI, not_done) 8. Go back to Step5 for next byte 22

Transfer ASCII A serially at 9600 baud continuously START: MOV TMOD, #20H ;Put T1 in mode2 MOV TH1, #-3 ;9600 baud MOV SCON, #50H ;8b, 1stop, 1start, REN enabled SETB TR1 ;start timer T1 AGAIN: CLR TI ;ready to transmit MOV SBUF, # A ;letter A is to be transmitted HERE: JNB TI, HERE ;poll TI until all the bits are transmitted SJMP AGAIN ;while(1) loop (forever loop) 23

1. Program T1 for Mode2 (TMOD 0x20) 2. Load TH1 with the initial value (baud rate dependent) (TH1 FD / FA / F4 / E8) 3. Program SCON for Mode1 (SCON 0x50) 4. Start Timer1 (setb TR1) 5. Clear RI 6. Wait until RI becomes 1 (jnb RI, not_done) 7. Store SBUF (A SBUF) 8. Go back to Step5 for next byte 24

Receive bytes serially and display them on P1, continuously. START: MOV TMOD, #20H ;T1 in mode 2 MOV TH1, #-3 ;9600 baud MOV SCON, #50H ;8b, 1start, 1stop SETB TR1 ;start T1 AGAIN: CLR RI ;ready to receive a byte HERE: JNB RI, HERE ;wait until one byte is Rx-ed MOV A, SBUF ;read the received byte from SBUF MOV P1, A ;display on P1 SJMP AGAIN ;while (1) 25

Using serial port with interrupts is THE way it was intended to be used. Both the RI and TI flags raise the Serial interrupt (S0), if S0 is enabled in IE. ISR for S0 is at 0x0023 Simple Case Transmit is polling based (Poll TI flag) and Receive is interrupt driven Receive is interrupt driven and Receive is polling based (Poll RI flag) In these cases, the ISR of S0 will check for the appropriate flag and either copy data to or from SBUF 26

General Case 8051 is in full duplex mode, I.e. receives and transmits data continuously Both Transmit and Receive is interrupt driven Write the ISR for S0 such that ISR must first check which one of RI and TI raised the S0 interrupt If RI is set, then read data from SBUF to a safe place and clear RI If TI is set, then copy the next character to be transmitted onto SBUF and clear TI. 27

8051 gets data from P1 and sends it to P2 continuously while receiving from Serial port. Serial port data is to be displayed on P0 ORG 0 LJMP MAIN ORG 23H LJMP SERIAL ; avoid the IVT ; serial port ISR ORG 30H MAIN: MOV P1, #0FFH ; P1 as input port MOV TMOD, #20 ; T1 in mode 2 MOV TH1, #-3 ; 9600 baud MOV SCON, #50H ; 8b, 1start, 1stop MOV IE, #10010000B ; enable S0 interrupt SETB TR1 ; enable T1 BACK: MOV A, P1 MOV P2, A SJMP BACK ORG 100H SERIAL: JB TI, TRANS MOV A, SBUF ;copy received data MOV P0, A ;display it on P0 CLR RI ;clear RI RETI TRANS: CLR TI ;do nothing RETI ;ISR does not handle TX end 28