LFern
Joined: 16 Mar 2011 Posts: 19 Location: Melbourne, Australia
|
Explorer 16 SPI EEPROM Code (PIC24F16KA102 & 25LC256) |
Posted: Mon Mar 21, 2011 11:23 pm |
|
|
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);
}
|
|
|