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 for PIC24FJ64GA004 PIM

 
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 for PIC24FJ64GA004 PIM
PostPosted: Sun May 29, 2011 11:27 pm     Reply with quote

Further to the post: http://www.ccsinfo.com/forum/viewtopic.php?t=45014

Hi All,

I have tested the below code which is working on Explorer 16 board PIC24FJ64GA004 PIM with explorer 16 built in SPI EEPROM 24LC256.

Best regards,
Lasith.

Save code as eeprom.c and driver as Driver_SPI_25LCxxx.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 38400, 8N1, no handshaking.

Output message:

Write data:4 to address:5

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

Sample Source:

Code:


//--- eeprom test code ---

#include <24FJ64GA004.h>
#include <string.h>
#include <Driver_SPI_25LCxxx.c>


#fuses NOWDT         
#use delay(internal=8Mhz)

//SPI EEPROM
#pin_select SCK1OUT   = PIN_C8
#pin_select SDI1   = PIN_C4
#pin_select SDO1   = PIN_C5

//UART
#pin_select U2TX = PIN_C9       
#pin_select U2RX = PIN_C3

#use rs232(uart2, baud=38400)

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:

// FILE NAME: Driver_SPI_25LCxxx.c
//---EEPROM 24LC256 DEVICE---

#define EEPROM_SELECT PIN_A8

#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