Search This Blog

Monday, November 12, 2007

25XX640

Here is a hardware SPI driver for the 25LC640. This should help
you to write a driver for the 25LC320.

The test program fills up the EEPROM with random numbers and then
reads back the data and compares it to the original data. Any errors
found are reported. The dots show the progress in writing or reading.
Example of good output:
Code:

writing................................
reading................................
done

Note: The \WP and \HOLD pins are both connected to +5v.

Here is the test program that calls the driver:
Code:
#include <16f877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define SPI_MODE_0_0 0x4000
#define SPI_MODE_0_1 0x0000
#define SPI_MODE_1_0 0x0010
#define SPI_MODE_1_1 0x4010

/*
// If your hardware SPI pins are different than the
// ones used on the 16F877, then define them here
// and un-comment this section.
#define EEPROM_SELECT PIN_C2
#define EEPROM_CLK PIN_C3
#define EEPROM_DI PIN_C5
#define EEPROM_DO PIN_C4
*/

#include <25lc640_hardware_spi.c>

#include
//========================
void main()
{
int8 data;
int8 wrote;
int16 addr;
int16 errors = 0;

init_ext_eeprom();

// Fill eeprom with random data.
printf("\n\r");
printf("writing");

srand(0x55);

for(addr = 0; addr < addr ="="" addr =" 0;" data =" read_ext_eeprom(addr);" wrote =" (int8)rand();">= 10)
break;
}

if((int8)addr == 0)
putc('.');
}

printf("\n\r");
printf("done\n\r");

while(1);
}



Here is the hardware SPI driver for the 25LC640:
Code:

#ifndef EEPROM_SELECT
#define EEPROM_SELECT PIN_C2
#define EEPROM_CLK PIN_C3
#define EEPROM_DI PIN_C5
#define EEPROM_DO PIN_C4
#endif

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 8192

void init_ext_eeprom()
{
output_high(EEPROM_SELECT);
setup_spi(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_16 );
}

//--------------------------------
int1 ext_eeprom_ready(void)
{
int8 data;

output_low(EEPROM_SELECT);
spi_write(0x05);
data = spi_read(0);
output_high(EEPROM_SELECT);
return(!bit_test(data, 0));
}

//--------------------------------
void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data)
{
while(!ext_eeprom_ready());

output_low(EEPROM_SELECT);
spi_write(0x06);
output_high(EEPROM_SELECT);

output_low(EEPROM_SELECT);
spi_write(0x02);
spi_write(address >> 8);
spi_write(address);
spi_write(data);
output_high(EEPROM_SELECT);
}
//--------------------------------

BYTE read_ext_eeprom(EEPROM_ADDRESS address)
{
int8 data;

while(!ext_eeprom_ready());

output_low(EEPROM_SELECT);
spi_write(0x03);
spi_write(address >> 8);
spi_write(address);

data = spi_read(0);
output_high(EEPROM_SELECT);

return(data);
}


Ref : PCM programmer,Wed Sep 13, 2006 2:19 pm, http://www.ccsinfo.com/forum/viewtopic.php?t=28199&start=1

No comments: