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

Explorer 16 SPI EEPROM Code (PIC24F16KA102 & 25LC256)

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
LFern



Joined: 16 Mar 2011
Posts: 19
Location: Melbourne, Australia

View user's profile Send private message

Explorer 16 SPI EEPROM Code (PIC24F16KA102 & 25LC256)
PostPosted: Mon Mar 21, 2011 11:23 pm     Reply with quote

Hi All,

I have tested the below code which is working on Explorer 16 board PIM PIC24F16KA102 and EEPROM 25LC256.

Best regards,
Lasith.

Save code as eeprom.c and driver as 25LC256_SPI.c.
You need to include driver file in to the "Other Files" on MPLAB project.

You can see the output message through a terminal emulator connected to Explorer 16 serial out with baud 9600, 8N1, no handshaking.

Output message:

Write data:4 to address:5

Read address:5 expecting data:4, received: 4

Sample Source:
Code:

//--- eeprom code ---

#include <24F16KA102.h>
#include <25LC256_SPI.c>
#include <STDLIB.H>

#fuses NOWDT         // No WatchDog Timer
#use delay(internal=8Mhz)
#use rs232(uart1, baud=9600)

void main()
{
int16 SPI_Temp;

init_ext_eeprom();
     
printf("\n\r\n\r");
   printf("Write data:4 to address:5");
   write_ext_eeprom(5, 4);

   SPI_Temp = read_ext_eeprom(5);
   printf("\n\r");
   printf("Read address:5 expecting data:4, received:%d", SPI_Temp);

while(1);
}

Code:

//--- driver code ----

#define EEPROM_SELECT PIN_B15

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE    32768

void init_ext_eeprom()
{
output_high(EEPROM_SELECT);   
setup_spi(SPI_MASTER | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 );
}

//--------------------------------
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);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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