Quantcast
Channel: MSP low-power microcontroller forum - Recent Threads
Viewing all articles
Browse latest Browse all 21937

I2C PROBLEM IN MSP430G25553

$
0
0

HI, I`m in a fight with i2c of MSP430G2553, and i saw a lot of answers but big part without a good explanation of problem.  To try solve it and help other users of MSP430G2553 I WILL POST MY QUESTION AND WISH SOMEONE CAN HELP US. 

INFORMATIONS: 

COMPILER CCS 

I`M USING THE MSP430G2553 TO READ THE SENSOR TMP102 AND M24LR, I STARTED USING MSPWARE EXAMPLE IN THIS EXAMPLE msp430g2xx3_usci_i2c_master_tx in  example the IC2 COMMUNICATION USES interrupt mode to works and show the communication between two MSP devices. 

in my project i want to use I2C in polling mode. and generate functions like :

int I2CSendByte(unsigned char I2CSlaveAddress, unsigned char data)

int I2CWriteRegisterByte(unsigned char I2CSlaveAddress, unsigned char Register, unsigned char Data)

I found a document of TI with i2c communication with bq76940 (http://www.ti.com/lit/an/slva626b/slva626b.pdf ) and 

) and in all codes tested  i found the same problem, the flag IFG didnt change the state: Below my code: 

void I2C_Init(void)
{

P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0, P1.6 for SCL and P1.7 for SDA
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0, P1.6 for SCL and P1.7 for SDA

UCB0CTL1 |= UCSWRST; // PUT I2C control in logic reset (register = 0x01)
UCB0CTL0 = UCMODE_3 + UCSYNC; //USMODE_3 = 0x06 = I2C MODE UCSYNC = 0x01 ( I2C + SYNC MODE)
UCB0BR0 = 20;
UCB0BR1 = 0;

UCB0I2CIE = 0;
IE2 &= ~(UCB0TXIE + UCB0RXIE); //disable interrupts
UCB0I2CIE |= UCNACKIE; // Interrupt on slave Nack
UCB0CTL1 |= UCSSEL_2;
UCB0CTL1 &= ~UCSWRST;

}

int I2CSendByte(unsigned char I2CSlaveAddress, unsigned char data)
{
unsigned long int DelayCounter = 0;

UCB0CTL0 |= UCMST;
UCB0I2CSA = I2CSlaveAddress;

UCB0CTL1 |= UCTR; //data in transmit direction
UCB0CTL1 |= UCTXSTT; //Generate Start Condition
//Send Start Byte

while(!(IFG2 & UCB0TXIFG)) //if UCB0TXIFG != 0, wait here
{
DelayCounter ++;
if (UCNACKIE ==1)
{
DelayCounter = 55;
return -2;
}
if (DelayCounter >= DELAY_LIMIT)
break;
}

if (DelayCounter >= DELAY_LIMIT)
return -1;

UCB0TXBUF = data; // send the data

DelayCounter = 0;

while(DelayCounter < DELAY_LIMIT && !(IFG2 & UCB0TXIFG))
{
DelayCounter++;
}

if (DelayCounter >= DELAY_LIMIT)
return -1;

UCB0CTL1 |= UCTXSTP; //send stop bit

DelayCounter = 0;

while(DelayCounter < DELAY_LIMIT && (UCB0CTL1 & UCTXSTP))
{
DelayCounter++;
}

if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred
return -1;
else
return 0;
}

the problem is in line while(!(IFG2 & UCB0TXIFG)) //if UCB0TXIFG != 0, wait here inside of fuction I2CSendbytes. 

Somebody can help solve this problem, and explain me why it happen. 

Thanks for your attention and with this code working I will share same in group to avoid this question again 


Viewing all articles
Browse latest Browse all 21937

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>