490 likes | 499 Views
This introduction covers I/O processing and serial port operation in microcontrollers. Learn about the 8051 microcontroller and its pin configuration, port functionalities, bit addressability, and serial port operations.
E N D
Introduction to Micro Controllers&Embedded System DesignI/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
VCC 40 AD7 AD6 AD5 AD4 AD3 AD2 AD1 AD0 P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0 32 33 34 35 36 37 38 39 19 XTL1 RD WR T1 T0 INT1 INT0 TXD RXD Pin Configuration 18 XTL2 29 30 31 9 PSEN P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 8 7 6 5 4 3 2 1 ALE EA RST 17 16 15 14 13 12 11 10 P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0 A15 A14 A13 A12 A11 A10 A9 A8 P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 28 27 26 25 24 23 22 21 20 A.R. Hurson VSS
Ports status upon reset 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. 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 A, P1 ; Get data from P1 MOV R6, A ACALL DELAY MOV A, P1 ; Get data from P1 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 2has 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. 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 LCALL DELAY 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 LCALL DELAY CLR P1.3 LCALL DELAY 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
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’ SJMP AGAIN 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
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 TH0 TL1 TL0 TMOD TCON PCON DPH DDL SP P0 FF F0 E0 D0 B8 B0 A8 A0 99 98 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 RxD (P3.0) TxD (P3.1) Shift register D SBUF (write only) CLK CLK Q Baud rate clock (receive) Baud rate clock (transmit) SBUF (read only) Internal bus 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 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. 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
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. Baud rate Clock On-chip oscillator /12 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 • 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 SMOD = 0 On-chip oscillator Baud rate Clock SMOD = 1 /32 A.R. Hurson
Serial port Baud rate • 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 SMOD = 0 Timer 1 Overflow Baud rate Clock SMOD = 1 /16 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 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: 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