Design of SPI interface based on PIC microcontroller and serial flash memory

Publisher:chenxiaohong68Latest update time:2011-11-26 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Design of SPI interface based on PIC microcontroller and serial flash memory

Previous article:Application of PIC16C72A in Intelligent Instruments of Automobile Electronics
Next article:Design of digital virtual surround sound controller based on PIC microcontroller

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号