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)
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.
Synchronous communication format: data is in "blocks", and a data block includes synchronization characters, data and check characters CRC.
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.
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
(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:
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
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
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
Previous article:【PIC microcontroller】--IIC related knowledge
Next article:【PIC microcontroller】-- ADC related knowledge
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Some learning experiences of NBIoT
- mPython 0.3.7 released
- PCB Design Analysis of Switching Power Supply and IC Controller
- Some explanations on LF2407A FLASH programming issues
- [GigaDevice GD32F310 Review] + Sensor Detection
- Verilog a large number of routines
- Rural hukou is more valuable than urban hukou
- Legendary figures in the field of radio frequency and microwaves and their outstanding contributions
- EEWORLD University Hall ---- Digital Signal Processing Beihang Wang Jun
- Can the circuit oscillation problem be solved by using only a 100Ω resistor?