Dominic Meyer

Problems to getting IIC with EEPROM to work! -  MC9S12XDT512

Discussion created by Dominic Meyer on Nov 29, 2007
Latest reply on Dec 2, 2007 by Michael Galda
Hi @ all,

i've little problems to get my IIC bus to work.
I try to read from an EEPROM, but i only get 0xFF in my data array.
I'm using the 9S12XDT512 and a Microchip 24LC16B EEPROM.

Here is my code:

int flag; // flag for the IIC-IR
int length; // datalength to read from IIC
int i; // counter for data
byte rAdr; // byte adress (EEPROM data)
byte dummy; // dummy byte for reading data
byte data[2048] = 0; // data from IIC


void IIC_Init(void)
{
    IIC0_IBFD = 0x4C; // frequency divider register: Bus = 8Mhz =>SCL = 91 kHz
    IIC0_IBAD = 0x00; // slave address of this module
    IIC0_IBSR = 0x12; // clear the IBAL and IBIF flags
    IIC0_IBCR = 0xC0; // enables ICC with interrupts
}

/******************************************************************
************************ END IIC_Init *****************************
******************************************************************/

/******************************************************************
  Function:     void IIC_Read(unsigned char read_addr)
  Developer:    nic_mcp
  Parameters:   read_addr: address of slave;
  Returns:      Nothing
  Description:  read EEPROM data
******************************************************************/

void IIC_Read(unsigned char read_addr, int len)
{
   flag=10;
   length = len;
   i=0;
   rAdr = read_addr;
   IIC0_IBCR_MS_SL = 1; // set transmit and master mode
   IIC0_IBCR_TX_RX = 1; // and generate start condition
   IIC0_IBDR = 0xb1;  // Send Slave Adress with read mode
}

/******************************************************************
************************ END IIC_Read *****************************
******************************************************************/

/******************************************************************
  Function:     void IIC_IR()
  Developer:    nic_mcp
  Parameters:   none
  Returns:      Nothing
  Description:  called by interrupt
******************************************************************/

void IIC_IR()
{
    IIC0_IBSR = 0x12; // clear the IBAL and IBIF flags
   
    switch(flag)
    {
   
      case 10:
      {
        IIC0_IBDR = rAdr;
        flag++;
        return;
      }
      case 11:
      {
        IIC0_IBDR = rAdr;
        flag++;
        return;
      }
      case 12:
      {
        IIC0_IBCR_RSTA = 1; // set up repeated start
        IIC0_IBDR = 0xa1;
        flag++;
        IIC0_IBCR_TX_RX = 0;
        IIC0_IBCR_TXAK = 0;
        return;
      }
     
      case 13:
      {
        if (length>1)
        {
          data[i] = IIC0_IBDR; // read data from bus
          length--;
          i++;
          return;
        } else
        {
           IIC0_IBCR_TXAK = 1; // ACK disabled
           i++;
           length--;
           flag++;
           return;
        }  
      }
      case 14:
      {
        IIC0_IBCR_MS_SL = 0; // create stop condition
        flag = 0;
        return;
      }
    
    }
}

/******************************************************************
************************ END IIC_IR *******************************
******************************************************************/



/******************************************************************
  Function:     void main(void)
  Developer:    nic_mcp
  Parameters:   none
  Returns:      Nothing
  Description:  main-method
******************************************************************/


void main(void)
{
  /* put your own code here */
  EnableInterrupts;
  IIC_Init(); // initialize the IIC-modul
  IIC_Read(0x00,2048); // read EEPROM
  for(;:smileywink:
  {
   
  } /* wait forever */
  /* please make sure that you never leave this function */
 
}

/******************************************************************
************************ END main *********************************
******************************************************************/

Can somebody help me to get this working?

thx nic_mcp






Added p/n to subject.


Message Edited by NLFSJ on 2007-11-29 02:50 PM

Outcomes