Christopher Arzola

9S12DP512 IIC not working

Discussion created by Christopher Arzola on Apr 9, 2011
Latest reply on Apr 9, 2011 by Christopher Arzola

I'm trying to interface an I2C device and so far have measured nothing but noise coming from the output of the device. I followed the exact process given by the AN3291.pdf file (which pretty much has the init and send functions already written). I am fairly certain that the actual I2C functions that I'm using (I2C_Open() and I2C_Send()) are correct. In addition, I added a timer wait of 250ms at the end of the I2C_Send() function to ensure that the slave device has enough time to process the request.


I'm thinking the problem could be the MODRR register. What should I be setting it to, if anything?


I've attached the AN3291.pdf file. Here's my code:


/************************* I2C.c *************************/
#include "I2C.h"

/************************* Public Functions *************************/
/************************* I2C_Open *************************/
// Inputs: none
// Outputs: none
// enable I2C interface on 9S12
void I2C_Open(void) {

  MODRR = 0x00;
  IBFD = 0x4C; // frequency divider register: 91kHz (@ 8MHz)
  IBCR = 0x80; // enable I2C module with no interrupts

/************************* I2C_Send *************************/
// Inputs:
//   slaveAddr - the address of the slave device (TEA6320T)
//   slaveReg - the register to write to on TEA6320T
//   data -  data to send to slave device (TEA6320T)
// Outputs: none
// will transmit a byte of data to TEA6320T
void I2C_Send(uint8 slaveAddr, uint8 slaveReg, uint8 data) {
  IBCR_TXAK = 0;
  IBCR |= 0x30;                          // send start
  IBDR = slaveAddr & (0xFE); // send slave address
  while(!IBSR_IBIF);                  // wait for the address to be sent
  IBSR_IBIF = 1;                        // clear IBIF
  while(IBSR_RXAK);              // wait for "ack" from slave
  IBDR = slaveReg;                 // send first byte
  while(!IBSR_IBIF);                // wait for the data to be sent
  IBSR_IBIF = 1;                      // clear IBIF
  while(IBSR_RXAK);             // wait for "ack" from slave
  IBDR = slaveReg;                // send second byte
  while(!IBSR_IBIF);               // wait for the data to be sent
  IBSR_IBIF = 1;                     // clear IBIF  
  while(IBSR_RXAK);           // wait for "ack" from slave
  IBDR = data;                       // send data to slave
  while (!IBSR_IBIF);            // wait for the data to be sent
  IBSR_IBIF = 1;                   // clear IBIF
  while(IBSR_RXAK);          // wait for "ack" from slave
  IBCR_MS_SL = 0;            // generate STOP condition;