Introduction to Micro Controllers & Embedded System Design I/O Processing and Serial Port Operation Department of Electrical & Computer Engineering Missouri.

Slides:



Advertisements
Similar presentations
Autumn 2012C.-S. Shieh, EC, KUAS, Taiwan1 The 8051 Family Microcontroller Chin-Shiuh Shieh Department of Electronic Engineering.
Advertisements

The 8051 Microcontroller Chapter 5 SERIAL PORT OPERATION.
Chapter 2 HARDWARE SUMMARY
8051 Core Specification.
Embedded Systems UNIT 3. Pin Details of 8051 Pins 1-8: Port 1 Each of these pins can be configured as an input or an output. Pin 9: The positive voltage.
Lecture 10 Serial Communication.
Microcontroller 8051.
The 8051 Microcontroller and Embedded Systems
1 Chapter 4 Timer Operation (I. Scott MacKenzie).
Chapter 4 TIMER OPERATION
CoE3DJ4 Digital Systems Design Chapter 4: Timer operation.
CoE3DJ4 Digital Systems Design Hardware summary. Microprocessors vs. Microcontrollers Microprocessors are single-chip CPU used in microcomputers Microcontrollers.
Lecture Set 9 MCS-51 Serial Port.
Serial Communication Lec note 9.
1. Registers Used in Timer/Counter  TH0, TL0, TH1, TL1  TMOD (Timer mode register)  TCON (Timer control register) 2.
Example. SBUF Register SCON Register(1) SCON Register(2)
8051 Micro controller. Architecture of 8051 Features of 8051.
Intel 8051 Another family of microcontroller is the Intel 8051 family. The basic 8051 microcontroller has four parallel input/output ports, port 0, 1,
Timer Programming in Assembly and C Prepared By:
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
The HCS12 SCI Subsystem A HCS12 device may have one or two serial communication interface. These two SCI interfaces are referred to as SCI0 and SCI1. The.
Serial I/O Port.
8051 Microcontroller Thanks to Prof. P.Suresh, RCET.
Serial mode of data transfer
8051 Pin - out PORT 0 PORT 1 PORT 2 PORT 3.
Assembly Language * * * * * * * 8051 Timer
The 8085 Microprocessor Architecture
Source: Serial Port Source:
Introduction to Micro Controllers & Embedded System Design Microprocessor/Microcontroller Department of Electrical & Computer Engineering Missouri University.
8051 Timers Timers are the peripherals of the 8051 Microcontroller.
Timer Source: under
Serial I/O and Data Communication.
SERIAL PORT PROGRAMMING
1 Input-Output Organization Computer Organization Computer Architectures Lab Peripheral Devices Input-Output Interface Asynchronous Data Transfer Modes.
Interrupt Source: under
Introduction to Micro Controllers & Embedded System Design Interrupt
An Introduction to Microprocessor Architecture using intel 8085 as a classic processor
Data bus CPU General Purpose microprocessor RAM ROM I/O Port Timer
* * * * * * * 8051 Interrupts Programming.
Computer Organization and Design
EMT 348: Microcontroller Timer/counter
Source: Serial Port Source:
Introduction to Micro Controllers & Embedded System Design Microprocessor/Microcontroller Department of Electrical & Computer Engineering Missouri University.
Introduction to Micro Controllers & Embedded System Design Timer Operation Department of Electrical & Computer Engineering Missouri University of Science.
Timer.
Memory organization On- chip memory Off-chip memory
Serial Communication Interface: Using 8251
Introduction to Micro Controllers & Embedded System Design Addressing Mode Department of Electrical & Computer Engineering Missouri University of Science.
The 8051 Microcontroller (8 bit )
8085 Microprocessor Architecture
8051 Microcontroller.
Timer Source: under
X1 & X2 These are also called Crystal Input Pins.
Interrupt Source: under
Interrupt.
Important 8051 Features On chip oscillator 4K bytes ROM 128 bytes RAM
UNIT-VIII 8051 Microcontroller Architecture Register set of 8051
JANAKIRAMAN E G S PILLAY ARTS AND SCIENCE COLLAGE NAGAPATTINAM DEPARTMENT OF PHYSICS.
Interrupt Source: under
INTRODUCTION TO 8051, A 8-BIT MICROCONTROLLER
8051 Micro Controller.
The 8051 Microcontroller (8 bit )
Source: Serial Port Source:
Timer Source: under
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
Compiled by Dr. N.Shanmugasundaram, HOD, ECE Dept, SECE.
Prof Afonso Ferreira Miguel
Source: Serial Port Source:
8051SERIAL PORT PROGRAMMING
Presentation transcript:

