Introduction
PIC microcontrollers are widely used in industrial control, instrumentation, home appliances, communications and other fields due to their stable performance and wide variety. Although many models have integrated memory, in many cases it is difficult to meet the system's requirements for large-capacity storage, and an external non-volatile memory is required. Compared with parallel Flash memory, serial Flash memory occupies fewer MCU pins, is small in size, easy to expand, has simple wiring, and works reliably, so it is increasingly used in various electronic products and industrial measurement and control systems. This article mainly discusses the SPI communication between the PIC16F877A microcontroller and the serial flash memory M25P16, which has practical value when large-capacity data storage is required and MCU pin resources are limited.
1 SPI working principle
SPI (Serial Peripheral Interface) is a commonly used serial communication protocol used for communication between MCU systems and peripheral devices. It can be used to connect memory, A/D converters, D/A converters, real-time clocks, LCD drivers, sensors, and even other processors. SPI mainly uses four signals: MOSI (master output/slave input), MISO (master input/slave output), SCK (serial clock) and CS (chip select). Among them, SCK is generated by the host as the synchronization clock of the transmission, controlling all data transmission. The host determines whether the SPI transmission between the two can be carried out by triggering the CS of the slave device. Both the host and the peripheral contain a serial shift register. The host initiates a transmission by writing a byte to its own SPI serial register, and then transmits the data to the peripheral through the MOSI signal line. At the same time, the peripheral returns the contents of its shift register to the host through the MISO signal line, as shown in Figure 1. In this way, the contents of the two shift registers are exchanged. In other words, the write and read operations of the peripheral are completed synchronously. In actual applications, if only write operations are performed, the host only needs to ignore the received bytes; if the host wants to read the data of the peripheral, it must send a byte to trigger the transmission of the slave, and the byte sent can be any data.
2 Introduction to M25P16
M25P16 is a 16 Mb serial flash memory with an advanced write protection mechanism that supports access operations of the SPI bus at speeds up to 50 MHz. The memory has 32 sectors, each with 256 pages and 256 bytes per page. The operating voltage range is 2.7 to 3.6 V, and the operating temperature range is -40 to +85°C. Data can be stored for up to 20 years, and each sector can be erased/programmed 100,000 times.
M25P16 supports a total of 12 operating instructions. The instruction format is:
Among them, the 8-bit command word is required, and the presence and length of the address, dummy and data bytes will vary depending on the instruction. The details are listed in Table 1. All command codes, addresses, and serial input/output data are high-order first and low-order last.
When operating the M25P16, first select the chip (that is, the chip select signal S is pulled low), then serially input the operation instruction byte, followed by the serial input address byte (0 or 3 bytes), and if necessary, add a dummy read byte, and finally serially input/output the data byte, then pull the chip select signal high, and then the M25P16 starts the internal control logic and completes the corresponding operation by itself. [page]
3 SPI Hardware Design
The PIC16F877A microcontroller has a very complete SPI interface (RC3/SCK, RC4/SDI, RC5/SDO, RA5/SS). Only when the PIC16F877A is a slave, the RA5/SS pin is used as a SPI pin. When the PIC16F877A is a master, the SS can be used as a common I/O. Through this interface, the communication between the PIC16F877A and the SPI Flash can be realized relatively easily. The hardware interface between the PIC16F877A and the M25P16 is shown in Figure 2. Among them, SCK, SDI, and SDO are the SPI dedicated pins of the MCU, which are connected to the corresponding pins of the memory respectively. Any I/O pin of the MCU can be selected as the chip select signal of the memory. In the figure, the RC2 pin is selected to be connected to the chip select S of the memory. In this way, only the C port of the MCU is involved in the SPI communication, which is convenient for operation. The HOLD and W of the M25P16 are directly connected to the high level, indicating that the SPI communication is not allowed to be suspended when S is valid and the entire storage area is not write-protected.
In Figure 2, VDD is +5 V. Since PIC16F877A works at 5 V, and the operating voltage range of M25P16 is 2.7-3.6 V, the two cannot be directly connected. Here, the resistor voltage division method is used to ensure that the voltage of the S, C, and D pins of M25P16 is within the range that the memory can withstand and recognize. By adding a pull-up resistor to the SDO pin of M25P16 to input data to PIC16F877A, the MCU can recognize the high voltage output by M25P16, thereby ensuring normal SPI communication. If the MCU works at 3.3 V, the corresponding pins of the two can be directly connected.
4 SPI software design
On the basis of correct hardware connection, in order to carry out SPI communication, a driver program must be written for M25P16, including SPI initialization, reading M25P16 data, writing data to M25P16, erasing data, etc. Here, C language programming is used, the compiler selects PICC, and the development environment is MPLAB IDE8.10.
PIC16F877A's SPI communication involves four registers: control register SSPCON, status register SSPSTAT, serial receive/transmit buffer SSPBUF, and shift register SSPSR. Among them, the 8 bits of SSPCON are all readable and writable, used to set the SSP in master/slave mode, clock frequency, clock polarity, SSP enable, and write conflict detection; only the upper 2 bits of SSPSTAT are readable and writable, and the lower 6 bits are read-only. When PIC16F877A is in receive mode, SSPSR and SSPBUF form a 2-level buffered receiver. Every time SSPSR receives a complete byte, it transfers the byte to SSPBUF and sets the interrupt flag SSPIF to 1. Data can be obtained by reading SSPBUF; when 877A is in transmit mode, writing SSPBUF will write data to SSPSR at the same time, triggering transmission. The following is a detailed explanation combined with specific code.
(1) SPI initialization and read/write functions
From the code of the SendByte and RcvByte functions, we can see that the completion of data transmission and reception is achieved by judging the STAT_BF flag (the BF bit of the SSPSTAT register, STAT_BF is the name defined in the header file pic1687x.h), while the description of the BF bit in the data sheet is only used for the receiving mode. This is because the PIC16F877A reads data through SDI while sending data through SDO. When 1 byte is sent, 1 byte is received into SSPBUF. At this time, SSPBUF is full and BY is set to 1. Therefore, the STAT_BF flag can be used to judge whether 1 byte is sent.
(2) Continuous write function
The PP instruction of M25P16 allows continuous writing of no more than 1 page (256 bytes) of data at a time. Before writing data, a write enable command must be issued first, and then the data write operation can be performed. The parameters of the data write function include address (32-bit address), block (write data buffer pointer), and n (the number of bytes to be written continuously at a time, n<256). If the lower 8 bits of address are not all 0, that is, it is not written from the beginning of the page, and the data to be written exceeds the remaining space of the page, the excess part will be discarded. The code is as follows: [page]
Among them, delay() is a delay sub-function, and the parameter is in ms level. Delay(1) means a delay of 1ms. The delay is added to ensure that the memory is ready for reading and writing operations.
(3) Continuous read function
M25P16 allows continuous reading of data after issuing a read instruction. This mode greatly improves bus efficiency. The parameters of the data read function include address: 32-bit address; block: read data buffer pointer; n: the number of bytes read continuously at a time. The code is as follows:
The difference between the continuous read operation of M25P16 and the continuous write operation is that, whether READ or FAST_READ, after reading 1 byte of data at the starting address, it will automatically address the data at a higher address, so there is no need for address++ statement in the program. In addition to initializing
, reading and writing M25P16, it is often necessary to erase it. There are two ways to erase: sector erase and overall erase. Executing data erase will change all internal data to FFH. The erase operation is similar to the write operation and will not be repeated here.
Conclusion
The interface between M25P16 and PIC16F877A introduced in this article has been applied to the local storage of tap water flow data collection. The operation is stable and reliable, and no data loss is found. It has certain reference value for other applications.
Previous article:Application of PIC16C72A in Intelligent Instruments of Automobile Electronics
Next article:Design of digital virtual surround sound controller based on PIC microcontroller
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- 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!
- Share: DSP2833x Programming Summary
- MSP430 MCU Development Record (24)
- Can this blue potentiometer be used for the load of the switching power supply?
- Body temperature detection and status call monitoring system
- 【EasyARM-RT1052 Review】+ Use of GPIO and external interrupts
- RS485 pull-up and pull-down matching resistor problem
- EEWORLD University Hall ---- Summary of the overall design of software and hardware of Gizwits experts' intelligent high-precision industrial instruments
- Is the FPGA or CPLD with 5V IO output?
- stm32 uses sql database function at the bottom layer
- How to deal with severe overheating of the adjustable DCDC circuit?