PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 13, 2006 2:19 pm |
|
|
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 <STDLIB.H>
//========================
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 < EEPROM_SIZE; addr++)
{
write_ext_eeprom(addr, (int8)rand());
if((int8)addr == 0)
putc('.');
}
// Read the eeprom and check for errors.
printf("\n\r");
printf("reading");
srand(0x55);
for(addr = 0; addr < EEPROM_SIZE; addr++)
{
data = read_ext_eeprom(addr);
wrote = (int8)rand();
if(data != wrote)
{
printf("%lx: read %x, should be %x\n\r", addr, data, wrote);
errors++;
if(errors >= 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);
} |
|
|