Hello everyone, through the previous study, we have learned and become familiar with the usage and learning methods of ICD2 simulation burner and enhanced PIC experiment board. Now we will strike while the iron is hot and take another step forward to learn the working principle and usage of SPI bus. In this way, we can save some data that needs to be saved into the memory chip, and it will not be lost when the power is off. For example, the odometer of a car is constantly counting, and its mileage is placed in a memory chip of the SPI bus.
1. Basic concepts of SPI bus
SPI (Serial Peripheral Interface) bus is a synchronous serial interface technology introduced by Motorola. The SPI bus system is a synchronous serial peripheral interface that allows MCU to communicate and exchange data with various peripheral devices (such as flash memory, digital-to-analog converter, network controller, and slave MCU) in a serial manner.
SPI is a high-speed, full-duplex, synchronous communication bus, and only occupies four wires on the chip pins, saving the chip pins and saving space on the PCB layout, providing convenience. It is precisely because of this simple and easy-to-use feature that more and more chips are now integrated with this communication protocol. There are two working modes: master mode and slave mode. SPI is a protocol that allows a master device to start a synchronous communication with a slave device, thereby completing the data exchange.
That is, SPI is a well-defined communication method. The advantage of this communication method is that it occupies fewer ports, and generally 4 ports are enough for basic communication (not counting the power line). At the same time, the transmission speed is also very high. Generally speaking, the master device is required to have an SPI controller (analog mode can also be used) to communicate with the SPI-based chip.
2. SPI bus system structure
The SPI system can directly interface with a variety of standard peripheral devices produced by various manufacturers. Generally, four lines are used: serial clock line (SCK), host input/slave output data line SDO, host output/slave input data line SDI and low-level effective slave select line SS. SDI and SDO are used to receive and send data serially, with the high bit of data in front and the low bit in the back. When the SPI is set to the master mode, SDO is the master reading the slave, and SDI is the master writing the slave. SCK is used to provide clock pulses to transmit data bit by bit. The block diagram of data transmission between SPI bus devices is shown in Figure 1.
3. Interface characteristics of SPI bus
SPI bus can be used to form various systems under software control. For example, one master MCU and several slave MCUs, several slave MCUs connected to each other to form a multi-host system (distributed system), one master MCU and one or several slave I/O devices. In most applications, one MCU can be used as a master to control data and transmit the data to one or several slave peripheral devices. The slave device can only receive or send data when the host sends a command. The data transmission format is always the high bit (MSB) first and the low bit (LSB) last.
When a master is connected to several different serial I/O chips via SPI, the enable control terminal of each chip must be used, which can be achieved through the I/O port output line of the MCU.
However, special attention should be paid to the input and output characteristics of these serial I/O chips: First, whether the serial data output of the input chip has a three-state control terminal. When the chip is not selected, the output terminal should be in a high-impedance state. If there is no three-state control terminal, a three-state gate should be added. Otherwise, the MISO terminal of the MCU can only connect to one peripheral. Secondly, whether the serial data input of the peripheral has an enable control terminal.
Because only when this chip is allowed, the SCK pulse will move the serial data into the peripheral; when it is prohibited, SCK has no effect on the peripheral. If there is no enable control terminal, add a control gate; of course, you can also connect only one peripheral on the SPI bus, so there is no need for enable control.
Through the introduction of the principle in the previous article, we have a general understanding of the characteristics and working principle of the SPI bus, but when we get a SPI bus device to apply it correctly, we still don’t know how to start. For example, if we want to store a specific data in the device, what should we do? Now, let’s take a look at an example of data storage. Through an example, I believe it will bring you a perceptual understanding. The following is an example of the 93C46 data storage device with SPI interface that is widely used in the current single-chip microcomputer system to introduce the basic application of SPI devices.
4. Introduction to 93C46 Serial Memory
The 93C46 is a 1k-bit serial EEPROM memory.
Each memory can be written or read through the DI/DO pin. Its storage capacity is 1024 bits, and the internal is 128×8 bits or 64×16 bits. 93C46 is a serial three-wire SPI operation chip, which receives instructions from the data port under the synchronization of the clock timing.
The instruction code is a 9-bit decimal code with 7 instructions, read, erase enable, erase, write, full erase, full write and erase prohibit. The chip has fast erase time, erase enable protection, high reliability, and can be erased up to 1 million times. The pin function diagram of 93C46 is shown in Figure 2.
Figure 2 93C46 pin diagram
Table 1: 93C46 Serial EEPROM Instruction Format Selection Table
Instruction Description:
① Read (READ): When the 10XXXXXX instruction is issued, the data at address (XXXXXXXX) is output by DO when SCK=1.
② Write (WRITE): Before writing data, you must first issue the write enable (EWEN) command, and then issue the 01XXXXXX command. When SCK=1, the data code will be written to the specified address (XXXXXXXX); and when DO=0, it means that the write operation is still in progress. After the write is completed, DO will turn to a high level. After the write operation is completed, the write disable (EWDS) command must be issued again.
③ Erase (ERASE): After issuing the erase command 11XXXXXX, the data at address (XXXXXXXX) will be cleared.
④ Write enable (EWEN): After issuing the 0011XXXX instruction, the write (WRITE) operation can be performed.
⑤ Write disable (EWDS): After issuing the 0000XXXX instruction, the write (WRITE) operation can be repeated.
⑥ Chip clear (ERAL): After issuing the 0010XXXX instruction, all are prohibited.
⑦ Chip write (WRAL): After issuing the 0001XXXX instruction, all "0" are written.
The function of our example program is to implement the read and write operations of the 93C46 memory and verify whether the data is correct. The program first writes 0x55 and 0xAA to the two addresses 0x02 and 0x03 respectively, then reads one of the addresses and displays the read data to verify whether it is correct. The program defaults to reading the data in the address 0x02, and the reader can also modify the address data to read other address data.
First, let's take a look at the 93C46 interface circuit on the enhanced PIC experiment board, because we need to combine software and hardware to consider how to program. The hardware schematic diagram for completing the experiment is shown in Figure 3. U6 is the 93C46 chip on the experiment board. The RB1, RB2, RB4, and RB5 of the microcontroller are respectively connected to the CS, CLK, DI, and DO pins of the chip. The seven-segment digital tubes D5, D7, and D8 form the display unit. The data of the glyph code is sent in through the RC port, and each digital tube scans and displays signals controlled by different RA ports.
Figure 3 Hardware schematic diagram
For the programming of the microcontroller software, we use MPLabIDE software to program in C language. It is our programming environment. At the same time, we can use ICD2 simulation burner and enhanced PIC experimental board to connect to perform simulation debugging and burning steps of the program. We have made detailed descriptions and introductions of the specific operation steps in previous issues. We will not repeat them here. Readers can refer to previous articles or log in to our website to view the information. Now we can enter the program code for debugging. We create a new project in MPLab IDE software, add source code, and select the chip model and set the configuration bit. The chip model used in our experiment is PIC16F877A. Due to space limitations, please download the source code of the program from the website www.ele169.com or www.hificat.com. The flow chart is shown in Figure 4.
Figure 4 Flowchart of the read and write demonstration program
After the program is compiled, the compiled HEX is burned into the MCU chip through the ICD2 emulation burner, and the power is turned on and the effect is shown in Figure 5. "0170" is the decimal value we wrote into the memory chip in advance and then read it out.
Figure 5 The data read from 93C46 is displayed on the digital tube
After reading this, I believe you can now complete some experiments in data storage.
Previous article:PIC microcontroller digital tube
Next article:PIC microcontroller RS232 serial communication
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Canaan's K510 development board is now on the forum. Let's warm it up first and post it later.
- Last week: 100 sets of Pingtouge RISC-V development boards, invite you to play ~ Come quickly ~ Share with friends and win red envelopes
- HP laser printer original toner cartridges are useless if you don't open them after using them up.
- Problems caused by macros that IAR cannot find defaulting to 0
- help
- FPGA technology introduction and FPGA application fields
- Learn MSP430F5529 programming routines
- Diode as a temperature compensation circuit for transistors
- TL431 as a voltage regulator
- Is the product on Taobao claiming to be an energy saver genuine?