Freescale 16-bit MCU (Part 7) - SCI serial port test

Publisher:Huayu8888Latest update time:2021-03-22 Source: eefocusKeywords:Freescale Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction to SCI Serial Port

       With the widespread application of single-chip microcomputer systems and the popularization of computer network technology, the communication function of single-chip microcomputers has become increasingly important. Single-chip microcomputer communication refers to the information exchange between single-chip microcomputers and computers or between single-chip microcomputers. There are two communication methods: parallel and serial. Since the interface resources of single-chip microcomputers are relatively precious, the use of parallel port communication will occupy more interface resources, so the exchange of information mostly adopts serial communication.


(1) Parallel communication mode


       Parallel communication usually uses multiple data lines to transmit each bit of a data byte at the same time. Each bit of data requires a data line. For an 8-bit data bus communication system, 8 data lines will be required to transmit 8 bits of data (one byte) at a time. In addition, a signal line and several control signal lines are required. This method is only suitable for short-distance data transmission. For example, older printers are connected to computers via parallel ports, but now they use USB interfaces with very fast transmission speeds. Parallel port communication is simple to control and has a relatively fast transmission speed, but due to the large number of transmission lines, the cost is high for long-distance transmission and it is difficult for the sender and receiver to receive each bit at the same time.


(2) Serial communication method


       Serial communication is to divide the data bytes into bits and transmit them one by one on a transmission line. In this case, only one data line is needed, and at least 8 bits are needed to complete the transmission. The necessary process of serial communication is: when sending, the parallel data must be converted into serial data and sent to the line, and when receiving, the serial signal must be converted back into parallel data so that it can be processed by computers and other devices. Serial communication has fewer transmission lines, low cost for long-distance transmission, and can use existing equipment such as telephone networks, but the data transmission control is more complicated than parallel communication.


       There are two types of serial communication: asynchronous serial communication and synchronous serial communication.


(3) Asynchronous serial communication method


       Asynchronous serial communication refers to the process in which the sending and receiving devices of the communication use their own clocks to control the sending and receiving of data. In order to coordinate the sending and receiving of both parties, the clocks of the sending and receiving devices are required to be as consistent as possible. Asynchronous communication is transmitted in units of characters (frames), and the gaps (time gaps) between characters are arbitrary, but each bit in each character is transmitted at a fixed time, that is, there is not necessarily an integer multiple relationship of "bit interval" between characters, but the distance between bits in the same character is an integer multiple of the "bit interval".


       The SCI module of Freescale's microcontroller uses asynchronous serial communication. The so-called SCI is the name given by Freescale itself. It has the same function as the UART serial port of other microcontrollers.


        A frame of character information in SCI serial communication consists of 4 parts: start bit, data bit, parity bit and stop bit, as shown in the figure below. SCI data format uses 8-bit data format and 9-bit data format.

       Characteristics of asynchronous communication: It does not require strict consistency of the clocks of both the sender and the receiver, is easy to implement, and has low equipment overhead, but each character must be added with 2 to 3 bits for the start bit, check bit, and stop bit, so the transmission efficiency is not high. When communicating between single-chip microcomputers and between single-chip microcomputers and computers, asynchronous serial communication is usually used.


(4) Synchronous serial communication mode


        In synchronous communication, the sender's clock directly controls the receiver's clock so that both parties are completely synchronized. At this time, the distance between the bits of the transmitted data is an integer multiple of the "bit interval", and there is no gap between the transmitted characters, that is, the bit synchronization relationship is maintained, and the character synchronization relationship is also maintained. We will introduce the synchronous serial communication method in detail in future experiments.


(5) Serial communication standard


Simplex. Simplex means that data can only be transmitted in one direction and cannot be transmitted in the reverse direction.

Half-duplex. Half-duplex means that data transmission can be in both directions, but it needs to be done in time.

Full-duplex. Full-duplex means that data can be transmitted in both directions at the same time.

Error checking for serial communications

①Parity check

When sending data, the 1 bit following the data bit is the parity bit (1 or 0). When odd parity is used, the sum of the number of 1s in the data and the number of parity bits 1 should be an odd number; when even parity is used, the sum of the number of 1s in the data and the number of parity bits 1 should be an even number. When receiving characters, the number of 1s is checked. If it is inconsistent, it means that an error has occurred during the data transmission process.

②Code and verification

