Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Micro Controllers & Embedded System Design I/O Processing and Serial Port Operation Department of Electrical & Computer Engineering Missouri."— Presentation transcript:

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

2 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

3 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

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

5 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 = B 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

6 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 = B 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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, # B 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 /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

40 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

41 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

42 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

43 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

44 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 = clocks. Since the Timer counts up and overflows on transition from FFH to 00H then TH1 reload value must be -26. A.R. Hurson

45 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

46 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

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

48 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 H A2D0 OUT: MOV C, P ; Parity bit in C 8102 B3 CPL C ; Odd parity E7 MOV ACC.7, C ; Add to Character FD 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

49 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 H 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


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

Similar presentations


Ads by Google