SERIAL PORT PROGRAMMING IN C

SECTION 10.5: SERIAL PORT PROGRAMMING IN C
This section shows C programming of the serial ports for the 8051/52 and DS89C4xO chips.
Transmitting and receiving data in 8051 C
As we stated in the last chapter, the SFR registers of the 8051 are accessible directly in 8051 C compilers by using the regSl.h file. Examples 10-15 through 10-19 show how to program the serial port in 8051 C. Connect your 8051 Trainer to the PC’s COM port and use HyperTerminal to test the operation of these examples.
Example 10-15





Example 10-16


Example 10-17




Example 10-18
Write an 8051 C program to send two different strings to the serial port. Assuming that
SW is connected to pin P2.0, monitor its status and make a decision as follows:
SW = 0: send your first name
SW = 1: send your last name
Assume XTAL = 11.0592 MHz, baud rate of 9600, 8-bit data, 1 stop bit.


Example 10-19
Write an 8051 C program to send the two messages “Normal Speed” and “High Speed”
to the serial port. Assuming that SW is connected to pin P2.0, monitor its status and set
the baud rate as follows:
SW = 0 28,800 baud rate
SW = 1 56K baud rate
Assume that XTAL = 11.0592 MHz for both cases.
Solution:


8051 C compilers and the second serial port
Since many C compilers do not support the second serial port of the DS89C4xO chip, we have to declare the byte addresses of the new SFR registers using the sfr keyword. Table 10-6 and Figure 10-12 provide the SFR byte and bit addresses for the DS89C4xO chip. Examples 10-20 and 10-21 show C versions of Examples 10-11 and 10-13 in Section 10.4.
Notice in both Examples 10-20 and 10-21 that we are using Tinier 1 to set the baud rate for the second serial port. Upon reset, Timer 1 is the default for the second serial port of the DS89C4xO chip.
Example 10-20
Write a C program for the DS89C4xO to transfer letter “A” serially at 4800 baud continuously. Use the second serial port with 8-bit data and 1 stop bit. We can only use Timer 1 to set the baud rate. Solution:

Example 10-21

Program the DS89C4xO in C to receive bytes of data serially via the second serial port and put them in PI. Set the baud rate at 9600, 8-bit data, and 1 stop bit. Use Timer 1 for baud rate generation.


SUMMARY
This chapter began with an introduction to the fundamentals of serial communication. Serial communication, in which data is sent one bit a time, is used when data is sent over significant distances since in parallel communication, where data is sent a byte or more a time, great distances can cause distortion of the data. Serial communication has the additional advantage of allowing transmission
over phone lines. Serial communication uses two methods: synchronous and asynchronous. In synchronous communication, data is sent in blocks of bytes; in asynchronous, data is sent in bytes. Data communication can be simplex (can send but cannot receive), half duplex (can send and receive, but not at the same time), or full duplex (can send and receive at the same time). RS232 is a standard for serial communication connectors.
The 8051 ‘s UART was discussed. We showed how to interface the 8051 with an RS232 connector and change the baud rate of the 8051. In addition, we described the serial communication features of the 8051, and programmed the 8051 for serial data communication. We also showed how to program the second serial port of the DS89C4xO chip in Assembly and C.

Next post:

Previous post: