Quantcast
Channel: Forum - Recent Threads
Viewing all articles
Browse latest Browse all 1583

Can't get I2C to work

$
0
0

Hi,

I am trying to control a digital pot via I2C, but I can't get it to work. I have added the code for initialization and transmission below. The clock for I2C channel 1 is enabled and the SCL and SDA pins are initialized as direct alternative function. The problem is that after setting the ST bit (issuing a start condition), the TDRE bit will not be set (as well as BBSY and START). Does someone have an idea about what I am doing wrong here?

Thanks in advance!



Code for initialization:

// Initial settings
RIIC1.RIICnCR1.UINT8[0] &= ~RIICn_RIICnCR1_ICE; // Clear ICE: output pins not driven
RIIC1.RIICnCR1.UINT8[0] |= RIICn_RIICnCR1_IICRST; // Set IICRST: RIIC reset
RIIC1.RIICnCR1.UINT8[0] |= RIICn_RIICnCR1_ICE; // Set ICE: Internal reset

// Bit rate
RIIC1.RIICnMR1.UINT8[0] |= 0x10; // P0/2
RIIC1.RIICnBRL.UINT8[0] = 18; // TODO
RIIC1.RIICnBRH.UINT8[0] = 19; // TODO
RIIC1.RIICnFER.UINT8[0] |= RIICn_RIICnFER_NACKE; // Everything else default

// Set interrupts
RIIC1.RIICnIER.UINT32 = 0; // TODO

// Release reset
RIIC1.RIICnCR1.UINT8[0] &= ~RIICn_RIICnCR1_IICRST;


Code for transmitting:

bool hasError = false;

// Wait for the bus to be free
while ((RIIC1.RIICnCR2.UINT8[0] & RIICn_RIICnCR2_BBSY) > 0);

// Issue start condition
RIIC1.RIICnCR2.UINT8[0] |= RIICn_RIICnCR2_ST;

// Wait for transmit buffer empty
while (!hasError)
{
  if ((RIIC1.RIICnSR2.UINT8[0] & RIICn_RIICnSR2_NACKF) > 0)
  {
    hasError = true;
  }
  else if ((RIIC1.RIICnSR2.UINT8[0] & RIICn_RIICnSR2_TDRE) > 0)
  {
    break;
  }
}

if (!hasError)
{
  // Transmit the slave address and write bit
  RIIC1.RIICnDRT.UINT8[0] = (POT_A_ADDRESS << 1);
}

// Wait for transmit buffer empty
while (!hasError)
{
  if ((RIIC1.RIICnSR2.UINT8[0] & RIICn_RIICnSR2_NACKF) > 0)
  {
    hasError = true;
  }
  else if ((RIIC1.RIICnSR2.UINT8[0] & RIICn_RIICnSR2_TDRE) > 0)
  {
    break;
  }
}

if (!hasError)
{
  // Transmit the command
  RIIC1.RIICnDRT.UINT8[0] = 0x00;
}

// Wait for transmit buffer empty
while (!hasError)
{
  if ((RIIC1.RIICnSR2.UINT8[0] & RIICn_RIICnSR2_NACKF) > 0)
  {
    hasError = true;
  }
  else if ((RIIC1.RIICnSR2.UINT8[0] & RIICn_RIICnSR2_TDRE) > 0)
  {
    break;
  }
}

if (!hasError)
{
  // Transmit the value
  RIIC1.RIICnDRT.UINT8[0] = value;

  // Wait for data transmitted
  while ((RIIC1.RIICnSR2.UINT8[0] & RIICn_RIICnSR2_TEND) == 0);
}

// Reset stop detection flag
RIIC1.RIICnSR2.UINT32 &= ~RIICn_RIICnSR2_STOP;

// Issue stop condition
RIIC1.RIICnCR2.UINT32 |= RIICn_RIICnCR2_SP;

// Wait for stopped
while ((RIIC1.RIICnSR2.UINT32 & RIICn_RIICnSR2_STOP) == 0);

// Reset registers
RIIC1.RIICnSR2.UINT32 &= ~RIICn_RIICnSR2_NACKF;
RIIC1.RIICnSR2.UINT32 &= ~RIICn_RIICnSR2_STOP;


Viewing all articles
Browse latest Browse all 1583

Trending Articles