The code and checksum is that the sender sums the data block (or XORs each byte) to generate a one-byte checksum character (checksum) and appends it to the end of the data block. When receiving data, the receiver sums the data block (except the check byte) (or XORs each byte) at the same time, and compares the result with the sender's "checksum". If they match, there is an error. Otherwise, it is considered that an error occurred during the transmission process.

③Cyclic redundancy check

This type of verification is to realize the cyclic verification between valid information and check bits through some mathematical operations, which is often used for the transmission of disk information, integrity verification of storage areas, etc. This verification method has strong error correction capability and is widely used in synchronous communication.


        In order for the SCI serial port of the microcontroller to communicate with the serial port of the computer, the level conversion must also be performed. The level standard of the XEP100 microcontroller is TTL level, 5V represents logic "1", 0V represents logic "0"; the serial port level of the computer is RS-232 level, -12V represents logic "1", 12V represents logic "0". In order for the computer to receive data from the microcontroller, the TTL level must be converted to RS-232 level; in order for the microcontroller to receive data from the computer, the RS-232 level must be converted to TTL level. The chips that realize these two level conversions mainly include MAX232, MAX202, HIN232, etc. In this experiment, MAX232 is used to realize the interchange of the levels of the two serial ports. The circuit schematic is shown in the figure below.

       The SCI module of Freescale microcontrollers allows full-duplex, asynchronous, serial communication between the microcontroller and remote devices (including other microcontrollers). The SCI consists of a baud rate generator, a transmitter, and a receiver. The transmitter and receiver operate independently, although they use the same baud rate generator. During normal operation, the microcontroller monitors the status of the SCI, writes data to be sent, and processes received data.


       The functional block diagram of the SCI module of the XEP100 microcontroller is shown in the figure below

2. Routine Test

       In this experiment, we use the SCI serial port of the microcontroller to communicate with the computer. You need to use a serial port to USB cable, which will simulate a serial port on the computer's USB port. In addition, you need a serial port assistant software sscom, the interface is shown in the figure below

       Now that we have prepared what we need, let's take a look at the code. The main code related to SCI is the SCI initialization, sending, and receiving functions, as shown below.


/*************************************************************/

/* Initialize SCI */

/*************************************************************/

void INIT_SCI(void) 

{

  SCI0BD = BUS_CLOCK/16/BAUD; //Set the SCI0 baud rate to 9600

  SCI0CR1 = 0x00; //Set SCI0 to normal mode, eight data bits, no parity check

  SCI0CR2 = 0x2c; // Allow receiving and sending data, and allow receiving interrupt function 

}

 

/*************************************************************/

/*Serial port sending function */

/*************************************************************/

void SCI_send(unsigned char data) 

{

  while(!SCI0SR1_TDRE); //Wait for the send data register (buffer) to be empty

  SCI0DRL = data;

}

 

/*************************************************************/

/* Serial port receiving function */

/*************************************************************/

unsigned char SCI_receive(void) 

{

  while(!SCI0SR1_RDRF); //Wait for the send data register to be full

  return(SCI0DRL);

}


       In the initialization code, first set the baud rate of the SCI serial port to 9600 according to the bus clock, then set the SCI to normal mode, 8 data bits, no parity bit, and finally set the SCI0CR2 register to allow reception and transmission, and enable the receive interrupt. In the SCI send function, first determine whether the send buffer is empty. If it is empty, assign the data to be sent to the SCI data register. In the SCI receive function, wait for the receive flag to be set to 1. If it is set to 1, it means that the data has been read, and then return the data.

[1] [2]
Keywords:Freescale Reference address:Freescale 16-bit MCU (Part 7) - SCI serial port test

Previous article:Freescale 16-bit MCU (VIII) - PWM module test
Next article:Freescale 16-bit MCU (VI) - Phase-locked loop test

Recommended ReadingLatest update time:2024-11-23 15:10

Freescale MC9S08AW60 Assembly Learning Notes (V)
We have learned the function of table lookup before. We can use the table lookup function to implement many powerful functions, such as looking up cube tables, square tables, function tables, segment code tables displayed by digital tubes, etc. Here is another powerful function that is realized by using table lookup a
[Microcontroller]
Freescale's ECT method for detecting speed
Resources: 1. Incremental rotary encoder. A rotary encoder is a device for measuring speed. Every time the rotary encoder rotates one circle, it will output a specific number of pulses. By recording the number of pulses output by the rotary encoder in a unit time, you can know the number of revolutions of the rotary
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号