I'm working with a MSP430G2553 and a BMP180 sensor. I have the following function in my program
uint16_t leer_temp_des() {
unsigned char temp[2];
escribir_com_bmp180(CTRL_MEAS, LEER_TEMP);
retardo_ms(RET_LEC_TEMP);
leer_2_bytes_bmp180(OUT_MSB, temp);
printf("t0 %u\n", (uint16_t) temp[0]);
printf("t1 %u\n", (uint16_t) temp[1]);
uint16_t temperatura = ((uint16_t) (temp[0] << 8) | (uint16_t) temp[1]);
printf("t %u\n", temperatura);
//temperatura <<= 8;
//temperatura |= temp[1];
return temperatura;
}
Which calls this other function
void leer_2_bytes_bmp180(uint8_t reg, uint8_t *v) {
inicioI2C(DIR_I2C_BMP180);
envioI2C(reg);
reinicioI2C(DIR_I2C_BMP180, false);
*v = recepcionI2C(false);
*(v + 1) = recepcionI2C(false);
finI2C();
}
I2C code
void inicializarI2C() {
UCB0CTL1 |= UCSWRST;
UCB0CTL0 &= ~(UCA10 | UCSLA10 | UCMM);
UCB0CTL0 |= (UCMST | UCMODE1 | UCMODE0 | UCSYNC);
UCB0CTL1 |= UCSSEL1 | UCSSEL0;
UCB0CTL1 |= UCTR;
UCB0STAT = 0;
UCB0BR0 = 160;
UCB0BR1 = 0;
UCB0I2COA = 0x1A;
UCB0I2CSA = 0x77;
P1DIR |= BIT6;
P1SEL |= BIT6;
P1SEL2 |= BIT6;
P1SEL |= BIT7;
P1SEL2 |= BIT7;
UCB0CTL1 &= ~UCSWRST;
}
void inicioI2C(uint8_t dir) {
UCB0I2CSA = dir;
UCB0CTL1 |= UCTR;
UCB0CTL1 |= UCTXSTT;
while((IFG2 & UCB0TXIFG) == 0) {}
//while((UCB0STAT & UCNACKIFG) == 0) {}
}
void reinicioI2C(uint8_t dir, bool rw) {
UCB0I2CSA = dir;
if(rw) {
UCB0CTL1 |= UCTR;
}
else {
UCB0CTL1 &= ~UCTR;
}
//UCB0CTL1 |= UCTR;
UCB0CTL1 |= UCTXSTT;
while((UCB0CTL1 & UCTXSTT) != 0) {
if((UCB0STAT & UCNACKIFG) != 0) {
break;
/*UCB0STAT &= ~UCNACKIFG;
UCB0CTL1 |= UCTXSTP;
while((UCB0CTL1 & UCTXSTP) != 0) {}*/
}
}
}
uint8_t envioI2C(uint8_t dato) {
//UCB0CTL1 |= UCTR;
//UCB0I2CSA = dir;
//UCB0CTL1 |= UCTXSTT;
UCB0TXBUF = dato;
while((UCB0CTL1 & UCTXSTT) != 0) {}
if((UCB0STAT & UCNACKIFG) != 0) {
UCB0STAT &= ~UCNACKIFG;
UCB0CTL1 |= UCTXSTP;
//while((UCB0CTL1 & UCTXSTP) != 0);
return 0;
}
while((IFG2 & UCB0TXIFG) == 0);
return 1;
//retardo_ms(1);
}
uint8_t recepcionI2C(bool parar) {
//UCB0CTL1 =& ~UCTR;
//UCB0I2CSA = dir;
//UCB0CTL1 |= UCTXSTT;
if(parar) {
if((UCB0STAT & UCNACKIFG) != 0) {
UCB0STAT &= ~UCNACKIFG;
}
UCB0CTL1 |= UCTXSTP;
}
while(!(IFG2 & UCB0RXIFG));
uint8_t dato = UCB0RXBUF;
//while(!(IFG2 & UCB0RXIFG));
if(parar) {
while((UCB0CTL1 & UCTXSTP) != 0) {}
}
return dato;
}
void finI2C() {
if((UCB0STAT & UCNACKIFG) != 0) {
UCB0STAT &= ~UCNACKIFG;
}
UCB0CTL1 |= UCTXSTP;
while((UCB0CTL1 & UCTXSTP) != 0) {}
}
Obviously, temp[0] is supposed to store the most significant byte while temp[1] is going to store the least significant byte but the opposite is happening: temp[0] is saving LSB and temp[1] is saving MSB. I already checked the I2C data with a logic analyzer but the sequence is the opposite of what my function prints.