Part Number:MSP430F5529
Tool/software: TI C/C++ Compiler
Hello all, I'm relatively new to MSP430 and microcontrollers as well as this forum. I'm currently trying to code my MSP430 to read from a TMP275-Q1 temperature sensor via I2C.
But for some reason, the program only works halfway.
The procedure should as follows, Start condition --> Slave address > ACK from slave > DATA > ACK from Master > Data >ACK from Master > Data > Stop bit.
But it for some reason, the Master (MSP430 board) does not send an acknowledge bit back to the slave.
Could someone take a look at my code and tell me where i went wrong?
Your help is much appreciated, thank you for reading and taking your time to help.
//
//
//
// /|\ /|\
// TMP275 10k 10k MSP430F5529
// slave | | master
// ----------------- | | -----------------
// -| SDA|<-|----+->|P3.0/UCB0SDA XIN|-
// | | | | |
// -| | | | XOUT|-
// | SCL|<-+------>|P3.1/UCB0SCL |
// | | | P1.0|--> LED
//
//******************************************************************************
// initializes UCB0 as I2C
// transmitts slave adresse and reads two bytes
#include "msp430f5529.h"
#define Button_S2 0x02 // Push button S2 of lauchpad
unsigned char RXData0 = 0;
unsigned char RXData1 = 0;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1OUT &= ~0x01; // P1.0 = 0
P1DIR |= 0x01; // P1.0 output
P4SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB1CTL1 |= UCSWRST; // Enable SW reset
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK
UCB1BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB1BR1 = 0;
UCB1I2CSA = 0x48; // Slave Address is 048h
UCB1CTL1 &= ~UCTR; // Receive Mode
UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB1IE |= UCRXIE; // Enable RX interrupt
P2DIR |= BIT0; // P2.0 is output an set to "1"
P2OUT |= BIT0;
while (1)
{
unsigned char RXData0;
P1IFG &= ~Button_S2; // clear IFG of Button_S2
while(!(P1IFG & Button_S2)); // wait for push button S2
P1IFG &= ~Button_S2; // clear IFG of Button_S2
//while (UCB1CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB1CTL1 |= UCTXSTT; // I2C start condition
while(UCB1CTL1 & UCTXSTT); // Start condition sent?
while(UCB1IFG & UCRXIFG == 0); // wait for RX IFG
RXData0 = UCB1RXBUF; // read first byte
while(UCB1IFG & UCRXIFG == 0); // wait for RX IFG
RXData1 = UCB1RXBUF; // read second byte
UCB1CTL1 |= UCTXSTP; // I2C stop condition
__no_operation(); // For debugger
}
}