【PIC microcontroller】--Serial port related knowledge

Publisher:数据梦想Latest update time:2022-07-25 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

00 Write in front

This series of articles is from a teacher who has served as a teaching assistant for the PIC microcontroller course. I mainly explained the contents of several experimental classes to my junior students. Here I record some of the knowledge learned in class.


This series of articles mainly introduces:

Experiment 1 - Introduction and Basic I/O - Buttons and LEDs (The first step in learning embedded systems is to turn on a light, just like the first step in learning a programming language is to write a "hello world" code)


Experiment 2 – MPLAB + PICkit 3 + LCD + I/O (This experiment mainly introduces the use of LCD screen, which is often needed for debugging our system. For example, if we make a temperature measurement system, we can use the LCD screen to display the values ​​collected by the sensor, and then use the software to do further processing)


Experiment 3-ADC (This is an analog-to-digital conversion experiment, which is a comprehensive application of the knowledge learned in the previous analog-to-digital electronics)


Experiment 4 – Serial Communication – UART


Experiment 5 – Serial Communication – I2C


Experiment 6 – Timers and interrupts (This part is also a very important knowledge point, which will be used in many embedded systems. For example, a smart car can be built by adding a single-chip microcomputer and a few sensors. The smart car will use timers and interrupts)


01 Serial port related knowledge

Serial communication is also a very important basic knowledge. For example, when you are doing a project, after the front camera collects data and processes the data, you can communicate the result through the serial port and send the result to the microcontroller.


First, let's look at the schematic diagram. Look at the two pins RC6 and RC7. There are TXRX here (these two are the most used functions, which are the asynchronous serial communication interfaces we are going to learn today), and the CKTD at the back (synchronous serial interface, CK is the clock of the two devices, TD is the data line)

insert image description here

02 What is the difference between synchronous communication and asynchronous communication?

Synchronous communication method

A string of characters is used as a transmission unit, and no identification bits are added between characters. A synchronization character is used to indicate the beginning of a string of characters. The hardware requirements are high, and both communicating parties must be strictly synchronized.

insert image description here

Synchronous communication format: data is in "blocks", and a data block includes synchronization characters, data and check characters CRC.

insert image description here

The communication connection usually adopts a three-wire system: SDA (signal line), SCL (clock line), GND (ground line)

The advantage is that the data transmission rate is high, and it is often used as a serial system bus (internal bus), such as I2C, SPI, USB, etc.

The disadvantage is that the hardware requires the sending clock and receiving clock to be strictly synchronized.

insert image description here

Asynchronous communication mode

The transmission unit is characters, and the start bit and stop bit are used to mark the beginning and end of each character. The interval is not fixed, and only synchronization is required when the characters are transmitted.

The asynchronous communication format is as follows

insert image description here

(1) When no data is sent, the data line remains in the "1" state.


(2) When sending data, the start bit "0" is sent first, followed by the data bits. Asynchronous transmission stipulates that the low bit comes first and the high bit comes last.


(3) The parity bit follows the highest bit of the data and occupies one bit (it can also be omitted)


(4) After the data is sent, a stop bit "1" is sent to indicate the end of a frame of data and to prepare for receiving the next frame of data. Before the start bit "0" of the next frame arrives, it is the default "1".


Communication protocol: (1) The communication rate of the sender and receiver must be consistent. (2) The data frame format of the two communicating parties must be consistent.


Advantages: No need to transmit synchronization pulses, high reliability, simple equipment required, suitable for long-distance communication, often used as serial communication bus (external bus), such as RS232, RS485, etc.


Disadvantages: The data contains start and stop bits for synchronization, which reduces the transmission rate of effective data.


03 Asynchronous Communication

In this experiment, we mainly explain asynchronous communication;


Baud rate? Start bit? Stop bit? Parity bit?

Baud rate refers to the speed of serial communication, that is, how many binary bits can be transmitted per second during serial communication. For example, if 9600 binary bits can be transmitted per second (the time required to transmit one binary bit is 1/9600 seconds, that is, 104us), the baud rate is 9600.


Starting position: the first position


End bit: the last bit


Parity bit: If odd parity is specified, that is, if the number of 1s in the first eight data bits is odd, the parity bit is written as 0, if it is even, it is written as 1; that is, the number of the first eight data bits plus the parity bit data 1 is an odd number.


04 Register

We have not used interrupts in this experiment, so the corresponding registers can be ignored.

Next, let's look at the corresponding registers

TXSTA:

insert image description here

Transmit Status and Control Registers

CSRC: Clock source selection, do not care when asynchronous, set to 0;

TX9: 9-bit transmit enable (including parity bit, which is 0 if we don’t have it);

TXEN: Transmit enable terminal, set to 1 when sending.

SYNC: Synchronous and asynchronous selection

BRGH: related to baud rate

TRMT: The status bit of the transmit shift register. When this bit is 1, it means that the data has been sent; when it is 0, it means that the data has not been sent.

TX9D: used for parity check bit, we don't use it, just set it to 0


RCSTA: Receive Status Control Register

insert image description here

SPEN: Serial port enable, when it is 1, the port is open

RX9: This also selects 8 bits, 0

SREN: Enabled when signal is received, ignored when asynchronous, so set to 0

CREN: Continuous reception enable terminal. After we receive a byte, we need to continuously receive the next byte, so it should be set to 1

ADDEN: Address detection enable terminal, this is used when 9-bit data is used, so don’t worry about it, set it to 0

FERR: Parity check error occurs, this bit is set

insert image description here

OERR: Accept overflow set

RX9D: Receive the 9th bit and put it here


Baud Rate Generator:

1. Whether SYNC selects synchronous or asynchronous, and BRGH=0/1, the baud rate calculation formula is different;


9600 baud rate, select high speed, 16 x 9600 = 4000000/(X+1) then X = 25

(Just look at the table in this chip manual!)


Then X is placed in the SPBRG register


05 Notes

There is another point that I need to remind you. When you are doing experiments, RC6/RC7 must be set as input. If you do not set it this way, some problems will occur.


06 bit banging pseudocode

insert image description hereinsert image description here

insert image description here
insert image description here


Reference address:【PIC microcontroller】--Serial port related knowledge

Previous article:【PIC microcontroller】--IIC related knowledge
Next article:【PIC microcontroller】-- ADC related knowledge

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号