Книга: Advanced PIC Microcontroller Projects in C

4.3.3 Software UART Library

4.3.3 Software UART Library

Universal asynchronous receiver transmitter (UART) software library is used for RS232-based serial communication between two electronic devices. In serial communication, only two cables (plus a ground cable) are required to transfer data in either direction. Data is sent in serial format over the cable bit by bit. Normally, the receiving device is in idle mode with its transmit (TX) pin at logic 1, also known as MARK. Data transmission starts when this pin goes to logic 0, also known as SPACE. The first bit sent is the start bit at logic 0. Following this bit, 7 or 8 data bits are sent, followed by an optional parity bit. The last bit sent is the stop bit at logic 1. Serial data is usually sent as a 10-bit frame consisting of a start bit, 8 data bits, and a stop bit, and no parity bits. Figure 4.21 shows how character “A” can be sent using serial communication. Character “A” has the ASCII bit pattern 01000001. As shown in the figure, first the start bit is sent, this is followed by 8 data bits 01000001, and finally the stop bit is sent.


Figure 4.21: Sending character “A” in serial communication

The bit timing is very important in serial communication, and the transmitting (TX) and receiving (RX) devices must have the same bit timings. The bit timing is measured by the baud rate, which specifies the number of bits transmitted or received each second. Typical baud rates are 4800, 9600, 19200, 38400, and so on. For example, when operating at 9600 baud rate with a frame size of 10 bits, 960 characters are transmitted or received each second. The timing between bits is then about 104ms.

In RS232-based serial communication the two devices are connected to each other (see Figure 4.22) using either a 25-way connector or a 9-way connector. Normally only the TX, RX, and GND pins are required for communication. The required pins for both types of connectors are given in Table 4.5.


Figure 4.22: 25-way and 9-way RS232 connectors

Table 4.5: Pins required for serial communication

Pin 9-way connector 25-way connector
TX 2 2
RX 3 3
GND 5 7

The voltage levels specified by the RS232 protocol are ±12V. A logic HIGH signal is at –12V and a logic LOW signal is at +12V. PIC microcontrollers, on the other hand, normally operate at 0 to 5V voltage levels, the RS232 signals must be converted to 0 to 5V when input to a microcontroller. Similarly, the output of the microcontroller must be converted to ±12V before sending to the receiving RS232 device. The voltage conversion is usually carried out with RS232 converter chips, such as the MAX232, manufactured by Maxim Inc.

Serial communication may be implemented in hardware using a specific pin of a microcontroller, or the required signals can be generated in software from any required pin of a microcontroller. Hardware implementation requires either an on-chip UART (or USART) circuit or an external UART chip that is connected to the microcontroller. Software-based UART is more commonly used and does not require any special circuits. Serial data is generated by delay loops in software-based UART applications. In this section only the software-based UART functions are described.

The mikroC compiler supports the following software UART functions:

• Soft_Uart_Init

• Soft_Uart_Read

• Soft_Uart_Write

Soft_Uart_Init

The Soft_Uart_Init function specifies the serial communications parameters and does so in the following order:

port, rx pin, tx pin, baud rate, mode

port is the port used as the software UART (e.g., PORTB), rx is the receive pin number, tx is the transmit pin number, baud rate is the chosen baud rate where the maximum value depends on the clock rate of the microcontroller, and mode specifies whether or not the data should be inverted at the output of the port. A 0 indicates that it should not be inverted, and a 1 indicates that it should be inverted. When an RS232 voltage level converter chip is used (e.g., MAX232) then the mode must be set to 0. Soft_Uart_Init must be the first function called before software-based serial communication is established.

The following example configures the software UART to use PORTB as the serial port, with RB0 as the RX pin and RB1 as the TX pin. The baud rate is set to 9600 with the mode noninverted:

Soft_Uart_Init(PORTB, 0, 1, 9600, 0);

Soft_Uart_Read

The Soft_Uart_Read function receives a byte from a specified serial port pin. The function returns an error condition and the data is read from the serial port. The function does not wait for data to be available at the port, and therefore the error parameter must be tested if a byte is expected. The error is normally 1 and becomes 0 when a byte is read from the port.

The following example illustrates reading a byte from the serial port configured by calling the function Soft_Uart_Init. The received byte is stored in variable Temp:

do Temp = Soft_Uart_Read(Rx_Error);
while (Rx_Error);

Soft_Uart_Write

The Soft_Uart_Write function transmits a byte to a configured serial port pin. The data to be sent must be specified as a parameter in the call to the function.

For example, to send character “A” to the serial port pin:

char MyData = 'A';
Soft_Uart_Write(MyData);

The following example illustrates the use of software UART functions.

Example 4.13

The serial port of a PC (e.g., COM1) is connected to a PIC18F452 microcontroller, and terminal emulation software (e.g., HyperTerminal) is operated on the PC to use the serial port. Pins RB0 and RB1 of the microcontroller are the RX and TX pins respectively. The required baud rate is 9600.

Write a program to read data from the terminal, then increase this data by one and send it back to the terminal. For example, if the user enters character “A,” then character “B” should be displayed on the terminal. Assume that a MAX232-type voltage level converter chip is converting the microcontroller signals to RS232 levels. Figure 4.23 shows the circuit diagram of this example.


Figure 4.23: Circuit diagram of Example 4.13

Solution 4.13

The MAX232 chip receives the TX signal from pin RB1 of the microcontroller and converts it to RS232 levels. Comparably, the serial data received by the MAX232 chip is converted into microcontroller voltage levels and then sent to pin RB0. Note that correct operation of the MAX232 chip requires four capacitors to be connected to the chip.

The required program listing is shown in Figure 4.24 (program SERIAL.C). At the beginning of the program, function Soft_Uart_Init is called to configure the serial port. Then an endless loop is formed using a for statement. The Soft_Uart_Read function is called to read a character from the terminal. After reading a character, the data byte is incremented by one and then sent back to the terminal by calling function Soft_Uart_Write.

/********************************************************************
                READING AND WRITING TO SERIAL PORT
                ==================================
In this program PORTB pins RB0 and RB1 are configured as serial RX and
TX pins respectively. The baud rate is set to 9600. A character is received
from a serial terminal, incremented by one and then sent back to the
terminal. Thus, if character "A" is entered on the keyboard, character "B"
will be displayed.
Programmer: Dogan Ibrahim
File:       SERIAL.C
Date:       May, 2007
**********************************************************************/
void main() {
 unsigned char MyError, Temp;
 Soft_Uart_Init(PORTB, 0, 1, 9600, 0); // Configure serial port
 for (;;) // Endless loop
 {
 do {
  Temp = Soft_Uart_Read(MyError);      // Read a byte
 } while (MyError);
 Temp++;                               // Increment byte
 Soft_Uart_Write(Temp);                // Send the byte
}


Figure 4.24: Program listing of Example 4.13

Оглавление книги


Генерация: 0.060. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз