Presentation is loading. Please wait.

Presentation is loading. Please wait.

Serial I/O Port.

Similar presentations


Presentation on theme: "Serial I/O Port."— Presentation transcript:

1 Serial I/O Port

2 Synchronous communication
Asynchronous communication

3 Because serial communication requires less physical wires, it is more suitable for transmitting data over longer distances. Advantage: Fewer wires Longer distance Disadvantage:Lower data rate

4 Baud rate: the data rate in serial communication; the number of bits transmitted per second.
The 8051 serial port is full duplex. In other words, it can transmit and receive data at the same time. The serial port can be transmitting data down the TXD line while it is at the same time receiving data on the RXD line.

5 SBUF register At location 99H is the serial buffer special function register (SBUF). Unlike any other register in the 8051, SBUF is in fact two distinct registers - the write-only register and the read-only register. To transmit data along the serial line you simply write to the serial buffer. To access data received on the serial port you simply read data from the serial buffer. For example: MOV SBUF, #45H ; This sends the byte 45H down the serial line MOV A, SBUF ; This takes whatever data was received by the serial port and puts it in the accumulator

6 Transmitted data is sent out from the write-only register.
Received data is stored in the read-only register.

7

8 How do we know when the complete data byte has been sent?
The serial port control register (SCON) contains a bit which alerts us to the fact that a byte has been transmitted The transmit interrupt flag (TI) is set by hardware once an entire byte has been transmitted down the line. Since SCON is bit-addressable we can test this bit and wait until it is set. In the following instruction sequence, the initial character will be overwritten before it was completely shifted down the line. MOV SBUF, #23H MOV SBUF, #56H MOV SBUF, #23H ; send the first byte down the serial line JNB TI, $ ; wait for the entire byte to be sent CLR TI ; the transmit interrupt flag is set by hardware but must be cleared by software MOV SBUF, #56H ; send the second byte down the serial line

9 How do we know when data has been received?
The receive interrupt flag (RI) in SCON is set by hardware when an entire byte is received by the serial port. The code below shows how you would program the controller to wait for data to be received and to then move that data into the accumulator. JNB RI, $ ; wait for an entire byte to be received CLR RI ; the receive interrupt flag is set by hardware but must be cleared by software MOV A, SBUF ; move the data stored in the read-only buffer to the accumulator Once an entire byte is received (ie; shifted along RXD into the shift register) the RI bit is set and the data byte is moved into the read-only buffer.

10 Bit 4, as indicated, must be set if data is to be received.
Therefore, the above code for reading a byte from the serial port into the accumulator would first have to include a line for setting REN, as shown below. SETB REN ; set bit 4 of SCON so that data can be received JNB RI, $ ; wait for an entire byte to be received CLR RI ; the receive interrupt flag is set by hardware but must be cleared by software MOV A, SBUF ; move the data stored in the read-only buffer to the accumulator

11 Bits 7 and 6 are used for putting the serial port into one of the four modes, as detailed in the table below.

12 MODE 0

13 MODE 1

14 In mode 1, the baud rate is determined by the overflow of timer 1
In mode 1, the baud rate is determined by the overflow of timer 1. However, since the timer operates at a relatively high frequency (compared to serial port baud rates) the timer 1 overflow frequency is divided by either 32 or 16, as shown in the diagram below.

15 How does TH1 control the baud rate
To configure the port to a specific baud rate we therefore need to configure timer 1 to give us an overflow at an appropriate interval. This can be achieved by setting up timer 1 as an 8-bit auto-reload interval timer. Then, if we put the correct value into TH1 the overflow signal will have the appropriate frequency to give us the baud rate we desire. To find the correct value to load into TH1 we use the following formula: TH1 = ((system frequency / (12 * 32)) / baud) For example, to achieve a baud rate of 1200 using a system clock frequency of 12MHz and with SMOD = 0: TH1 = ((system frequency / (12 * 32)) / baud) TH1 = ((12MHz / (12 * 32)) / 1200) TH1 = TH1 = (rounding down => error of 0.04) TH1 = 230

16 The value 230 in TH1 will result in timer 1 counting continuously from 230 to 255. TF1 will be set every time the count rolls over from 255 back to 230. Since we are using a system clock frequency of 12MHz, the timer changes state every 1us (remember, the timer clock frequency is the system clock frequency divided by 12 - hence the divide by 12 in the above equation). This results in an overflow every 26us which implies a timer overflow frequency of 1 / 26us = 38.46kHz. The baud rate is the timer overflow frequency divided by 32 (hence the divide by 32 in the above equation) which, in this case is 38.46kHz / 32 = 1202 bits/sec. The code below shows how to configure both timer 1 and the serial port to transmit data at 1200 baud. CLR SM0 SETB SM ;clear SM0 and set SM1 to put the serial port in mode 1 MOV TMOD, #20H ;put timer 1 in mode 2 interval timing MOV TH1, # ;put the reload value in TH1 - this results in a baud rate of 1200 SETB TR ;start timer 1 MOV SBUF, A ;send data in the accumulator down the serial line

17 How do we set SMOD? SMOD is the MSB of the PCON register. However, if you look at the memory map you will notice that PCON, at address 87H, is not bit addressable. Therefore we cannot simply use the SETB instruction to set SMOD. The code below shows how you read-modify-write PCON in order to set SMOD without changing the other seven bits in the register. MOV A, PCON ;copy PCON to the accumulator SETB ACC ;set the accumulator MSB MOV PCON, A ;copy the accumulator back to PCON

18 Baud rate table

19 Adding a Parity Bit to ASCII Data
The 8051 maintains even parity with the accumulator, ie; the number of ones in the accumulator together with the parity bit (in the program status word) is always even. How to transmit and receive ASCII characters with even parity: MOV A, 30H ; move the ASCII character stored in location 30H to the ACC MOV C, P ;copy the parity bit to the carry bit MOV ACC.7, C ;then move it from the carry bit to the ACC MSB MOV SBUF, A ;transmit the character along with the parity bit down the serial line JNB TI, $ ; wait for the byte to be sent CLR TI

20

21 Timer 1

22

23


Download ppt "Serial I/O Port."

Similar presentations


Ads by Google