CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Hardware SPI <-> 25LC320 EEPROM question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 13, 2006 2:19 pm     Reply with quote

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);
}
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Wed Sep 13, 2006 2:57 pm     Reply with quote

Solved. Thanks PCM.

The issue is my prototyping board, an ME Labs LAB-X1. It has a socket for an 8 pin DIP SPI EEPROM. It has the MOSI and MISO lines reversed. Confused

I pulled the EEPROM out of the socket and placed it in a separate protoboard and it works just fine now that pin C5 -> EEPROM's SI and pin C4 <- EEPROM's SO.

Geez. All this time wasted for something so simple and so obvious. Reinforced lesson: never trust that a commercial product is wired correctly.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group