Introduction to Micro Controllers & Embedded System Design I/O Processing and Serial Port Operation Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst.edu A.R. Hurson

Recall 8051 has a total of 40 pins out of which 32 pins are dedicated to four I/O ports of eight pins each (i.e., P0, P1, P2, and P3). All port upon RESET are configured as input. When the first 0 is written to a port, it becomes an output. To reconfigure it as an input, a 1 must be sent to the port. A.R. Hurson

Pin Configuration AD7 AD6 AD5 AD4 AD3 AD2 AD1 AD0 P0.7 P0.6 P0.5 P0.4 32 33 34 35 36 37 38 39 8 7 6 5 4 3 2 1 P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 28 27 26 25 24 23 22 21 17 16 15 14 13 12 11 10 P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0 RD WR T1 T0 INT1 INT0 TXD RXD 29 30 31 9 40 VCC VSS 19 18 20 RST XTL2 PSEN ALE EA XTL1 A15 A14 A13 A12 A11 A10 A9 A8 Pin Configuration A.R. Hurson

Ports status upon reset Register Value P0 11111111 P1 P2 P3 A.R. Hurson

Port 0 (pins 32 through 39): it has a dual role; an I/O port and a buffer for both data and address (when connected to an external memory). To make port 0 as input, 1 must be written to all its bits: Example: MOV A, #0FFH ; Note FFH = 11111111B MOV P0, A ; Now P0 is an input port BACK: MOV A, P0 ; Get data from P0 MOV P1, A ; Send data to port 1 SJMP BACK A.R. Hurson

Port 1 (pins 1 through 8) can be used as input or output Port 1 (pins 1 through 8) can be used as input or output. Upon reset, it is configured as input. Example: MOV A, #0FFH ; Note FFH = 11111111B MOV P1, A ; Now P1 is an input port MOV A, P1 ; Get data from P1 MOV R7, A ACALL DELAY ; Wait MOV R6, A ACALL DELAY MOV R5, A A.R. Hurson

Port 2 (pins 21 through 28) is very similar to port 1 and can be used as input or output. Note in 8031 based systems, port 2 has another function; along with port 0, it is used as a 16-bit address register for external memory. A.R. Hurson

Port 2 has a dual rule, since 8051/31 is capable of accessing a 64K bytes of external memory, we need a 16- bit address register. As a result, Port 2 (A8-A15) along with Port 0 (AD0-AD7) form such a 16-bit address register. A.R. Hurson

Port 3 (pins 10 through 17) in addition of being an I/O port, it has an alternative function – interrupts. Bit number Function Pin Number Remarks P3.0 RxD 10 Serial communication P3.1 TxD 11 P3.2 12 External Interrupt P3.3 13 External interrupt P3.4 T0 14 Timer 0 P3.5 T1 15 Timer 1 P3.6 16 External Memory P3.7 17 A.R. Hurson

Bit addressability of I/O ports All four ports of 8051 can be accessed either as an eight bit entity or any single bit entity. The general format of bit addressability is as: SETB PX.Y Where X is the port number (i.e., 0, 1, 2, or 3) and Y is the bit number (i.e., 0, 1, 2, 3, 4, 5, 6, or 7) Example: SETB P1.2 BACK: CPL P1.2 ACALL DELAY SJMP BACK A.R. Hurson

Example: Create a square wave of 50% duty cycle on bit 0 of port1 HERE: SETB P1.0 LCALL DELAY CLR P1.0 SJMP HERE 8051 P1.0 A.R. Hurson

Example: Create a square wave of 66% duty cycle on bit 3 of port1 BACK: SETB P1.3 LCALL DELAY CLR P1.3 SJMP BACK 8051 P1.3 A.R. Hurson

Example: Write a program to perform the following: a) Keep monitoring P1.2 until it becomes high b) When P1.2 becomes high, write value 45H to port 0 c) Send a high-to-low pulse to P2.3 SETB P1.2 ; Make P1.2 an input MOV A, #45H AGAIN: JNB P1.2, AGAIN MOV P0, A SETB P2.3 CLR P2.3 A.R. Hurson

