Hello all. This is my first time posting in the forums here, but I did some digging around prior to this to try to make sure I wasn't being redundant in the issue I'm having. I saw an almost identical issue here, but it appears the initial problem was resolved by the poster and the solution to the initial question was not part of the discussion:
I'm attempting to use the msp430FR5969 to read temperature data from an mcp9808. I have constructed the following code based on as much information as I could gather regarding driverlib and the mcp9808. When I debug the code in CC, the initTempSensor() function runs all the way through, but the readTempsensor() method hangs on waiting for the interrupt flag within EUSCI_B_I2C_masterReceiveSingleByte() in the driverlib:
while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG))
My question is this; I have the interrupt enabled, I have configured and enabled the msp430 as a master device, I have enabled the secondary module function on the i2c pins; I am I doing any of these things improperly, or is there something obvious I missed that I need to add or take out of the code below to allow the flag to trigger?
Code:
#include "TempSensor.h" #include "main.h" #include "driverlib.h" #include "math.h" #include "time.h" volatile unsigned char * tempUnit = &BAKMEM4_H; // Temperature Unit int degC; // Celcius measurement const unsigned char TXData[] = { 0xA1, 0xB1, 0xC1, 0xD1 }; volatile unsigned char TXByteCtr; void initTempSensor() { GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P1, GPIO_PIN6 + GPIO_PIN7, GPIO_SECONDARY_MODULE_FUNCTION ); EUSCI_B_I2C_initMasterParam param={}; param.selectClockSource=EUSCI_B_I2C_CLOCKSOURCE_SMCLK; param.dataRate=EUSCI_B_I2C_SET_DATA_RATE_100KBPS, param.i2cClk=CS_getSMCLK(); param.byteCounterThreshold=1; param.autoSTOPGeneration=EUSCI_B_I2C_NO_AUTO_STOP; EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, ¶m); // Specify slave address EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, 0x18); // Set in transmit mode EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE); //enable interrupt EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_STOP_INTERRUPT); //Enable USCI_B_I2C_Module to start operations EUSCI_B_I2C_enable(EUSCI_B0_BASE); // Delay if bus is busy while(EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE)); // Send single byte data specifying the register to read from on the mcp9808 EUSCI_B_I2C_masterSendSingleByte(EUSCI_B0_BASE, 0x05); } int readTempSensor() { // Set in receive mode EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE); //Enable USCI_B_I2C_Module to start operations EUSCI_B_I2C_enable(EUSCI_B0_BASE); unsigned char tempHighByte, tempLowByte; //The mcp9808 sends a high and low byte back consecutively with the temperature data tempHighByte=EUSCI_B_I2C_masterReceiveSingleByte(EUSCI_B0_BASE); while(EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE)); tempLowByte=EUSCI_B_I2C_masterReceiveSingleByte(EUSCI_B0_BASE); while(EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE)); __delay_cycles(100); return tempLowByte | (tempHighByte << 8);
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector = USCI_B0_VECTOR __interrupt void USCI_B0_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCI_B0_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 UCB0CTLW0 |= UCTXSTT; // resend start if NACK 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 12: TXIFG3 case USCI_I2C_UCRXIFG2: break; // Vector 14: RXIFG2 case USCI_I2C_UCTXIFG2: break; // Vector 16: TXIFG2 case USCI_I2C_UCRXIFG1: break; // Vector 18: RXIFG1 case USCI_I2C_UCTXIFG1: break; // Vector 20: TXIFG1 case USCI_I2C_UCRXIFG0: break; // Vector 22: RXIFG0 case USCI_I2C_UCTXIFG0: // Vector 24: TXIFG0 if (TXByteCtr) // Check TX byte counter { //UCB0TXBUF = TXData[SlaveFlag]; // Load TX buffer TXByteCtr--; // Decrement TX byte counter } else { UCB0CTLW0 |= UCTXSTP; // I2C stop condition UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag __bic_SR_register_on_exit(LPM0_bits); // Exit LPM0 } break; default: break; } }