Thursday, May 09, 2013
Microchip Technology SPI Master EEPROM 25LC256 program example
SPI Master EEPROM by Microchip Technology - June 21, 2012
http://embeddedcodesource.com/developer/microchip_technology_59/spi_master_eeprom
In this code example, we make PIC24FJ128GA010 as an SPI Master to write and read 25LC256 SPI EEPROM on Explorer 16 Development Board.
About this Code Example
In this code example, we make PIC24FJ128GA010 as an SPI Master to write and read 25LC256 SPI EEPROM on Explorer 16 Development Board.
The code does the following operations on 25LC256,
(1) write two bytes to EEPROM;
(2) read two bytes from EEPROM;
Release History SPI Master EEPROM (current version) June 21, 2012 First Release to ECS
Current Version: 1.0.0 Updated: Jun 21, 2012 Downloads: 775 Rating: 0/5 (0 votes cast) Status: Released
Readme File for Code Example:
SPI Master EEPROM
---------------------------------------------------------------
This file contains the following sections:
1. Code Example Description
2. Folder Contents
3. Suggested Development Resources
4. Reconfiguring the project for a different PIC24F device
5. Revision History
1. Code Example Description:
----------------------------
In this code example, we make PIC24FJ128GA010 as an SPI Master to write and read 25LC256 SPI EEPROM on Explorer 16 Development Board.
The code does the following operations on 25LC256,
(1) write two bytes to EEPROM;
(2) read two bytes from EEPROM;
(3) compare the bytes written into and read out.
2. Folder Contents:
-------------------
This folder contains the following sub-folders:
a. C:\Program Files\Microchip\MPLAB C30\support\gld
This folder will have the device GLD file, it is used for building the project.
This file was provided with the MPLAB?C30 toolsuite.
b. C:\Program Files\Microchip\MPLAB C30\support\h
This folder contains C header files useful in building this
project. Device register and bit definitions are provided in
the *.h file that follows the device name. These files were provided
with the MPLAB?C30 toolsuite.
c. C:\Program Files\Microchip\MPLAB C30\lib
This folder contains library archive files, which are a
collection of precompiled object files. The file
named "libpic30-coff.a" contains the C run-time start-up
library. These file were provided with the
MPLAB?C30 toolsuite.
d. hex
This folder contains three file types - coff, hex and map.
These are files generated by the MPLAB?C30 toolsuite on build
operation performed within MPLAB?IDE. The *.map file contains
details on memory allocation for various variables, constants
and dsPIC instructions specified in the source and library
code. The *.hex file contains a binary file that may be
programmed into the dsPIC device. The *.coff file contains
a binary file that is used by MPLAB?IDE for simulation.
e. h
This folder contains include files for the code example.
f. src
This folder contains all the C and Assembler source files (*.c,
*.s) used in demonstrating the described example. This folder
also contains a sub-folder named "obj" that stores compiled
object files generated when the project is built.
3. Suggested Development Resources:
-----------------------------------
a. Explorer 16 Development board with PIC24FJ128GA010 controller
4. Reconfiguring the project for a different PIC24F device:
-------------------------------------------------------------
The Project/Workspace can be easily reconfigured for any PIC24F device.
Please use the following general guidelines:
a. Change device selection within MPLAB?IDE to a PIC24F device of
your choice by using the following menu option:
MPLAB IDE>>Configure>>Select Device
b. Re-build the MPLAB?project using the menu option:
MPLAB IDE>>Project>>Build All
c. Download the hex file into the device and run.
5. Revision History :
---------------------
06/20/2008 - Initial Release of the Code Example
.END
/**********************************************************************
* ?2007 Microchip Technology Inc.
*
* FileName: spieeprom.h
* Dependencies: Header (.h) files if applicable, see below
* Processor: PIC24Fxxxx
* Compiler: MPLAB?C30 v3.00 or higher
*
* SOFTWARE LICENSE AGREEMENT:
* Microchip Technology Incorporated ("Microchip") retains all
* ownership and intellectual property rights in the code accompanying
* this message and in all derivatives hereto. You may use this code,
* and any derivatives created by any person or entity by or on your
* behalf, exclusively with Microchip's proprietary products. Your
* acceptance and/or use of this code constitutes agreement to the
* terms and conditions of this notice.
*
* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS". NO
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
* NOT LIMITED TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS
* CODE, ITS INTERACTION WITH MICROCHIP'S PRODUCTS, COMBINATION WITH
* ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
*
* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE
* LIABLE, WHETHER IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR
* BREACH OF STATUTORY DUTY), STRICT LIABILITY, INDEMNITY,
* CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR
* EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER
* CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE
* DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWABLE BY LAW,
* MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS
* CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP
* SPECIFICALLY TO HAVE THIS CODE DEVELOPED.
*
* You agree that you are solely responsible for testing the code and
* determining its suitability. Microchip has no obligation to modify,
* test, certify, or support the code.
*
* REVISION HISTORY:
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Author Date Comments on this revision
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Albert Z. 05/16/08 First release of source file
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 25LC256 is connected to SPI2
************************************************************************/
#include "p24fxxxx.h"
// peripheral configurations
#define SPI_MASTER 0x0120 // select 8-bit master mode, CKE=1, CKP=0
#define SPI_ENABLE 0x8000 // enable SPI port, clear status
/************************************************************************
* EEPROM Commands *
* *
************************************************************************/
#define EEPROM_PAGE_SIZE (unsigned)64
#define EEPROM_PAGE_MASK (unsigned)0x003f
#define EEPROM_CMD_READ (unsigned)0b00000011
#define EEPROM_CMD_WRITE (unsigned)0b00000010
#define EEPROM_CMD_WRDI (unsigned)0b00000100
#define EEPROM_CMD_WREN (unsigned)0b00000110
#define EEPROM_CMD_RDSR (unsigned)0b00000101
#define EEPROM_CMD_WRSR (unsigned)0b00000001
/************************************************************************
* Aliases for IOs registers related to SPI connected to EEPROM *
* *
************************************************************************/
#define EEPROM_SS_TRIS TRISDbits.TRISD12
#define EEPROM_SS_PORT PORTDbits.RD12
#define EEPROM_SCK_TRIS TRISGbits.TRISG6
#define EEPROM_SDO_TRIS TRISGbits.TRISG8
#define EEPROM_SDI_TRIS TRISGbits.TRISG7
/************************************************************************
* Structure STATREG and union _EEPROMStatus_ *
* *
* Overview: Provide a bits and byte access to EEPROM status value. *
* *
************************************************************************/
struct STATREG{
unsigned WIP:1;
unsigned WEL:1;
unsigned BP0:1;
unsigned BP1:1;
unsigned RESERVED:3;
unsigned WPEN:1;
};
union _EEPROMStatus_{
struct STATREG Bits;
unsigned char Char;
};
/************************************************************************
* Macro: Lo *
* *
* Preconditions: None *
* *
* Overview: This macro extracts a low byte from a 2 byte word. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
#define Lo(X) (unsigned char)(X&0x00ff)
/************************************************************************
* Macro: Hi *
* *
* Preconditions: None *
* *
* Overview: This macro extracts a high byte from a 2 byte word. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
#define Hi(X) (unsigned char)((X>>8)&0x00ff)
/************************************************************************
* Macro: mEEPROMSSLow *
* *
* Preconditions: SS IO must be configured as output. *
* *
* Overview: This macro pulls down SS line *
* to start a new EEPROM operation. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
#define mEEPROMSSLow() EEPROM_SS_PORT = 0;
/************************************************************************
* Macro: mEEPROMSSHigh *
* *
* Preconditions: SS IO must be configured as output. *
* *
* Overview: This macro set SS line to high level *
* to start a new EEPROM operation. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
#define mEEPROMSSHigh() EEPROM_SS_PORT = 1;
/************************************************************************
* Function: EEPROMInit *
* *
* Preconditions: SPI module must be configured to operate with *
* parameters: Master, MODE16 = 0, CKP = 1, SMP = 1. *
* *
* Overview: This function setup SPI IOs connected to EEPROM. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
extern void EEPROMInit(void);
/************************************************************************
* Function: EEPROMReadStatus() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function reads status register from EEPROM. *
* *
* Input: None. *
* *
* Output: Status register value. *
* *
************************************************************************/
extern union _EEPROMStatus_ EEPROMReadStatus(void);
/************************************************************************
* Function: EEPROMWriteByte() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function writes a new value to address specified. *
* *
* Input: Data to be written and address. *
* *
* Output: None. *
* *
************************************************************************/
extern void EEPROMWriteByte(unsigned char, unsigned int);
/************************************************************************
* Function: EEPROMReadByte() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function reads a value from address specified. *
* *
* Input: Address. *
* *
* Output: Data read. *
* *
************************************************************************/
extern unsigned char EEPROMReadByte(unsigned int);
/************************************************************************
* Function: EEPROMWriteEnable() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function allows a writing into EEPROM. Must be called *
* before every writing command. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
extern void EEPROMWriteEnable(void);
extern void EEPROMWriteDisable(void);
/**********************************************************************
* ?2007 Microchip Technology Inc.
*
* FileName: spieeprom.c
* Dependencies: Header (.h) files if applicable, see below
* Processor: PIC24Fxxxx
* Compiler: MPLAB?C30 v3.00 or higher
*
* SOFTWARE LICENSE AGREEMENT:
* Microchip Technology Incorporated ("Microchip") retains all
* ownership and intellectual property rights in the code accompanying
* this message and in all derivatives hereto. You may use this code,
* and any derivatives created by any person or entity by or on your
* behalf, exclusively with Microchip's proprietary products. Your
* acceptance and/or use of this code constitutes agreement to the
* terms and conditions of this notice.
*
* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS". NO
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
* NOT LIMITED TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS
* CODE, ITS INTERACTION WITH MICROCHIP'S PRODUCTS, COMBINATION WITH
* ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
*
* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE
* LIABLE, WHETHER IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR
* BREACH OF STATUTORY DUTY), STRICT LIABILITY, INDEMNITY,
* CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR
* EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER
* CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE
* DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWABLE BY LAW,
* MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS
* CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP
* SPECIFICALLY TO HAVE THIS CODE DEVELOPED.
*
* You agree that you are solely responsible for testing the code and
* determining its suitability. Microchip has no obligation to modify,
* test, certify, or support the code.
*
* REVISION HISTORY:
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Author Date Comments on this revision
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Albert Z. 05/16/08 First release of source file
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 25LC256 is connected to SPI2
************************************************************************/
#include "spi2.h"
#include "spieeprom.h"
/************************************************************************
* Function: EEPROMInit *
* *
* Preconditions: SPI module must be configured to operate with *
* parameters: Master, MODE16 = 0, CKP = 1, SMP = 1. *
* *
* Overview: This function setup SPI IOs connected to EEPROM. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
void EEPROMInit()
{
// Set IOs directions for EEPROM SPI
EEPROM_SS_PORT = 1;
EEPROM_SS_TRIS = 0;
}
/************************************************************************
* Function: EEPROMWriteByte() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function writes a new value to address specified. *
* *
* Input: Data to be written and address. *
* *
* Output: None. *
* *
************************************************************************/
void EEPROMWriteByte(unsigned char Data, unsigned int Address)
{
unsigned char Local_8;
EEPROMWriteEnable();
mEEPROMSSLow();
Local_8 = writeSPI2(EEPROM_CMD_WRITE);
Local_8 = writeSPI2(Hi(Address));
Local_8 = writeSPI2(Lo(Address));
Local_8 = writeSPI2(Data);
mEEPROMSSHigh();
// wait for completion of previous write operation
while(EEPROMReadStatus().Bits.WIP);
EEPROMWriteDisable();
}
/************************************************************************
* Function: EEPROMReadByte() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function reads a value from address specified. *
* *
* Input: Address. *
* *
* Output: Data read. *
* *
************************************************************************/
unsigned char EEPROMReadByte(unsigned int Address)
{
unsigned char Local_8;
mEEPROMSSLow();
Local_8 = writeSPI2(EEPROM_CMD_READ);
Local_8 = writeSPI2(Hi(Address));
Local_8 = writeSPI2(Lo(Address));
Local_8 = writeSPI2(0);
mEEPROMSSHigh();
return Local_8;
}
/************************************************************************
* Function: EEPROMWriteEnable() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function allows a writing into EEPROM. Must be called *
* before every writing command. *
* *
* Input: None. *
* *
* Output: None. *
* *
************************************************************************/
void EEPROMWriteEnable()
{
unsigned char Local_8;
mEEPROMSSLow();
Local_8 = writeSPI2(EEPROM_CMD_WREN);
mEEPROMSSHigh();
}
void EEPROMWriteDisable()
{
unsigned char Local_8;
mEEPROMSSLow();
Local_8 = writeSPI2(EEPROM_CMD_WRDI);
mEEPROMSSHigh();
}
/************************************************************************
* Function: EEPROMReadStatus() *
* *
* Preconditions: SPI module must be configured to operate with EEPROM. *
* *
* Overview: This function reads status register from EEPROM. *
* *
* Input: None. *
* *
* Output: Status register value. *
* *
************************************************************************/
union _EEPROMStatus_ EEPROMReadStatus()
{
unsigned char Local_8;
mEEPROMSSLow();
Local_8 = writeSPI2(EEPROM_CMD_RDSR);
Local_8 = writeSPI2(0);
mEEPROMSSHigh();
return (union _EEPROMStatus_)Local_8;
}
.END
No comments:
Post a Comment