Hi,
Currently I can read data from sensor using I2C protocol, but I cannot write data on sensor.
Here is my write code:
char UCB0_I2C_write ( unsigned char slave_addr,unsigned char _addr, unsigned char val)
{
//WAIT FOR PREVIOUS TRAFFIC TO CLEAR
while ( UCB0STAT & UCBBUSY );
// LOAD THE DEVICE SLAVE ADDRESS
UCB0I2CSA = slave_addr;
// ENABLE TRANSMIT, GENERATE START BIT
UCB0CTL1 |= UCTR + UCTXSTT;
// WAIT FOR FIRST INT
while ( ! ( UCB0IFG & UCTXIFG ) ); // 0000 0010 & 0000 0010 = 0000 0010
// LOAD THE REGISTER ADDRESS
UCB0TXBUF = _addr;
// NOW WAIT FOR START BIT TO COMPLETE
while ( UCB0CTL1 & UCTXSTT );
UCB0TXBUF = val;
while ( UCB0CTL1 & UCTXSTT );
UCB0CTL1 |= UCTXSTP;
return 0;
}
And here is my init code:
void I2Cinit()
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST + UCTR; // Use SMCLK, keep SW reset
UCB0BR0 = 11; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
//UCB0I2CSA = 0xEE; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
//UCB0IFG = 0x10;
//UCB0IE |= UCRXIE+UCALIE+UCTXIE; // Enable RX interrupt
}
I'm using MSP430F5529LP. If I'm not mistaken, USCI automatically reads ack, so if I write two data on TXBUF, the first one being register address and the second being data needed to input into the register, the data should be written, but somehow no data can be written.
Here is how I've tested if data has been written according to my write code:
UCB0_I2C_read(ITG3200_ADDR_AD0_LOW,0x1A,data12,1);
__no_operation();
UCB0_I2C_write(ITG3200_ADDR_AD0_LOW,0x1A,0);
__no_operation();
I've added two break point in _no_operation();. After reading the data, which happens to be 1, I wrote data 0 into that register. However, after re-reading the register, the data shown is still 1.
What is the problem here? Any help would be appreciated.
HeeJun Kim