a) If SW = 0, send letter ‘N’ to P2. Example: A switch is connected to pin P1.7. Write a program to check the status of SW and perform the following: a) If SW = 0, send letter ‘N’ to P2. b) If SW = 1, send letter ‘Y’ to P2. SETB P1.7 ; Make P1.7 an input AGAIN: JB P1.7, OVER MOV P2, #’N’ SJMP AGAIN OVER: MOV P2, #’Y’ A.R. Hurson

Serial port operation The function of serial port is to perform parallel-to- serial conversion for output data and serial-to-parallel conversion for input data. Hardware access to serial port is through the TxD and RxD pins which are the alternate functions for two port bits: P3.1 on pin 11 (TxD) and P3.0 on pin 10 (RxD). A.R. Hurson

Bit number Function Pin Number Remarks P3.0 RxD 10 Serial communication P3.1 TxD 11 P3.2 12 External Interrupt P3.3 13 External interrupt P3.4 T0 14 Timer 0 P3.5 T1 15 Timer 1 P3.6 16 External Memory P3.7 17 A.R. Hurson

Serial port operation The serial port features full duplex operation (i.e., simultaneous transmission and reception) and receive buffering, allowing one character to be received and held in the buffer while a second character is received. As a result, if CPU reads the first character before the second character is fully received, no data is lost. A.R. Hurson

Serial port operation Two special function registers provide software access to the serial port: The Serial port BUFfer at address 99H, and The Serial CONtrol register at address 98H. A.R. Hurson

Byte address Byte address 90 8D 8C 8B 8A 89 88 87 83 82 81 80 P1 TH1 TL1 TL0 TMOD TCON PCON DPH DDL SP P0 FF F0 E0 D0 B8 B0 A8 A0 99 98 Byte address B ACC PSW IP P3 IE P2 SBUF SCON A.R. Hurson

Serial port operation The Serial port BUFfer (SBUF) is in fact two buffers. Writing to SBUF loads data to be transmitted and reading SBUF accesses received data. The Serial CONtrol (SCON) is a bit addressable register containing status bits and control bits. Status bits indicate the end of a character transmission or reception. These bits are tested in software or programmed to cause an interrupt. Control bits set the operating mode for the serial port. A.R. Hurson

Serial port operation The serial port frequency of operation or baud rate, can be fixed (derived from on-chip oscillator) or variable. In case of variable baud rate, Timer1 supplies the baud rate clock and must be programmed accordingly. A.R. Hurson

Serial port operation SBUF (write only) TxD Internal bus Baud rate clock (transmit) TxD (P3.1) Internal bus Shift register Q CLK (receive) RxD (P3.0) (read only) D A.R. Hurson

Serial port control register The operation mode is set by writing to the serial port mode register (SCON) at address 98H. Note: Before using the serial port, SCON must be initialized for the correct mode: Example: MOV SCON, #01010010B Initializes the serial port for mode 1, enables the receiver (REN = 1), and sets the transmit interrupt flag (TI = 1) to indicate the transmitter is ready for operation. A.R. Hurson

Serial port control register BIT Symbol Address Semantics SCON.7 SM0 9FH Serial port mode bit 0 SCON.6 SM1 9EH Serial port mode bit 1 SCON.5 SM2 9DH Serial port mode bit 2. Enables multiprocessor communications in modes 2 and 3. SCON.4 REN 9CH Receiver enable (must be set to receive) SCON.3 TB8 9BH Transmit bit 8. 9th bit transmitted in modes 2 and 3. Set/cleared by software SCON.2 RB8 9AH Receive bit 8 SCON.1 TI 99H Transmit interrupt flag. Set at the end of character transmission. Cleared by software SCON.0 RI 98H Receive interrupt flag. Set at the end of character reception. Cleared by software A.R. Hurson

Modes of operation: Three modes enable asynchronous communications with each character received or transmitted framed by a start bit and a stop bit. The fourth mode allows the serial port to operate as a shift register. SM0 SM1 Mode Semantics Baud rate Shift register Fixed (oscillator frequency/12) 1 8-bit UART Variable set by Timer 2 9-bit UART Fixed (oscillator frequency/12 or /16) 3 A.R. Hurson

