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 CCS Technical Support

I need help with external eeprom 25AA02E48

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

I need help with external eeprom 25AA02E48
PostPosted: Wed Jun 10, 2015 12:45 am     Reply with quote

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!
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 1:39 am     Reply with quote

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.
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 11:15 am     Reply with quote

I forgot I'm the master and I have to send dummy bite. I'll try it later. Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 11:27 am     Reply with quote

You are going to need to enable writing first though. Did you read the data sheet section.
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 10:41 pm     Reply with quote

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
Code:

data=spi_read(0);

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?
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 11:55 pm     Reply with quote

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.
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu Jun 25, 2015 12:17 pm     Reply with quote

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?!
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Thu Jun 25, 2015 12:46 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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