|
|
View previous topic :: View next topic |
Author |
Message |
pebbert9
Joined: 31 Dec 2010 Posts: 39
|
SPI EEPROM with a Explorer 16 Board |
Posted: Fri Dec 31, 2010 12:45 am |
|
|
Hello,
I am trying to get the 25LC256 EEPROM on an Explorer 16 board to work.
I found this http://www.ccsinfo.com/forum/viewtopic.php?t=28199&start=1 which seems to explain everything clearly.
I've edited the SELECT, CLK, DI, and DO pins to conform to the Explorer 16 board, but when I look at the pins on a scope I'm not getting any signals to them.
I am using PCWHD v4.116
I then found this driver which doesn't use the built-in SPI functions http://www.ccsinfo.com/forum/viewtopic.php?t=38064 . This driver does drive signals to the pins.
The SPI port doesn't seem to be setting up correctly. Any ideas why I am not getting any signals to the CLK, DI, and DO pins?
Here is the test code:
Code: | #include <24FJ128GA010.h>
#device ICD=TRUE
#fuses NOIESO // No 2-speed Start Up
#fuses NOJTAG // No JTAG
#fuses NOPROTECT // No Memory Protection
#fuses NOWDT // No WatchDog Timer
#fuses NODEBUG // Debug Mode.
#FUSES CKSFSM // Clock Switching is enabled, fail Safe clock monitor is enabled
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_F4, rcv=PIN_F5, 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_D12
#define EEPROM_CLK PIN_G6
#define EEPROM_DI PIN_G8
#define EEPROM_DO PIN_G7
#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);
} |
The driver is identical to the post.
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);
} |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Dec 31, 2010 2:03 am |
|
|
The SPI mode coding in your post is completely invalid for PIC24, they are for PIC16. Because CCS has recently fixed a bug in PIC24 mode coding, it's a bit complicated. See:
http://www.ccsinfo.com/forum/viewtopic.php?t=41421 |
|
|
pebbert9
Joined: 31 Dec 2010 Posts: 39
|
|
Posted: Sat Jan 01, 2011 12:06 am |
|
|
It turns out my issue was that the EEPROM is connected to SPI2 and I was trying to initialize SPI1 with SPI2's pins.
I have the EEPROM on the Explorer 16 board working with the following code.
Test Code:
Code: | #include <24FJ128GA010.h>
#device ICD=TRUE
#fuses NOWDT // No WatchDog Timer
#FUSES CKSFSM // Clock Switching is enabled, fail Safe clock monitor is enabled
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_F4, rcv=PIN_F5, ERRORS)
#include <25LC256_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(0x54);
for(addr = 0; addr < 16; addr++) // Change addr < 16 to addr < EEPROM_SIZE to write to all memory locations
{
write_ext_eeprom(addr, (int8)rand());
if((int8)addr == 0)
putc('.');
}
// Read the eeprom and check for errors.
printf("\n\r");
printf("reading");
srand(0x54);
for(addr = 0; addr < 16; addr++) // Change addr < 16 to addr < EEPROM_SIZE to read from all memory locations
{
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);
} |
Place the following driver into 25LC256_SPI.c
Code: |
#define EEPROM_SELECT PIN_D12
#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 32678
void init_ext_eeprom()
{
output_high(EEPROM_SELECT);
setup_spi2(SPI_MASTER | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 );
}
//--------------------------------
int1 ext_eeprom_ready(void)
{
int8 data;
output_low(EEPROM_SELECT);
spi_write2(0x05);
data = spi_read2(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_write2(0x06);
output_high(EEPROM_SELECT);
output_low(EEPROM_SELECT);
spi_write2(0x02);
spi_write2(address >> 8);
spi_write2(address);
spi_write2(data);
output_high(EEPROM_SELECT);
}
//--------------------------------
BYTE read_ext_eeprom(EEPROM_ADDRESS address)
{
int8 data;
while(!ext_eeprom_ready());
output_low(EEPROM_SELECT);
spi_write2(0x03);
spi_write2(address >> 8);
spi_write2(address);
data = spi_read2(0);
output_high(EEPROM_SELECT);
return(data);
} |
|
|
|
|
|
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
|