i2c and serial eeprom

Status
Not open for further replies.

cipi-cips

Member level 4
Joined
Jun 30, 2008
Messages
76
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Visit site
Activity points
1,867
Hello

Can someone help with my code, I am just trying to write something in the 24LC32 serial EEPROM with i2c protocol.
I have studied datasheat both for memory and PIC controller. I am using 18F4550.
Compiler is C18 from microchip.

Here is my little code:

Code:
#include <pic18f4550.h>
#include <i2c.h>
#define OUT PORTDbits.RD7

void main (main)
{
	TRISDbits.TRISD7 = 0;
	 OpenI2C(MASTER, SLEW_OFF);
	SSPADD = 0x31;     // determinate baud rate (20Mhz xtal, and 100Khz rate), with this formula I got ((Fos/4)/100Khz)-1.

        StartI2C();
        IdleI2C();
        WriteI2C(0xA0);    //this is control byte
        IdleI2C();
        WriteI2C(0x01);    //write at this address
        IdleI2C();
        WriteI2C(0xFF);    //write data 255
        IdleI2C();
        StopI2C();

        StartI2C();
        IdleI2C();
        WriteI2C(0xA1);    //this is control byte
        IdleI2C();
        number = Read2C();    //read
        IdleI2C();
        StopI2C();

       if (number == 255)
       {
          OUT = 1;
        }
        else
        {
           OUT = 0;
         }
        while(1);
}

and this does not work ( any Help ?
 

HI there

I have put resistors both on SDK and SCL to the VCC, (I found on internet that 24C32 wont work without that).
Second I put SDA and SCL both LOW at start (TRISBbits.TRISB0 = 0 and TRISBbits.TRISB1 = 0).

Whit these settings I get something on the output, but when ever I change data output is always the same.

Any help ?
 

Done I have put delay and this is what happens, no matter what data I put always get 0 at output

- - - Updated - - -

It is like my ReadI2C() function doesn't read anything
 

Hello!

Check ACK signal during Reading process

And possibly SEND ack signal when you're reading.
Basically when you write, you read ack to check whether the receiver got the message.
And when you read, it's the opposite: you read, and then when you got the byte, you pull
ack low. See the timing and check where you have to read/write an ack/nack.

Dora.
 

Hi
Ok I have studied and recorded more results,
In the picture you can see that reading sequence is always reading 255 (11111111) all data bits are high no mater what I enter as data.

**broken link removed**



Btw what are those outputs that are yellow circled
 

may be io port burn!

- - - Updated - - -

it have Problem, you written 0x00, where you read ?
 

Hi

WP pin is tied to ground, do you probably now why does PIC cant leave this loop while(EEAckPolling(0xA));.
 

Reviewing the thread, I found that the code in fact doesn't perform required acknowledge polling after the write cycle. Acknowlegde of the succeeding read command does not guarantee that the write has been completed.

WP pin is tied to ground, do you probably now why does PIC cant leave this loop while(EEAckPolling(0xA));.
What is EEAckPolling(), a built-in compiler function? Presumed it does correct acknowledge polling, it should finish within 5 ms. Otherwise there's something wrong.
 

Peripheral address correct? and How do you prove peripherals OK?

- - - Updated - - -

right! Possible! it should finish within 0.4us-5ms!

- - - Updated - - -

may be speed very fast
 



What resistor values did you use ?

- - - Updated - - -

What pins on 24LC32 you connect ?
 

Hello

What is EEAckPolling(), a built-in compiler function? Presumed it does correct acknowledge polling, it should finish within 5 ms. Otherwise there's something wrong.

Yep this is a built in compiler function and it does correct acknowledge polling. Inside of that function is If state which test bus collision
Code:
 If (PIR2bits.BCLIF)
           {
             return (-1)
            }

Program loops here and always return -1.

Peripheral address correct? and How do you prove peripherals OK?

Yep address are corrected from datasheet,

How do you prove peripherals OK?

How do you mean ?

may be speed very fast

Speed of PIC 18F4550 is 48Mhz because I use it for USB connection.
By this speed I set SSPADD using this formula ((Fosc/4)/)Desired speed))-1 , where Desired speed using PIC datasheet can be 100Khz, 400Khz and 1Mhz.

Code:
SSPADD = 119  //100Khz
SSPADD = 29  //400Khz
SSPADD = 11 //1Mhz

What resistor values did you use ?

Using AppNote AN997 from microchip (I2C and PIC18 interfacing) I have tried with 2K, 2.2K, 4.7K, 10K.
Results are always same

What pins on 24LC32 you connect

WP, A0, A1, A2 to the ground, SCL to SCL(PIC), SDA to SDA(PIC) and of course supply 5V.



Hope this will help you to help me

Best regards
 

First Those yellow is not output,it just Write Process,It has a relationship with your code!
Second You do not know what I mean,A0, A1 ,A2 Code should be set according to the circuit !
Instead of Default Address from code!
Do you understand ?
 

A0, A1 ,A2 Code should be set according to the circuit
That's right, but some question are already answered by the waveforms. If the EEPROM does ACK the commands, you can expect it's addressed correctly, isn't it?
 


Well every paper that I have read says that A0, A1, and A2 are used for multiple slave devices. Since I have only 1 slave only 1 EEPROM than according to datasheet those pins must be tied to ground.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…