I2C read EEPROM not working

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2C read EEPROM not working

957 Views
AceMax
Contributor I

Is there any problem in this read_ee function?

Debuging I notice that IIC2D isn't fill with EEPROM_CTRL_WR & 0xff. Does anybody know why.

I am using the MCF51CN128.

 

//-----------------------------------------------------------

 

  I2C_Init();

 

// EEPROM test

 

  I2C_WRITE;                      //Write Protection OFF

  test = read_ee(10);

 

//-----------------------------------------------------------

//-----------------------------------------------------------

 

void I2C_Init(void)
{
    u8 temp;
   
    // Config. I2C pins

    PTCPF1_C5 = 0;             //GPIO - Write Protection Pin
    PTCDD_PTCDD5 = 1;       //Output
    PTAPF2_A1 = 0b11;        //SDA2
    PTAPF2_A2 = 0b11;        //SCL2

    I2C_NWRITE();              //Write Protection ON


    IIC2F = 0x09;                // divide CPU clock by 32
    IIC2C1_IICEN = 1;          // Turn on the I2C port
    IIC2C1_IICIE = 1;

// if bit busy set, send a stop condition to slave module

    if (IIC2S_BUSY)
    {
        IIC2C1 = 0;                // clear control register
        IIC2C1_IICEN = 1;       // enable module
        IIC2C1_MST = 1;        // send a START conditionn
        temp = IIC2D;            // dummy read
        IIC2S = 0;                 //clear status register
        IIC2C1 = 0;               // clear control register
        IIC2C1_IICEN = 1;      // enable the module again
        IIC2C1_IICIE = 1;       // enable I2C interrupt
    }  
}

 

//-----------------------------------------------------------

//-----------------------------------------------------------

 

#define    EEPROM_CTRL_WR        0xA0
#define    EEPROM_CTRL_RD        0xA1

 

 

u8 read_ee(int address)
{
    u8 data = 0;

    //wait if the bus is busy or a transfer is in progress
   
    while( !IIC2S_TCF ||             // Wait here while either xfer is not done..
           IIC2S_BUSY ) {};          // or the bus is busy

    IIC2S_IICIF = 0;                   // clear the interrupt flag if on
   
    IIC2C1_TXAK = 0;                 // enable the ACK
    IIC2C1_TX = 1;                    // Set the Transmit Mode
    IIC2C1_MST = 1;                  // Generate the START condition

    IIC2D = EEPROM_CTRL_WR & 0xff;   // Send the Control byte to the EEPROM
    
    while(!IIC2S_IICIF){};           // then wait for the control byte done
    IIC2S_IICIF = 0;                   // clear the flag
    
    IIC2D = address & 0xff;          // Send address to EEPROM

    while(!IIC2S_IICIF){};            // then wait for the address byte done
    IIC2S_IICIF = 0;                   // clear the flag

    IIC2C1_RSTA = 1;                 // Generate a REPEAT START condition
    IIC2D = EEPROM_CTRL_RD & 0xff;   // Send the Read control byte to the EEPROM

    while(!IIC2S_IICIF){};           // then wait for control byte to go out
    IIC2S_IICIF = 0;                   // clear the flag

    IIC2C1_TX = 0;                    // setup for the last xfer
    IIC2C1_TXAK = 1;                 // no ack....                   

    data = IIC2D;                      // Dummy xfer to trigger the read
    while(!IIC2S_IICIF){};           // then wait for read data
    IIC2S_IICIF = 0;                   // clear the flag

    IIC2C1_MST = 0;                  // Send the stop condition

    data = IIC2D & 0xff;             // Grab it.

    return(data);
}

 

//-----------------------------------------------------------

 

Thanks

 

André Fernandes

Labels (1)
0 Kudos
Reply
1 Reply

372 Views
AceMax
Contributor I
By the way. I'm using the CAT24C01 EEPROM.
0 Kudos
Reply