Part Number:MSP430FR4133
Hello,
I have interfaced a temperature sensor MCP9808 with MSP430FR4133 trough i2c communication. I am using the following code to read the output voltage from the sensor. But it doesn't seem to be working. Could you please have a look?
tool - IAR Embedded
#define I2C__MSP430FR4133_H_
#include <MSP430.h>
#include <stdint.h>
#include <stdio.h>
#define AMBIENT_TEMPERATURE 0x05
#define DEVICE_ID_REGISTER 0x04
#define MANUFACTURE_ID_REGISTER 0x0054
uint16_t tlen =7;
uint16_t rlen =7;
int *tx =NULL;
int *rx =NULL;
uint8_t addr = 0x18;
volatile unsigned char RXData;
long temp;
void Stop_WD (void);
void Set_I2C(void);
uint16_t I2C_write(void* tx, uint16_t tlen,uint8_t addr);
uint16_t I2C_read(void* rx, uint16_t rlen, uint8_t addr);
int main(void)
{
Stop_WD();
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
Set_I2C();
//Assuming yopu wish to write 8 bits to the sensor with char = 'DDDDDDDD'
char toWrite='DDDDDDDD';
I2C_write(toWrite,8,0*18);
//Assuming you receive 16 bits from the address sent
// the valueis stored in retrieved
string retieved='';
retrieved = I2C_read(_____,16,0*18); // ___ denotes pin address probably.. still looking for this.
__bis_SR_register(GIE);
while (1)
{
__delay_cycles(2000);
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTXSTT; // I2C start condition
__bis_SR_register(LPM0_bits|GIE); // Enter LPM0 w/ interrupt
}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_B0_VECTOR
__interrupt void USCIB0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCIB0_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
{
case USCI_NONE: break; // Vector 0: No interrupts
case USCI_I2C_UCALIFG: break; // Vector 2: ALIFG
case USCI_I2C_UCNACKIFG: // Vector 4: NACKIFG
UCB0CTL1 |= UCTXSTT; // I2C start condition
break;
case USCI_I2C_UCSTTIFG: break; // Vector 6: STTIFG
case USCI_I2C_UCSTPIFG: break; // Vector 8: STPIFG
case USCI_I2C_UCRXIFG3: break; // Vector 10: RXIFG3
case USCI_I2C_UCTXIFG3: break; // Vector 14: TXIFG3
case USCI_I2C_UCRXIFG2: break; // Vector 16: RXIFG2
case USCI_I2C_UCTXIFG2: break; // Vector 18: TXIFG2
case USCI_I2C_UCRXIFG1: break; // Vector 20: RXIFG1
case USCI_I2C_UCTXIFG1: break; // Vector 22: TXIFG1
case USCI_I2C_UCRXIFG0: // Vector 24: RXIFG0
RXData = UCB0RXBUF; // Get RX data
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
break;
case USCI_I2C_UCTXIFG0: break; // Vector 26: TXIFG0
case USCI_I2C_UCBCNTIFG: // Vector 28: BCNTIFG
break;
case USCI_I2C_UCCLTOIFG: break; // Vector 30: clock low timeout
case USCI_I2C_UCBIT9IFG: break; // Vector 32: 9th bit
default: break;
}
}
void Stop_WD (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
}
void Set_I2C(void)
{
P5SEL0 |= BIT2 | BIT3; // I2C pins
UCB0CTLW0 |= UCSWRST; // Software reset enabled
UCB0CTLW0 |= UCMODE_3 | UCMST | UCSYNC; // I2C mode, Master mode, sync
UCB0CTLW1 |= UCASTP_2; // Automatic stop generated
// after UCB0TBCNT is reached
//UCB0I2COA0 = 0x0A | UCOAEN; // own address is 0x0A| enable
UCB0BRW = 0x08; // baudrate = SMCLK / 8
UCB0TBCNT = 0x07; // number of bytes to be received
UCB0I2CSA = 0x18; // Slave address is 0x18
UCB0CTL1 &= ~UCSWRST;
UCB0IE |= UCRXIE | UCNACKIE | UCBCNTIE ;
}
uint16_t I2C_write(void* tx, uint16_t tlen,uint8_t addr)
{
uint16_t r_val = 0;
UCB0I2CSA = addr; // Assing slave address
while ((UCB0IFG & UCSTPIFG)); // Check if Stop condition on
UCB0CTL1 |= UCTR + UCTXSTT; // Start writing through I2C
while (!(UCB0IFG & UCTXIFG)); // Wait until TX buffer ready
for(uint16_t i = 0; i < tlen; i++)
{
UCB0TXBUF = *((uint8_t*)tx + i); // Write string in TX buffer of I2C
while (!(UCB0IFG & UCTXIFG)); // Wait until TX buffer ready
if( i == tlen - 1)
{ // If only one byte left to write
UCB0CTL1 |= UCTXSTP; // I2C stop condition
UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag
}
r_val++; // Increment return value
}
return r_val;
}
uint16_t I2C_read(void* rx, uint16_t rlen, uint8_t addr)
{
uint16_t r_val = 0;
UCB0I2CSA = addr; // Assing slave address
if (rlen != 0)
{
while ((UCB0CTL1 & UCTXSTP)); // Check if Stop condition on
UCB0CTL1 &= ~UCTR; // Set writing bit in register to 0
UCB0CTL1 |= UCTXSTT; // Start reading through I2C
while (!(UCB0CTL1 & UCTXSTT));
if (rlen == 1)
{
while (!(UCB0IFG & UCRXIFG))
{
if((UCB0CTL1 & UCTXSTT)==0)
UCB0CTL1 |= UCTXSTP;
}
while (UCB0CTL1 & UCTXSTP);
*((uint8_t*)rx) = UCB0RXBUF;
return 1;
}
for (uint8_t i = 0; i < rlen-1; i++)
{
while (!(UCB0IFG & UCRXIFG));
*((uint8_t*)rx + i) = UCB0RXBUF;
r_val ++;
}
UCB0CTL1 |= UCTXSTP;
while (UCB0CTL1 & UCTXSTP);
*((uint8_t*)rx+rlen-1) = UCB0RXBUF;
}
return r_val++;
}
Best Regards,
Mr. Shetty