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

PIC16F737 wtih SPI External EEPROM

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







PIC16F737 wtih SPI External EEPROM
PostPosted: Fri Dec 29, 2006 6:17 am     Reply with quote

Hi !

I try to write and read data with a external Eeprom from microchip using SPI hardware of my PIC 16F737, the external eeprom is 25AA256.
I didn't find any drivers for it but I found a drivers for a 25LC640 which are almost the same except memory size.

Here is my code : eeprom function, then main programm...

Code:

#include <16F737>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock=16000000)
#use rs232(baud=114000, xmit=PIN_C6, rcv=PIN_C7)

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

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

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE    32768

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

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


/*
// 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_A5
#define EEPROM_CLK    PIN_C3
#define EEPROM_DI     PIN_C5
#define EEPROM_DO     PIN_C4
*/


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

delay_ms(5000);

//set_tris_c(0b00000000);
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>= 100)
          break;
      }

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

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

while(1);
}



by executing this programm, nothing is written in the eeprom (the programm return 100 lines with "data = 0x00" ... I'm not sure about my setup_spi .. can anyone help me ?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Dec 29, 2006 8:04 am     Reply with quote

Code:
for(addr = 0; addr <EEPROM_SIZE>= 100)
          break;
      }
When posting code, please select the checkbox for disabling HTML code, now the above code fragment is corrupted. Or, even better, become a member of this forum and you can edit messages you have already posted and you can disable the HTML setting by default in your profile settings.

Quote:
I'm not sure about my setup_spi
Your SPI-setup looks correct, that is, if your chip is similar to the 25LC640. I didn't look at the datasheets. However, instead of hardcoding the values for the different SPI-modes I would use the defines from the CCS include files. Doing so will reduce the risk of CCS changing values in a new compiler release.
Code:
#define SPI_MODE_0_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1  (SPI_L_TO_H)
#define SPI_MODE_1_0  (SPI_H_TO_L)
#define SPI_MODE_1_1  (SPI_H_TO_L | SPI_XMIT_L_TO_H)


One problem in your program is that you didn't disable the analog ports. In the PIC processors many pins have multiple possible uses, for example pin A5 can be a digital input, output, slave select input, analog input or comparator 2 output. By default the analog pins get priority over the digital input/output functions, so you will have to disable all other port functionality by adding the following lines to the start of your program.
Code:
setup_adc_ports(NO_ANALOGS);
setup_comparator(NC_NC_NC_NC);
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