Mode 0: 8-bit shift register (SM0 = 0 and SM1 = 0) Serial data enter and exit through RxD line and TxD line serves as the clock. Eight bits are transmitted or received with the least significant bit first. The baud rate is fixed at 1/12th the on-chip oscillator frequency. Transmission is initiated by any instruction that writes data to SBUF. Each transmitted bit is valid on the RxD pin for one machine cycle. Reception is initiated when the receiver enable bit (REN) is 1 and receiver interrupt bit (RI) is 0. The general rule is to set REN at the beginning of a program to initialize the serial port and then clear RI to begin a data input operation. A.R. Hurson

Mode 1: 8-bit UART(SM0 = 0 and SM1 = 1) A Universal Asynchronous Receiver/Transmitter is a devise that receives and transmits serial data with each data character preceded by a start bit and followed by a stop bit. A parity bit is sometimes inserted between the last data bit and the stop bit. The baud rate is set by the Timer 1 overflow rate. A.R. Hurson

Mode 1: 8-bit UART(SM0 = 0 and SM1 = 1) Transmission is initiated by writing to SBUF and supplying the serial port baud rate. Reception is initiated by a 1-to-0 transition on RxD. After detection of a valid start bit, character reception continues. The start bit is skipped and eight bit data bits are clocked into the serial port shift register. After all eight bits are checked in, the following occurs: The ninth bit (the stop bit) is clocked into RB8 of SCON, SBUF is loaded with the eight data bits, and The receiver interrupt flag (RI) is set. A.R. Hurson

Mode 2: 9-bit UART(SM0 = 1 and SM1 = 0) In this case eleven bits are transmitted or received; a start bit, eight data bits, a programmable ninth data bit, and a stop bit. The ninth bit is whatever has been put in TB8 of SCON (perhaps a parity). On reception, the ninth bit received is placed in RB8. The baud rate is either 1/32nd or 1/64th of the on-chip oscillator frequency. A.R. Hurson

Mode 3: 9-bit UART(SM0 = 1 and SM1 = 1) This case is similar to mode2 except the baud rate is programmable and provided by Timer. A.R. Hurson

Initializing and accessing serial port registers Receiver Enable: The receiver enable bit (REN) in SCON must be set by software. This is done at the beginning of the program when the serial port , timer, etc. are initialized. SETB REN Or MOV SCON, #xxx1xxxxB Will do the job. A.R. Hurson

Initializing and accessing serial port registers Ninth data bit: The receiver enable bit (REN) in SCON must be set by software. This is done at the beginning of the program when the serial port , timer, etc. are initialized. SETB REN Or MOV SCON, #xxx1xxxxB Will do the job. A.R. Hurson

Initializing and accessing serial port registers Adding a Parity bit: The common use of the ninth bit is to add parity to a character. For example, if communication requires eight data bits plus even parity we have: MOV C, P ; Put even parity bit in TB8 MOV TB8, C ; This becomes the ninth bit MOV SBUF, A ; Move 8 bits from accumulator to SBUF A.R. Hurson

Initializing and accessing serial port registers Adding a Parity bit: Similarly, in case of odd parity we have: MOV C, P ; Put even parity bit in TB8 CPL C MOV TB8, C ; This becomes the ninth bit MOV SBUF, A ; Move 8 bits from accumulator to SBUF A.R. Hurson

Initializing and accessing serial port registers Adding parity is not limited to modes 2 or 3. Eight data bits transmission could be an ASCII code plus a parity bit: CLR ACC.7 ; To ensure the most significant bit is clear MOV C, P MOV ACC.7, C ; Put even parity into MSB MOV SBUF, A ; Send character, seven bits plus even parity A.R. Hurson

Interrupt Flags The receive and transmit interrupt flags (i.e., RI and TI) is SCON play important role in serial communications. Both bits are set by hardware but must ne cleared by software. A.R. Hurson

Interrupt Flags Typically, RI is set at the end of character reception to indicate “receive buffer is full”. This condition is tested in software to cause an interrupt. So to input a character from external device, one must wait until RI is set, then clear RI and read the character from SBUF. WAIT: JNB RI, WAIT ; Check RI until set CLR RI MOV A, SBUF ; Read character A.R. Hurson

Interrupt Flags TI is set at the end of character transmission to indicate “transfer buffer is empty”. So to send a character to a connected device to serial port, one must check to make sure that serial port is ready, i.e., previous character was sent out. WAIT: JNB TI, WAIT ; Check TI until set CLR TI MOV SBUF, A ; Send character A.R. Hurson

