Part Number: MSP430FR2433
Other Parts Discussed in Thread: TMP102, , TMP112
Tool/software: Code Composer Studio
Dear team,
My customer is using MSP430FR2433 with TMP102/TMP112.
The response is normal when temperature changes slowly.
However,there will be no response when the temperature changes drastically for a certain period of time (such as when hot air is blown) and it will be better after a while.
Other MCU (AVR) without this phenomenon. The SDA\SCL pins are all pulled up through a 10K resistor, and the frequency is about 100K.
Please help.
#include <msp430.h>
#include <stdint.h>
#include <string.h>
#include <i2c.h>
#define TIMEOUT 60000
void i2c_init(void){
static char init=0;
if(!init){
P1REN |= BIT2 | BIT3;
P1OUT |= BIT2 | BIT3;
// if the bus is stuck (SDA = low): clock until it recovers
P1DIR |= BIT3;
while(!(P1IN & BIT2)){
P1OUT |= BIT3;
__delay_cycles(8*80);
P1OUT &= ~BIT3;
__delay_cycles(8*80);
}
P1SEL0 |= BIT2 | BIT3; // I2C pins
// Configure USCI_B0 for I2C mode
UCB0CTLW0 |= UCSWRST; // Software reset enabled
UCB0CTLW0 |= UCMODE_3 | UCMST | UCSYNC; // I2C mode, Master mode, sync
// after UCB0TBCNT is reached
UCB0BRW = 10; // baudrate = SMCLK / 10 = ~100K
UCB0CTL1 &= ~UCSWRST;
init = 1;
}
}
int i2c_senddata(char *data, int len){
unsigned int timeout=0;
unsigned int i;
static int dummy=0;
/*if(dummy){
UCB0TXBUF = 0;
timeout = 0;
while(!(UCB0IFG & UCTXIFG0))
if(timeout++ > TIMEOUT)
return -1;
}
if(dummy == 0) dummy = 1;
*/
//__delay_cycles(5000);
for(i=0;i<len;i++){
UCB0TXBUF = data[i];
timeout = 0;
while(!(UCB0IFG & UCTXIFG0))
if(timeout++ > TIMEOUT)
return 3;
}
return 0;
}
int i2c_readdata(char *data, int len){
unsigned int timeout=0;
unsigned int i;
memset(data, 0, len);
for(i=0;i<len;i++){
data[i] = 0;
timeout=0;
while(!(UCB0IFG & UCRXIFG0))
if(timeout++ > TIMEOUT)
return -1;
data[i] = UCB0RXBUF;
if(len > 1)
if(i == len-2)
UCB0CTLW0 |= UCTR | UCTXSTP;
}
//while(!(UCB0IFG & UCTXIFG0));
return 0;
}
int i2c_start(char addr, int read){
unsigned int timeout=0;
UCB0I2CSA = addr;
if(read)
UCB0CTLW0 &= ~UCTR;
else
UCB0CTLW0 |= UCTR;
while (UCB0CTL1 & UCTXSTP);
UCB0CTLW0 |= UCTXSTT; // I2C TX, start condition
while(!(UCB0IFG & UCTXIFG0))
if(timeout++ > TIMEOUT)
return 1;
if(UCB0IFG & UCNACKIFG)
{
UCB0CTL1 |= UCTXSTP; // I2C start condition
UCB0IFG = 0; // Clear All USCI_B0 flags
return 2; // check for NACK
}
if(!read){
UCB0TXBUF = 0;
while(!(UCB0IFG & UCTXIFG0));
}
return 0;
}
int i2c_start2(char addr, int read){
unsigned int timeout=0;
UCB0I2CSA = addr;
if(read)
UCB0CTLW0 &= ~UCTR;
else
UCB0CTLW0 |= UCTR;
while (UCB0CTL1 & UCTXSTP);
UCB0CTLW0 |= UCTXSTT; // I2C TX, start condition
while(!(UCB0IFG & UCTXIFG0))
{
if(timeout++ > TIMEOUT)
return 5;
}
if(UCB0IFG & UCNACKIFG)
{
UCB0CTL1 |= UCTXSTP; // I2C start condition
UCB0IFG = 0; // Clear All USCI_B0 flags
return 6; // check for NACK
}
/*if(!read){
UCB0TXBUF = d;
while(!(UCB0IFG & UCTXIFG0));
}*/
return 0;
}
int i2c_stop(void){
unsigned int timeout=0;
UCB0CTLW0 |= UCTR | UCTXSTP; // I2C TX, stop condition
while(UCB0IFG & UCTXSTP)
if(timeout++ > TIMEOUT)
return 4;
return 0;
}
int read_reg(int address, int reg){
int err;
char buf[2] = {reg};
err = i2c_start2(address, 0);
if(err != 0) return err;
err = i2c_senddata(buf, 1);
if(err != 0) return err;
err = i2c_stop();
if(err != 0) return err;
__delay_cycles(10000); //等待转换完成
err = i2c_start2(address, 1);
if(err != 0) return err;
err = i2c_readdata(buf, 2);
if(err != 0) return err;
return buf[0]<<8|buf[1];
}
void write_reg(int address, int reg, int value){
char buf[2] = {reg, value};
i2c_start2(address, 0);
i2c_senddata(buf, 2);
i2c_stop();
}
void tmp117_init(void){
i2c_init();
}
int tmp117_gettemp(void)
{
unsigned int tmp117_raw = 0;
float t = 0;
tmp117_raw = read_reg(0x48, 0);
tmp117_raw >>= 4;
if(tmp117_raw <= 0x7ff){
t= tmp117_raw * 6.25;
}
if(tmp117_raw>=0xc90) {
tmp117_raw = ~tmp117_raw;
tmp117_raw &=0x0fff;
tmp117_raw +=1;
t= tmp117_raw*6.25;
t =-t;
}
return (int)t;
}


