Nesse exemplo mostro como utilizar uma rotina de Leitura/Escrita 25LC256.

Segue o código:

[sections] [section title=”Código Fonte”]

#define NAO 0
#define SIM 1
#define DESLIGA NAO
#define LIGA SIM
#define FALSO NAO
#define VERDADEIRO SIM
#define SAIDA NAO
#define ENTRADA SIM

#define WRITENABLE 0x06 // 0b00000110
#define WRITEDISABLE 0x04 // 0b00000100
#define RDSR 0x05 // 0b00000101
#define WRSR 0x01 // 0b00000001
#define READ 0x03 // 0b00000011
#define WRITE 0x02 // 0b00000010

// SELEÇÃO DO SPI
sbit EXT_EEPROM_DI at RC4_bit;
sbit EXT_EEPROM_DI_Direction at TRISC4_bit;
sbit EXT_EEPROM_DO at RC5_bit;
sbit EXT_EEPROM_DO_Direction at TRISC5_bit;
sbit EXT_EEPROM_CLK at RC3_bit;
sbit EXT_EEPROM_CLK_Direction at TRISC3_bit;
// HABILITAR EEPROM EXTERNA
sbit EXT_EEPROM_CS at RC0_bit;
sbit EXT_EEPROM_CS_Direction at TRISC0_bit;

//##############################################################################
// MANIPULAÇÃO DAS EEPROM´S [INTERNA OU EXTERNA] // Rotina Testada e OK
unsigned char spi_write(unsigned char data_value)
{
unsigned char read=0,cont;
for(cont=0;cont<8;cont++)
{
asm{CLRWDT}
read<<=1;
EXT_EEPROM_DO = ((data_value & 0x80)==0x80);
EXT_EEPROM_CLK = DESLIGA;
Delay_us(2);
read|=EXT_EEPROM_DI?1:0;
Delay_us(2);
EXT_EEPROM_CLK = LIGA;
data_value<<=1;
}
return read;
}

// Rotina Testada e OK
void writeEEPROM(unsigned int mem_address,unsigned char value)
{
unsigned char INTCON_SAVE;
EXT_EEPROM_CS = DESLIGA;
spi_write(WRITENABLE);
EXT_EEPROM_CS = LIGA;
Delay_us(100);
EXT_EEPROM_CS = DESLIGA;
spi_write(WRITE);
spi_write(mem_address>>8);
spi_write(mem_address&0XFF);
spi_write(value);
EXT_EEPROM_CS = LIGA;
Delay_ms(5);
}

// Rotina Testada e OK
unsigned char readEEPROM(unsigned int mem_address)
{
unsigned char value;

EXT_EEPROM_CS = DESLIGA;
spi_write(READ);
spi_write(mem_address>>8);
spi_write(mem_address&0XFF);
value = spi_write(0);
EXT_EEPROM_CS = LIGA;

return value;
}

// Rotina Testada e OK
void writeArrayEEPROM(unsigned int mem_address,unsigned char *value,unsigned int qtde)
{
unsigned int cnt=0,cnt2=0,n_pages,mem;
mem=mem_address;

cnt=0;

//#define bytes_per_page 32 // 25lc640
#define bytes_per_page 64 // 25lc256
cnt2=(mem)/bytes_per_page;
n_pages= (mem+qtde)/bytes_per_page; //bytes_per_page numero maximo de bytes de escrita por página
cnt=(mem%bytes_per_page);
while(cnt2<=n_pages)
{
EXT_EEPROM_CS = DESLIGA;
spi_write(WRITENABLE);
EXT_EEPROM_CS = LIGA;

EXT_EEPROM_CS = DESLIGA;
spi_write(WRITE);
spi_write(mem>>8);
spi_write(mem&0XFF);
while(cnt++<bytes_per_page)
{
if((++mem)>(qtde+mem_address))
break;
spi_write(*value++);
}
EXT_EEPROM_CS = LIGA;
cnt=0;
cnt2++; //muda de página
//mem=cnt2*bytes_per_page;
Delay_ms(5);
}
}
// Rotina Testada e OK
void readArrayEEPROM(unsigned int mem_address,unsigned char *value,unsigned int qtde)
{
unsigned char cnt=0;

while(cnt<qtde)
{
*value++ = readEEPROM(mem_address+cnt++,EEPROM_EXTERNA);
}
}

[/section] [/sections]
Obrigado e até o próximo post.

DATEK Tecnologia Eletrônica
[C] Rotina de Leitura/Escrita 25LC256
Tags:                     

Deixe uma resposta

%d blogueiros gostam disto: