Part Number: MSP430F5529
Tool/software:
Hello guys im trying to communicate with the sensor BH1750FVI with a MSP430F5529 but im facing an issue of NACK from part of the slave when im trying to read data. And getting back null data.
The address pin of the sensor is connected to vcc -> 0x5C
Also inside the while loop my code is getting stuck in:
And not moving on for the next message.
The code is shown bellow, im also using pull-up resistors of 5k ohm and a frequency of 10KHz just to try to get it to work right now.
#include "driverlib.h"
#include "intrinsics.h"
#include "msp430f5529.h"
#include "msp430f5xx_6xxgeneric.h"
//#include <cstdint>
uint8_t TransmitBuffer;
uint8_t* on = 0x01; // turn on
uint8_t* data = 0x20; // get data
uint8_t Data_In[2] = {0, 0};
int bytes = 0;
typedef enum I2C_ModeEnum{
SENSOR_ACTIVATE,
TX_DATA_MODE
} I2C_Mode;
I2C_Mode MasterMode;
void sensorON(void);
void main (void)
{
WDTCTL = WDTPW | WDTHOLD;
P4DIR |= BIT7;
P1DIR |= BIT0;
UCB1CTL1 |= UCSWRST; // put in SW reset
UCB1CTL1 |= UCSSEL_3;
UCB1BR0 = 100; // fSCL = SMCLK/10 = ~100kHz
UCB1BR1 = 0;
UCB1CTL0 |= UCMODE_3 + UCSYNC; // I2C mode
UCB1CTL0 |= UCMST; // Master mode
UCB1I2CSA = 0x5C; // Slave Address is 5C -> address pin connected to VCC
P4SEL &= ~BIT1;
P4SEL &= ~BIT2;
P4SEL |= BIT1 + BIT2; // PIN 4.1, 4.2 for communication
UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB1IE |= UCNACKIE + UCTXIE + UCRXIE;
// Communication
while (1)
{
sensorON();
// Transmit register Address with WRITE message
MasterMode = TX_DATA_MODE;
UCB1IFG &= ~(UCTXIFG + UCRXIFG); // Clear any pending interrupts
UCB1IE &= ~UCRXIE; // Disable RX interrupt
UCB1IE |= UCTXIE; // Enable TX interrupt
UCB1CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
bytes = 2;
__bis_SR_register(LPM0_bits + GIE);
__no_operation();
if (Data_In[0] != 0 | Data_In[1] != 0)
{
P4OUT |= BIT7;
}
}
}
void sensorON(void)
{
// Transmit register Address with WRITE message
UCB1IFG &= ~(UCTXIFG + UCRXIFG); // Clear any pending interrupts
UCB1IE &= ~UCRXIE; // Disable RX interrupt
UCB1IE |= UCTXIE; // Enable TX interrupt
UCB1CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
MasterMode = SENSOR_ACTIVATE;
__bis_SR_register(LPM0_bits + GIE);
__no_operation();
}
//-------------------------------------------------------------------------------
//-- ISR
#pragma vector=USCI_B1_VECTOR
__interrupt void USCI_B1_ISR(void)
{
switch(UCB1IV)
{
case USCI_NONE:break; // Vector 0 - no interrupt
case USCI_I2C_UCALIFG:break; // Interrupt Vector: I2C Mode: UCALIFG
case USCI_I2C_UCNACKIFG:
P1OUT |= BIT0;
break; // Interrupt Vector: I2C Mode: UCNACKIFG
case USCI_I2C_UCSTTIFG:break; // Interrupt Vector: I2C Mode: UCSTTIFG
case USCI_I2C_UCSTPIFG:break; // Interrupt Vector: I2C Mode: UCSTPIFG
case USCI_I2C_UCRXIFG:
if (bytes)
{
Data_In[bytes-1] = UCB1RXBUF;
bytes--;
}
if (bytes == 1)
{
UCB1CTL1 |= UCTXSTP;
}
else if (bytes == 0)
{
UCB1IE &= ~UCRXIE;
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
break;
case USCI_I2C_UCTXIFG:
switch (MasterMode)
{
case SENSOR_ACTIVATE:
//UCB1CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
UCB1TXBUF = 0x01; // turn on sensor
UCB1CTL1 |= UCTXSTP;
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
//while (!(UCB1IFG & UCSTPIFG)); // Wait until STOP condition interrupt flag is set
//UCB1IFG &= ~UCSTPIFG; // Clear it
break;
case TX_DATA_MODE:
UCB1TXBUF = 0x10; // read command
UCB1IE |= UCRXIE; // Enable RX interrupt
UCB1IE &= ~UCTXIE; // Disable TX interrupt
UCB1CTL1 &= ~UCTR; // Switch to receiver
UCB1CTL1 |= UCTXSTT; // Send repeated start
if (bytes == 1)
{
//Must send stop since this is the N-1 byte
while((UCB1CTL1 & UCTXSTT));
UCB1CTL1 |= UCTXSTP; // Send stop condition
}
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
break;
default:
__no_operation();
break;
}
break;
default:
break;
}
}
I provide some pictures i took from the oscilloscope.




