View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
I need help with external eeprom 25AA02E48 |
Posted: Wed Jun 10, 2015 12:45 am |
|
|
Greetings! I have 25AA02E48 connected to PIC18F86J65 micro. I have connected WP and HOLD through pull-up resistors to Vcc. Here is my code:
Code: |
#include <18F86J65.h>
#Fuses HS,NOWDT
#use delay(clock=25M)
#use spi(MASTER,MODE=0,DI=PIN_C4,DO=PIN_C5,CLK=PIN_C3,bits=8,FORCE_HW)
#define EEPROMCS PIN_C2
#define EEPROMREAD 0x03
#define EEPROMWRITE 0x02
int data;
void main()
{
data=0;
delay_ms(100);
output_low(EEPROMCS);
delay_ms(1);
output_high(EEPROMCS);
delay_ms(100);
output_low(EEPROMCS);
delay_us(50);
spi_write(EEPROMWRITE);
spi_write(50);
spi_write(10);
output_high(EEPROMCS);
delay_us(10);
delay_ms(5);
output_low(EEPROMCS);
spi_write(EEPROMREAD);
spi_write(50);
while(1)
{
if(spi_data_is_in())
{
data=spi_read();
delay_ms(1);
}
}
}
|
The problem is I'm getting only 255 from the chip. Can you tell me what I'm doing wrong?
Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Wed Jun 10, 2015 1:39 am |
|
|
First, check your connections.
PIC SCL - chip SCL
PIC DI - chip DO
PIC DO - chip DI
The fact that the in and out lines have to cross, is very easy to forget...
Then read the data sheet. Look particularly at paragraph 2.3.....
You can't just write to the chip (even with the hardware WP off). You have to set the write latch before writing.
As a separate comment, spi_data_is_in, tells you nothing. You are the master. Data only arrives when _you_ clock it in. |
|
![](templates/subSilver/images/spacer.gif) |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Wed Jun 10, 2015 11:15 am |
|
|
I forgot I'm the master and I have to send dummy bite. I'll try it later. Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Wed Jun 10, 2015 11:27 am |
|
|
You are going to need to enable writing first though. Did you read the data sheet section. |
|
![](templates/subSilver/images/spacer.gif) |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Wed Jun 10, 2015 10:41 pm |
|
|
Yes! As far as I understood it I need to send 0x02 to enable read and 0x03 for writing.
I`ve change last part of my code to
No offect! It returns 255 always. I`ve tried to add small delay (50us) after every spi command - nothing again.
Am I doing write latch correctly? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Wed Jun 10, 2015 11:55 pm |
|
|
No.
You are missing the point slightly.
To write, you have to send the WREN command, and _then_ the WRITE command. As I said read section 2.3.
Note how you must send the WREN instruction, finish this, and _then_ send the write instruction. |
|
![](templates/subSilver/images/spacer.gif) |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Thu Jun 25, 2015 12:17 pm |
|
|
OK! I think I understood, but still my program is not working properly.
Here is my latest code:
Code: |
void WriteEnableSequence()
{
output_low(EEPROMCS);
delay_us(5);
spi_write(EEPROMWREN);
delay_us(5);
output_high(EEPROMCS);
}
void WriteDisableSequence()
{
output_low(EEPROMCS);
delay_us(5);
spi_write(EEPROMWRDI);
delay_us(5);
output_high(EEPROMCS);
}
void WriteByteToAddress(int data,int address)
{
WriteEnableSequence();
output_low(EEPROMCS);
delay_ms(1);
spi_write(EEPROMWRITE);
spi_write(address);
spi_write(data);
delay_ms(1);
output_high(EEPROMCS);
WriteDisableSequence();
delay_ms(1);
}
int ReadDataFromAddress(int address)
{
output_low(EEPROMCS);
delay_ms(1);
spi_write(EEPROMREAD);
spi_write(address);
return spi_read();
}
|
There is somekind of problem. I don`t always recieve correct values.
What did I miss this time?! |
|
![](templates/subSilver/images/spacer.gif) |
jeremiah
Joined: 20 Jul 2010 Posts: 1328
|
|
Posted: Thu Jun 25, 2015 12:46 pm |
|
|
You should be using spi_xfer if you use #use spi(). If you want ot use spi_read and spi_write, then you have to use setup_spi() instead of #use spi(). It is described more in detail in the manual.
Also note that your code raises the CS line in your write enable sequence and then immediately lowers it when you come out of the function. Did you verify you are giving the chip enough time to notice the transition?
Also, your read routine isn't raising CS at the end like it is supposed to. |
|
![](templates/subSilver/images/spacer.gif) |
|