View previous topic :: View next topic |
Author |
Message |
dustimv
Joined: 18 Sep 2007 Posts: 4
|
SPI EEPROM Help |
Posted: Tue Sep 18, 2007 10:14 pm |
|
|
I am using a PIC18F4550 to write to a Microchip 25AA512 EEPROM. No matter what I send to the memory to be stored, I receive back a 0. This includes the reading of status registers. I have sent the data to another PIC and it read the data fine. I am not using any pull-up or pull-down resistors on any SPI lines. I have enclosed the code I am using. My software is MPLAB v7.60 and my CCS Compiler is PCH v3.249. Any help would be greatly appreciated.
#include <18F4550>
#fuses intrc_io,nowdt,noprotect,nolvp
#use delay(clock=8000000)
#use rs232(baud=9600)
#define cs pin_d2
#define wp pin_d3
#define hold pin_d4
int dat;
void main(){
setup_oscillator(osc_8mhz);
setup_spi(spi_master | spi_l_to_h | spi_clk_div_16);
//Setup Chip
output_high(hold);
output_high(wp);
output_high(cs);
delay_ms(500);
//Setup Send
output_low(cs);
spi_write(0x06);
output_high(cs);
delay_ms(500);
//Send Data
output_low(cs);
spi_write(0x02); //Write Command
spi_write(0x00); //16-bit Address
spi_write(0x00);
spi_write(0x04); //Data
output_high(cs);
delay_ms(500);
//Get Data
output_low(cs);
spi_write(0x03); //Read Command
spi_write(0x00); //16-bit Address
spi_write(0x00);
dat=spi_read(); //Data
output_high(cs);
if(dat==0x04){
output_high(pin_D1);
}
while(1){
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 19, 2007 1:07 am |
|
|
Look at Figure 1-2 on page 5 of the 25AA512 data sheet.
Notice on the left side of the timing diagram that it lists the two
SPI modes that the eeprom supports.
http://ww1.microchip.com/downloads/en/DeviceDoc/22021B.pdf
Now look at the table in ckielstra's post.
http://www.ccsinfo.com/forum/viewtopic.php?t=32040&start=5
What SPI mode is being specified by your setup_spi() statement below ?
Quote: | setup_spi(spi_master | spi_l_to_h | spi_clk_div_16); |
ckielstra has provided some convenient #define statements that you
can include in your program. Then you can easily put the proper
SPI mode constant in the middle part of the setup_spi() statement.
You don't have to use the confusing CCS constants. You can just
look at the eeprom (or other device) data sheet and easily setup
the proper SPI mode. |
|
|
dustimv
Joined: 18 Sep 2007 Posts: 4
|
|
Posted: Wed Sep 19, 2007 6:18 pm |
|
|
Thank you for the reply. I tried in both mode 0 and 3, still I read a zero back from the memory. I used both of the following lines of code.
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16);
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16);
Where did you find the command SPI_XMIT_L_TO_H? I can't find it anywhere in the compiler manual.
Any other ideas of what to try? Once again thank you for the reply, I appreciate the help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 19, 2007 6:38 pm |
|
|
You may still have other problems with your driver, but the first step
is to setup the correct SPI mode, and also to make sure the connections
are correct. Example:
Code: |
PIC pins EEPROM pins
SCLK --> SCK
SDO --> SI
SDI <-- SO
\CS pin --> \CS |
That constant value can be found in the .H file for your PIC.
The .H files are in this directory:
Quote: | c:\Program Files\Picc\Devices |
|
|
|
dustimv
Joined: 18 Sep 2007 Posts: 4
|
|
Posted: Wed Sep 19, 2007 9:39 pm |
|
|
I've checked the connections many times (twice more because of your post).
18F4550
-----------
SDO (C7) --> (P5) SI
SDI (B0) <-- (P2) SO
SCK (B1) --> (P6) SCK
CS (D2) --> (P1) CS
Which does bring me to a question, why did Microchip decide to make the SPI out the same pin as RS232 in. Anyway I'm still trying to figure out way I am always reading a zero from the store variable (dat). Thank you again for the quick response and suggestion. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 19, 2007 11:30 pm |
|
|
Quote: | dat=spi_read(); //Data |
This line is part of the problem. You need to give the spi_read() function
a parameter of 0, so it will make the SPI module generate the clock
on the SCLK pin. Currently, you're not generating a clock.
Example:
Also, all of the CCS drivers for the "25" series SPI eeproms call a
routine called ext_eeprom_ready(). This routine sends the RDSR
command to the eeprom, to read the status register. I don't have
the 25AA512 data sheet with me, but read the data sheet. You
probably need to call that routine. You can get the code from the
25640.c driver. It's in this directory:
Quote: | c:\Program Files\Picc\Drivers |
|
|
|
|