Interfacing Spi™ Serial Eeproms To Microchip Picmicro Microcontrollers
Interfacing Spi™ Serial Eeproms To Microchip Picmicro Microcontrollers
Interfacing Spi™ Serial Eeproms To Microchip Picmicro Microcontrollers
INTRODUCTION
There are many different microcontrollers on the market today that are being used in embedded control applications. Many of these embedded control systems need nonvolatile memory. Because of their small footprint, byte level flexibility, low I/O pin requirement, low power consumption, and low cost, serial EEPROMs are a popular choice for nonvolatile storage.
FIGURE 1:
PIC16F877
Vcc
CS SO WP Vss
1 2 3 4 25XXXXX
8 7 6 5
Note:
CS, WP and HOLD pins should all have pull-up resistors (~10k ohms).
DS00966A-page 1
AN966
FIRMWARE DESCRIPTION
The program shows individual features of the SPI protocol and gives code samples of opcodes so that basic building blocks of a system can be put together for rapid deployment of electronics using SPI EEPROMs. The opcodes used in the program are Write Enable (WREN), Write, Read, Read Status Register (RDSR) (used in the program for WIP (Writein-Process) polling), and Write Disable (WRDI). The oscilloscope pictures have markers that are shown from CS enable to CS disable for ease in reading. The data sheet version of the waveform is below the actual oscilloscope picture. The MSSP module is set up for Mode 1,1 operation at approximately 625 kHz. The code is written in modules and commented so changing modes, speeds, and modifying commands, such as sequential reads and page writes, are simple. The values represented in this application note are all hex values.
WRITE ENABLE
Figure 2 shows an example of the Write Enable command. Chip Select is brought low (active) and the opcode is sent out through the MSSP port. The Write Enable command must be given before a write is attempted to either the array or the Status Register. The WEL bit can be cleared by issuing a Write Disable command (WRDI), or it is automatically reset if the device is powered down or a write cycle is completed. Instances of this command can be found in the firmware by searching for WREN.
FIGURE 2:
CS 0 SCK 1 2 3 4 5 6 7
SI
SO
high-impedance
DS00966A-page 2
AN966
READ STATUS REGISTER TO CHECK FOR WEL BIT
Figure 3 shows an example of the Read Status Register command to check for the Write Enable Latch (WEL) bit. The WEL bit must be set before a write is attempted to either the Status Register or the array. It is good programming practice to check for the bit to be set before attempting the write. Once again the device is selected using CS and the opcode 0x05 is received. Although it appears that the opcode is received twice, the second 8 bits are dont cares on the Data In pin and act as a dummy byte just to provide clock signals so that the Status Register can be shifted out on the Data Out pin. A value of 0x02 shows that the WEL bit in the Status Register has been set. The device is now ready to do a write on either the Status Register or the array.
FIGURE 3:
high-impedance SO
DS00966A-page 3
AN966
WRITE COMMAND (OPCODE, ADDRESS AND DATA)
Figure 4 shows an example of the Write command. For this waveform the device is selected and the opcode 0x02 is received. The High Address byte receives 0x00 followed by the Low Address byte 0x55. Finally, the data is clocked in which is 0xAA. Once the Chip Select is toggled at the end of this command, the internal write cycle is initiated. After the internal write cycle has begun, the WIP bit in the Status Register can now be polled to check when the write finishes, or a delay needs to be added to the microcontroller firmware (~5ms) if the WIP bit is not being polled. This code uses WIP polling.
FIGURE 4:
Twc
high-impedance SO
DS00966A-page 4
AN966
DATA POLLING (RDSR CHECK FOR WIP SET)
After a valid Write command is given, the Status Register can be read to check if the internal write cycle has been initiated. It can also be monitored continuously to look for the end of the write cycle. In this case, the device is selected and the opcode 0x05 is received. The Status Register contents are then shifted out on the Data Out pin, resulting in a value of 0x03. Figure 5 shows that both the WEL bit (bit 1) and the WIP bit (bit 0) are set (0x03), meaning the write cycle is in progress.
FIGURE 5:
high-impedance SO
DS00966A-page 5
AN966
DATA POLLING FINISHED (RDSR WIP BIT CLEARED)
The code was written to stay in a continuous loop and evaluate the status Register until the WIP bit is cleared. Figure 6 shows the Status Register Read command followed by a value of 0x00 being shifted out on the Data Out pin. This indicates that the write cycle has finished and the EEPROM is now ready for additional commands. The WEL bit is also cleared at the end of a write cycle. This serves as additional protection against unwanted writes.
FIGURE 6:
high-impedance SO
DS00966A-page 6
AN966
WRITE DISABLE COMMAND
Previously, we showed a Status Register Read to illustrate the WEL bit being set. Figure 7 shows the WEL being set followed by the WRDI command. The Status Register is then read and indicates that the WEL bit has been cleared and the device will no longer accept Write commands to either the array or the Status Register.
FIGURE 7:
RDSR FOR WEL SET AND THEN CLEARED AFTER WRDI COMMAND
CS 0 SCK 1 2 3 4 5 6 7
SI
0 1
high-impedance SO
DS00966A-page 7
AN966
CONCLUSION
This is an application note of the basic features of SPI communications using the MSSP module on one of Microchips mid-range devices. The code is highly portable and can be used on many devices that have the MSSP module, possibly with very minor modifications. Using the code provided, designers can begin to build their own SPI libraries to be as simple or complex as needed. The code was tested on Microchips PICDEM 2 Plus Demonstration Board with the connections shown in Figure 1.
DS00966A-page 8
Note the following details of the code protection feature on Microchip devices: Microchip products meet the specification contained in their particular Microchip Data Sheet. Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions. There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property. Microchip is willing to work with the customer who is concerned about the integrity of their code. Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as unbreakable.
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our products. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchips products as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.
Trademarks The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KEELOQ, microID, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart, rfPIC, and SmartShunt are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB, PICMASTER, SEEVAL, SmartSensor and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Analog-for-the-Digital Age, Application Maestro, dsPICDEM, dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo, PowerMate, PowerTool, rfLAB, rfPICDEM, Select Mode, Smart Serial, SmartTel and Total Endurance are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. All other trademarks mentioned herein are property of their respective companies. 2004, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper.
Microchip received ISO/TS-16949:2002 quality system certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona and Mountain View, California in October 2003. The Companys quality system processes and procedures are for its PICmicro 8-bit MCUs, KEELOQ code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchips quality system for the design and manufacture of development systems is ISO 9001:2000 certified.
DS00966A-page 9
ASIA/PACIFIC
Australia - Sydney Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 China - Beijing Tel: 86-10-8528-2100 Fax: 86-10-8528-2104 China - Chengdu Tel: 86-28-8676-6200 Fax: 86-28-8676-6599 China - Fuzhou Tel: 86-591-8750-3506 Fax: 86-591-8750-3521 China - Hong Kong SAR Tel: 852-2401-1200 Fax: 852-2401-3431 China - Shanghai Tel: 86-21-5407-5533 Fax: 86-21-5407-5066 China - Shenyang Tel: 86-24-2334-2829 Fax: 86-24-2334-2393 China - Shenzhen Tel: 86-755-8203-2660 Fax: 86-755-8203-1760 China - Shunde Tel: 86-757-2839-5507 Fax: 86-757-2839-5571 China - Qingdao Tel: 86-532-502-7355 Fax: 86-532-502-7205
ASIA/PACIFIC
India - Bangalore Tel: 91-80-2229-0061 Fax: 91-80-2229-0062 India - New Delhi Tel: 91-11-5160-8631 Fax: 91-11-5160-8632 Japan - Kanagawa Tel: 81-45-471- 6166 Fax: 81-45-471-6122 Korea - Seoul Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 Singapore Tel: 65-6334-8870 Fax: 65-6334-8850 Taiwan - Kaohsiung Tel: 886-7-536-4818 Fax: 886-7-536-4803 Taiwan - Taipei Tel: 886-2-2500-6610 Fax: 886-2-2508-0102 Taiwan - Hsinchu Tel: 886-3-572-9526 Fax: 886-3-572-6459
EUROPE
Austria - Weis Tel: 43-7242-2244-399 Fax: 43-7242-2244-393 Denmark - Ballerup Tel: 45-4450-2828 Fax: 45-4485-2829 France - Massy Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany - Ismaning Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 England - Berkshire Tel: 44-118-921-5869 Fax: 44-118-921-5820
10/20/04
DS00966A-page 10