Download presentation
Presentation is loading. Please wait.
Published byBerenice Ashlynn Stevenson Modified over 5 years ago
1
Source: http://www.edsim51.com/8051Notes/8051/serial.html
Serial Port Source:
2
Serial communication Asynchronous serial communication
Tx Clk Gnd Rx Tx Gnd Rx Tx Rx Clk Gnd Tx Rx Gnd
3
Transmission modes Simplex One way all time Half-duplex Or
Either way all time Full-duplex Two ways all time Receiver Transmitter Transmitter/Receiver Transmitter/Receiver Transmitter/Receiver
4
Serial port in 8051 8051 Properties of 8051 serial port:
*) Full duplex (i.e., it can transmit and receive data at the same time.) *) The data rate (baud rate, bit/sec) is controller by timer 1. *)
5
RelatedRegisters
6
Serial Control Register (SCON)
Bit Symbol Address Description 7 SM0 9FH serial port mode bit 0 6 SM1 9EH serial port mode bit 1 5 SM2 9DH serial port mode bit 2 - will be dealt with at a later date 4 REN 9CH receiver enable - this bit must be set to receive data 3 TB8 9BH transmit bit 8 - this is the ninth bit transmitted in the 9-bit UART modes 2 RB8 9AH receive bit 8 - this is the ninth bit received in the 9-bit UART modes 1 TI 99H transmit interrupt flag - this bit is set by hardware when an entire byte has been transmitted - it must be cleared by software RI 98H receive interrupt flag - this bit is set by hardware when an entire byte has been received - it must be cleared by software
7
Serial port modes SM0 SM1 Mode Description Baud Rate shift register
shift register fixed - system clock frequency divided by 12 (machine cycle frequency) 1 8-bit UART variable - set by timer 1 2 9-bit UART fixed - system clock frequency divided by 12 or divided by 64 3 9-bit variable baud rate UART
8
Mode 1 --- 8-bit UART UART (Universal Asynchronous Receive/Transmit)
This is asynchronous communication, therefore no clock signal is sent with the data. Instead, a single start bit (always logic 0) and a single stop bit (always logic 1) encapsulate the data (8 bits). Therefore, in total, ten bits are sent per byte of data.
9
Baud rate If the designer of an 8051 system is required to transmit data from the serial port to a device (e. g., a monitor), he/she needs to discover what baud rate the device's serial port operates at and configure the 8051 serial port to operate at the same baud rate. In mode 1, the baud rate is determined by the overflow of timer 1. By default, the baud rate is equal to the timer 1 overflow frequency divided by 32. Timer (??) 12 12 MHz Baud rate
10
How to 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. The formula that determines the relationship between TH1 and baud rate: 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: = ((12MHz / (12 * 32)) / 1200) = = (rounding down => error of 0.04) = 230 Timer (26) 12 12 MHz
11
The code below shows how to configure both timer 1 and the serial port to transmit data at 1200 baud. CLR SM ; SETB SM ; 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 Once the timer is started the programmer does not need to do anything else with it. The serial port itself monitors (through hardware) the overflow signal and every 32nd overflow (16 if SMOD is set) a bit is transmitted.
12
Various standard baud rates
The table shows some standard baud rates together with the required TH1 value to achieve this baud rate with the 8051 serial port in mode 1.
13
Why MHz In Example 9, why do we need to choose MHz for the system clock when we select the 19,200 baud rate? Ans. Because MHz / 12 / 3 / 16 = 20, MHz / 12 / 3 / 16 = 19, 19,200 Timer (3) 12 M 19,
15
8051 instructions for transmitting and receiving data
MOV SBUF, #45H MOV A, SBUF To transmit data along the serial line you simply write to the serial buffer and to access data received on the serial port you simply read data from the serial buffer. For example MOV SBUF, #45H ; to send the byte 45H down the serial line MOV A, SBUF ; to take data received by the serial port and puts it in the accumulator.
16
How do we know when the complete data byte has been sent?
MOV SBUF, #23H MOV SBUF, #56H If the second byte is sent to SBUF immediately after the first by is sent, the second byte may be overwritten before it was completely shifted down the line MOV SBUF, #23H; JNB TI, $ CLR TI MOV SBUF, #56H; “JNB TI, $” is to wait for the byte to be transmitted down the line. The transmit interrupt flag (TI) is set by hardware once an entire byte has been transmitted. The flag TI must be clear by software.
17
How do we know when data has been received?
JNB RI, $ CLR RI MOV A, SBUF “JNB RI, $” is to wait for an entire byte to be received The receive interrupt flag (RI) is set by hardware when an entire byte is received by the serial port. RI must be cleared by software.
18
Parity Even parity in 8051 accumulator:
Definition: The total number of 1’s in the accumulator and the parity bit (of the PSW) is even. Example 1: If ACC contains the ASCII code of ‘Q’ ACC = B, P = 1 Example 2: If ACC contains the ASCII code of ‘S’ ACC = B, P = 0 How can even parity be maintained while transmitting an ASCII code? Method: Use the MSB for parity purpose, becqause it is not used for encoding the ASCII code. Example: Original In the serial port To transmit ‘Q’ ACC = B, P = 1 B To transmit ‘S’ ACC = B, P = 0 B
19
Parity Instructions in program example 8 MOV C, P MOV ACC.7, C Purpose
To make the Acc even parity Q1 How does the p flag change? (What causes it to change? To what does it change?) 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. Q2 How can we make the Acc odd parity?
20
Bit-addressability of SFR
SM0, SM1 bits in SCON register SMOD bit in PCON register Example instructions CLR SM0 SETB SM1 MOV A, PCON SETB ACC.7 MOV PCON, A Implication The bits in SCON is bit-addressable. The bits in PCON is NOT bit-addressable.
21
The ASCII codes go from 0 to 7FH
The ASCII codes go from 0 to 7FH. Therefore, only seven bits are needed to code the basic ASCII characters. This leaves the 8th bit free - we can use this spare bit as a parity bit
22
The receiver must be able to count the number of ones in the received data. If an 8051 was being used to receive even parity ASCII data on the serial line it could test the data validity as shown below: waitForByte: JNB RI, $ ;wait for a byte to be received CLR RI MOV A, SBUF ;move the received byte to the accumulator JB P, waitForByte ;if the parity bit is set, data transmission error => ignore data CLR ACC.7 ;valid data - therefore clear the parity bit .
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.