Part Number: TMS320F28069
Other Parts Discussed in Thread: C2000WARE
Hi,
I am trying to read temperature data from MCP9808 via I2C of F28069 MCU. But I could not. Please find the code below. Do you have any suggestion?
// I2C GPIO Configuration /* Enable internal pull-up for the selected pins */ GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for GPIO32 (SDAA) GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for GPIO33 (SCLA) /* Set qualification for selected pins to asynch only */ GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // Asynch input GPIO32 (SDAA) GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3; // Asynch input GPIO33 (SCLA) /* Configure I2C pins using GPIO regs*/ GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // Configure GPIO32 for SDAA operation GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // Configure GPIO33 for SCLA operation
void InitI2C()
{
// Initialize I2C
I2caRegs.I2CMDR.bit.IRS = 0;
I2caRegs.I2CPSC.all = 8; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
return;
}
long GetTemperature(void)
{
long Temperature;
long TempUpperByte;
long TempLowerByte;
I2caRegs.I2CMDR.bit.IRS = 1; // reset I2C
// Make sure I2C is not busy and has stopped
while (I2caRegs.I2CSTR.bit.BB == 1); // busy loop
I2caRegs.I2CSTR.bit.SCD = 1; // Clear the SCD bit (stop condition bit)
while(I2caRegs.I2CMDR.bit.STP == 1); // stop bit loop
//I2caRegs.I2CMDR.all = 0x6620; // start, stop, no rm, reset i2c 01101110 00100000
I2caRegs.I2CMDR.bit.NACKMOD = 0; // NACK mode bit
I2caRegs.I2CMDR.bit.FREE = 1; // Run free I2C when suspended
I2caRegs.I2CMDR.bit.STT = 1; // START condition bit
I2caRegs.I2CMDR.bit.STP = 0; // STOP condition bit
I2caRegs.I2CMDR.bit.MST = 1; // Master mode
I2caRegs.I2CMDR.bit.TRX = 1; // Transmitter mode
I2caRegs.I2CMDR.bit.XA = 0; // 7-bit addressing mode
I2caRegs.I2CMDR.bit.RM = 0; // Nonrepeat mode
I2caRegs.I2CMDR.bit.DLB = 0; // Digital loopback mode is disabled
I2caRegs.I2CMDR.bit.IRS = 1; // The I2C module is enabled
I2caRegs.I2CMDR.bit.STB = 0; // The I2C module is not in the START byte mode
I2caRegs.I2CMDR.bit.FDF = 1; // Free data format mode is disabled
I2caRegs.I2CMDR.bit.BC = 0; // 8 bits per data byte
while(I2caRegs.I2CSTR.bit.XRDY == 0); // Do nothing till bus is free
I2caRegs.I2CMDR.bit.STT = 1; // START condition bit
I2caRegs.I2CDXR = 0x32;
while(I2caRegs.I2CSTR.bit.NACK == 1);
I2caRegs.I2CDXR = 0x05; // register address of the sensor (1 byte)
while(I2caRegs.I2CSTR.bit.NACK == 1);
I2caRegs.I2CMDR.bit.STT = 1; // START condition bit
I2caRegs.I2CDXR = 0x33;
while(I2caRegs.I2CSTR.bit.NACK == 1);
TempUpperByte = I2caRegs.I2CDRR;
while(I2caRegs.I2CSTR.bit.NACK == 1);
I2caRegs.I2CMDR.bit.NACKMOD == 1;
TempLowerByte = I2caRegs.I2CDRR;
while(I2caRegs.I2CSTR.bit.NACK == 0);
I2caRegs.I2CMDR.bit.STP = 1;
Temperature = (TempUpperByte * 16 + TempLowerByte / 16);
return Temperature;
}