/12 Serial port Baud rate For modes 0 and 3 baud rate is fixed: In mode 0 it is always the on-chip oscillator frequency divided by 12. Usually, the crystal drives the on-chip oscillator, though other clock sources can be used as well. If the nominal oscillator frequency is 12 MHz, then the mode 0 baud rate is 1 MHz. /12 On-chip oscillator Baud rate Clock A.R. Hurson

Serial port Baud rate Following a system reset, the mode 2 baud rate is 1/64th of the oscillator frequency. Baud rate is also affected by bit 7 in Power CONtrol register (PCON). Setting this bit (i.e., SMOD) doubles the baud rate in modes 1, 2, and 3. A.R. Hurson

Serial port Baud rate /64 /32 In mode 2; SMOD = 0 implies default value of 1/64, and SMOD = 1 sets the baud rate to 1/32nd the oscillator frequency Note: PCON is not bit addressable. /64 On-chip oscillator Baud rate Clock /32  SMOD = 1 SMOD = 0 A.R. Hurson

Serial port Baud rate /32 /16 The baud rate in modes 1 and 3 are determined by Timer 1 overflow rate. Note: Timer operates at a relatively high frequency. MOV A, PCON ; Get current value of PCON SETB ACC.7 MOV PCON, A ; Write modified value back to PCON /32 Timer 1 Overflow Baud rate Clock /16  SMOD = 0 SMOD = 1 A.R. Hurson

Using Timer 1 as the Baud Rate Clock The usual technique for baud rate generation is to initialize TMOD and put the correct related value in TH1 to yield proper overflow rate. MOV TMOD, #0010xxxxB A.R. Hurson

Using Timer 1 as the Baud Rate Clock In case of modes 1 and 3: Baud rate = Timer1 overflow rate/32 i.e., 1200 baud rate operation requires an overflow rate of: 1200 = Timer 1 overflow rate/32 Timer 1 overflow rate = 38.4 KHz If a 12 MHz crystal drives on-chip oscillator, then Timer 1 Is clocked at a rate of 1 MHz. Since Timer must overflow at a rate of 38.4 KHz and Timer is clocked at the rate of 1000KHz, an overflow is required every 1000/38.4 = 26.04 clocks. Since the Timer counts up and overflows on transition from FFH to 00H then TH1 reload value must be -26. A.R. Hurson

Baud rate Summary Baud rate Crystal Frequency SMOD TH1 reload value Actual Baud rate Error 9600 12,000 MHz 1 -7 (F9H) 8923 7% 2400 -13 (F3H) 2404 .16% 1200 -26 (E6H) 1202 19200 11,059 MHz -3 (FDH) -12 (F4H) -24 (E8H) A.R. Hurson

Example Write an instruction sequence to initialize the serial port as an 8-bit UART at 2400 baud: The registers involved and their contents: SCON SM0 SM1 SM2 REN TB8 RB8 TI RI 1 TMOD GTE C/T M1 M0 M2 TCON TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TH1 A.R. Hurson

Example Write an instruction sequence to initialize the serial port as an 8-bit UART at 2400 baud: ORG 8100H 759852 INIT: MOV SCON, #52H ; Serial port mode 1 8103 758920 MOV TMOD, #20H ; Timer 1 mode 2 758DF3 MOV TH1, #-13 ; Reload count 8109 D28E SETB TR1 ; Start Timer 1 END A.R. Hurson

Example Write a subroutine to transmit 7-bit ASCII code in the accumulator out of the serial port , with odd parity bit added as the 8th bit: ORG 8100H A2D0 OUT: MOV C, P ; Parity bit in C 8102 B3 CPL C ; Odd parity 8103 92E7 MOV ACC.7, C ; Add to Character 8105 3099FD AGAIN: JNB TI, AGAIN ; Loop until 8108 C299 CLR TI ; Clear flag 810A F599 MOV SBUF, A ; Send character 810C C2E7 CLR ACC.7 ; reset accumulator 810E 22 RET END A.R. Hurson

Example Write a subroutine to input a character from the serial port and return with 7-bit ASCII code in the accumulator with the expectation of odd parity. Set the carry flag if there is a parity error. ORG 8100H 3099FD INPUT: JNB RI, $ ; Wait for character 8103 C298 CLR RI ; Clear flag 8105 E599 MOV A, SBUF A2D0 MOV C, P ; For odd parity in A ; P should be set 8109 B3 CPL C ; Indicate if “error” 810A C2E7 CLR ACC.7 ; Strip off parity 810C 22 RET END A.R. Hurson