PROGRAMMING THE SECOND SERIAL PORT

SECTION 10.4: PROGRAMMING THE SECOND SERIAL PORT
Many of the new generations of the 8051 microcontrollers come with two serial ports. The DS89C4xO.(DS89C420/30/40/…) and DS80C320 are among them. In this section we show the programming of the second serial port of the DS89C4xO chip.
DS89C4xO second serial port
The second serial port of the DS89C4xO uses pins PI .2 and PI .3 for the Rx and Tx lines, respectively. See Figure 10-10. The MDE8051 Trainer (available from www.MicroDigitalEd.com) uses the DS89C4xO chip and comes with two serial ports already installed. It also uses the MAX232 for the RS232 connection to DB9. The connections for the RS232 to the DS89C4xO of the MDE8051 Trainer are shown in Figure 10-11. Notice that the first and second serial ports are designated as Serial #0 and Serial #1, respectively.
Addresses for all SCON and SBUF registers
All the programs we have seen so far in this chapter assume the use of the first serial port as the default serial port since every version of the 8051 comes with at least one serial port. The SCON, SBUF, and PCON registers of the 8051 are part of the special function registers. The address for each of the SFRs is shown in Table 10-6. Notice that SCON has address 98H, SBUF has address 99H, and finally PCON is assigned the 87H address. The first serial port is supported by all assemblers and C compilers in the market for the 8051. If you examine the list file for 8051 Assembly language programs, you will see that these labels are replaced with their SFR addresses. The second serial port is not implemented by all versions of the 8051/52 microcontroller. Only a few versions of the 8051/52, such as the DS89C4xO, come with the second serial port. As a result, the second serial port uses some reserved SFR addresses for the SCON and SBUF registers and there is no universal agreement among the makers as to which addresses should be used. In the case of the DS89C4xO, the SFR addresses of COH and C1H are set aside for SBUF and SCON, as shown in Table 10-6. The DS89C4xO technical documentation refers to these registers as SCON1 and SBUF1 since the first ones are designated as SCONO and SBUFO.
Table 10-6: SFR Byte Addresses for DS89C4xO Serial Ports






Figure 10-10. DS89C4xO Pin Diagram
Note: Notice PI.2 and P1.3 pins are used by Rx and Tx lines of the 2nd serial port

Figure 10-11. (a) Inside MAX232 and (b) its Connection to the DS89C4xO


Table 10-7:
SFR Addresses for the DS89C4xO (420,
430, etc.)
Symbol
Name
Address
ACC*
Accumulator
EOH
B*
B register
FOR
PSW*
Program status word
DOH
SP
Stack pointer
81H
DPTR
Data pointer 2 bytes

DPL
Low byte
82H
DPH
High byte
83H
PO*
PortO
80H
PI*
Port 1
90H
P2*
Port 2
OAOH
P3*
Port3
BOH
IP*
Interrupt priority control
B8H
IE*
Interrupt enable control
A8H
TMOD
Timer/counter mode control
89H
ICON*
Timer/counter control
88H
T2CON*
Timer/counter 2 control
C8H
T2MOD
Timer/counter mode control
C9H
THO
Timer/counter 0 high byte
8CH
TLO
Timer/counter 0 low byte
8AH
TH1
Timer/counter 1 high byte
8DH
TL1
Timer/counter 1 low byte
8BH
TH2
Timer/counter 2 high byte
CDH
TL2
Timer/counter 2 low byte
CCH
RCAP2H
T/C 2 capture register high byte
CBH
RCAP2L
T/C 2 capture register low byte
CAH
SCONO*
Serial control (first serial port)
98H
SBUFO
Serial data buffer (first serial port)
99H
PCON
Power control
87H
SCON1*
Serial control (second serial port)
COH
SBUF1
Serial data buffer (second serial port)
C1H
* Bit-addressable
Programming the second serial port using timer 1
While each serial port has its own SCON and SBUF registers, both ports can use Timer 1 for setting the baud rate. Indeed, upon reset, the DS89C4xO chip uses Timer 1 for setting the baud rate of both serial ports. Since the older 8051 assemblers do not support this new second serial port, we need to define them as shown in Example 10-11. Notice that in both C and Assembly, SBUF and SCON refer to the SFR registers of the first serial port. To avoid confusion, in DS89C4xO programs we use SCONO .and SBUFO for the first and SCON1 and SBUF1 for the second serial ports. For this reason, the MDE8051 Trainer designates the serial ports as Serial #0 and Serial #1 in order to comply with this designation.
See Examples 10-12 through 10-14.


Figure 10-12. SCONO and SCON1 Bit Addresses (TI and RI bits must be noted)
Example 10-11
Write a program for the second serial port of the DS89C4xO to continuously transfer the letter “A” serially at 4800 baud. Use 8-bit data and 1 stop bit. Use Timer 1.




Example 10-12
Write a program to send the text string “Hello” to Serial #1. Set the baud rate at 9600, 8-bit data, and 1 stop bit.


Example 10-13
Program the second serial port of the DS89C4xO to receive bytes of data serially and put them on PI. Set the baud rate at 4800, 8-bit data, and 1 stop bit.


Examole 10-14
Assume that a switch is connected to pin P2.0.
Write a program to monitor the switch and perform the following:
  1. If SW = 0 send the message “Hello” to the Serial #0 port.
  2. If SW = 1 send the message “Goodbye” to the Serial #1 port.

Next post:

Previous post: