En esta ocasion traigo una versión modificada de la libreria para el 25xx1024.
///////////////////////////////////////////////////////////////////////////
//// Autores:
//// Luis Alberto Vargas Tijerino
//// Francisco Sevilla
////
//// Version: 2.0
//// Fecha: 19-Dic-10
////
//// LIBRERIA PARA EEPROM EXTERNA 25AA1024 UTILIZANDO SPI ////
//// ////
//// init_ext_eeprom(); Call before the other functions are used ////
//// if you use hardware spi ////
//// ////
//// write_ext_eeprom(a, d); Write the byte d to the address a ////
//// ////
//// d = read_ext_eeprom(a); Read the byte d from the address a ////
//// ////
//// ext_eeprom_ready(); wait until the eeprom is ready ////
//// to receive opcodes ////
//// ////
//// The main program may define EEPROM_SELECT and #use spi(...) ////
//// ////
//// ////
//// Pin Layout ////
//// ----------------------------------------------- ////
//// | __ | ////
//// | 1: CS EEPROM_SELECT | 8: VCC +5V | ////
//// | | ____ | ////
//// | 2: SO EEPROM_DO | 7: HOLD +5V | ////
//// | __ | | ////
//// | 3: WP +5V | 6: SCK EEPROM_CLK | ////
//// | | | ////
//// | 4: VSS GND | 5: SI EEPROM_DI | ////
//// ----------------------------------------------- ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// Este codigo fue modificado para utilizarzse en ////
//// SPI Serial Bus 25AA1024 a partir de la libreria 25640.c ////
//// 9356SPI.c y ex_spi.c ////
///////////////////////////////////////////////////////////////////////////
//Definicion de pines
#ifndef EEPROM_SELECT
#define EEPROM_SELECT PIN_A4
#use spi(MASTER,DI=PIN_A0,DO=PIN_A1,CLK=PIN_A2,MODE=0,MSB_FIRST,BITS=8)
#endif
#use spi(MASTER,DI=PIN_A0,DO=PIN_A1,CLK=PIN_A2,MODE=0,MSB_FIRST,BITS=8)
#define EEPROM_SIZE 8388608 //1024Kb = 1024*1024*8=8388608
//SET DE INSTRUCCIONES DE 25AA1024
#define READ 0x03 //Read data from memory array beginning at selected address
#define WRITE 0x02 //Write data to memory array beginning at selected address
#define WREN 0x06 //Set the write enable latch (enable write operations)
#define WRDI 0x04 //Reset the write enable latch (disable write operations)
#define RDSR 0x05 //Read STATUS register
#define WRSR 0x01 //Write STATUS register
#define PE 0x42 //Page Erase – erase one page in memory array
#define SE 0xD8 //Sector Erase – erase one sector in memory array
#define CE 0xC7 //Chip Erase – erase all sectors in memory array
#define RDID 0xAB //Release from Deep power-down and read electronic signature
#define DPD 0xB9 //Deep Power-Down mode
#define init_ext_eeprom() SETUP_SPI(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4); //Configurar SPI
void ext_eeprom_ready(){
OUTPUT_LOW(EEPROM_SELECT);
spi_xfer(RDSR); //Espera a que el registro status de la eeprom sea
while(bit_test(spi_xfer(RDSR),0)); //igual a cero
OUTPUT_HIGH(EEPROM_SELECT);
}
void write_enable_extee(){
OUTPUT_LOW(EEPROM_SELECT);
spi_xfer(WREN); //Enviamos el comando Write Enable
OUTPUT_HIGH(EEPROM_SELECT);
}
void write_ext_eeprom(int32 address, BYTE data) {
int cmd[5];
int i;
cmd[0]=WRITE;
cmd[1]=make8(address,2);//Tomamos el byte 2 de address
cmd[2]=make8(address,1);//Tomamos el byte 1 de address
cmd[3]=make8(address,0);//Tomamos el byte 0 de address
cmd[4]=data;
write_enable_extee();
OUTPUT_LOW(EEPROM_SELECT);//Habilito el pin de seleccion de chip.
for(i=0;i<=4;i++)
spi_xfer(cmd[i]); //Envio los cmd del 0 hasta el 4
OUTPUT_HIGH(EEPROM_SELECT);//Deshabilito el pin de seleccion de chip.
}
BYTE read_ext_eeprom(int32 address) {
int cmd[4];
int i,data;
cmd[0]=READ;
cmd[1]=make8(address,2);//Tomamos el byte 2 de address
cmd[2]=make8(address,1);//Tomamos el byte 1 de address
cmd[3]=make8(address,0);//Tomamos el byte 0 de address
ext_eeprom_ready();
OUTPUT_LOW(EEPROM_SELECT);
for(i=0;i<=3;i++)
spi_xfer(cmd[i]); //envio los cmd desde el 0 hasta el 3
data=spi_xfer(0); //luego envio 0 y recivo el dato.
OUTPUT_HIGH(EEPROM_SELECT);
return data;
}
Y a continuacion el ejemplo de utilización.
#include <12f683.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=4M)
#define EEPROM_SELECT PIN_A4
#use spi(MASTER,DI=PIN_A0,DO=PIN_A1,CLK=PIN_A2,MODE=0,MSB_FIRST,BITS=8)
#include <251024.c>
void main(){
int datos_recibidos,i=0;
OUTPUT_HIGH(EEPROM_SELECT);
while (TRUE){
write_ext_eeprom(i,i+' ');
datos_recibidos=read_ext_eeprom(i);
i++;
}
}
La ventaja de esta version es que puede ser utilizada por los PIC que no tienen spi por hardware.
A continuacion dejo el archivo adjunto del trabajo.