Download presentation
Presentation is loading. Please wait.
Published byAlžběta Horáčková Modified over 5 years ago
1
Source: http://www.edsim51.com/8051Notes/8051/serial.html
Serial Port Source:
2
Serial communication Asynchronous serial communication
Data Data Clock Internally generated clock synchronous to the data Tx Gnd Rx Tx Clk Gnd Rx Tx Rx Gnd Tx Rx Clk 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 8051 serial port:
Q: Simplex? Half-duplex? Full duplex? Q: Synchronous? Asynchronous?
5
Serial port in 8051 8051 8051 Properties of 8051 serial port:
*) Full duplex (i.e., it can transmit and receive data at the same time.) *) Asynchronous *) The data rate (baud rate, bit/sec) is controller by timer 1.
6
RelatedRegisters SMOD
7
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
8
Serial port modes SM0 SM1 Mode Description Baud Rate Mode 0
Mode 0 shift register fixed - system clock frequency divided by 12 (machine cycle frequency) 1 Mode 1 8-bit UART variable - set by timer 1 Mode 2 9-bit UART fixed - system clock frequency divided by 12 or divided by 64 Mode 3 9-bit variable baud rate UART
9
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.
10
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. Default baud rate = 12 MHz NT 32 Timer (NT) 12 12 MHz Baud rate Default baud rate = 12 MHz NT 32
11
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 with an error of 0.04) = 230 Timer (/26) /12 12 MHz
12
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 (8-bit UART mode) MOV TMOD, #20H ;put timer 1 in mode 2 (8-bit auto-reload mode) 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.
13
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.
14
Calculation of system clock rate for desired baud rate
Counter cycle = (system frequency / (12 * 32)) / baud Counter cycle = (system frequency / (12 * 16)) / baud TH1 = 256 – counter cycle. Real baud Rate = system clock/(12*32)/(rounded counter cycle) Real baud Rate = system clock/(12*16)/(rounded counter cycle) system clock (modified) = Baud Rate * (12*32) * (rounded counter cycle) system clock (modified) = Baud Rate * (12*16) * (rounded counter cycle) Baud rate (desired) System frequency SMOD Counter cycle (exact) Counter cycle (rounding) TH1 Baud rate (real) System clock (modified) 19200 12 MHz 2 254 15625 1 3 253 9600 6 250 4800 7 249 13 243 2400 26 230 1200 52 204
15
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,
17
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.
18
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.
19
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.
20
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’, then P = ? Ans: ACC = B, P = (Acc has odd parity) Example 2: If ACC contains the ASCII code of ‘S’ Ans: ACC = B, P = (Acc has even parity) How can even parity be maintained while transmitting an ASCII code? Method: Use the MSB for parity purpose, because 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
21
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? Acc = B (‘A’) P = Transmit B Acc = (‘C’) P = Transmit B
22
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.
23
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
24
Error detection 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 .
25
PCON Power Management Register (PCON)
Power management using a microcontroller is something you see every day in mobile phones. Haven’t you noticed and got wondered by a mobile phone automatically going into stand by mode when not used for a couple of seconds or minutes ? This is achieved by power management feature of the controller used inside that phone. As the name indicates, this register is used for efficient power management of 8051 micro controller. Commonly referred to as PCON register, this is a dedicated SFR for power management alone. From the figure below you can observe that there are 2 modes for this register :- Idle mode and Power down mode. Setting bit 0 will move the micro controller to Idle mode and Setting bit 1 will move the micro controller to Power down mode.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.