Hi,
I am having some issues with the basic stuff of I2C communication protocol on the MSP432. I am running a code in CCS for a basic master mode write function. This is my code at the moment:
MAIN.C
#include <I2CComm.h>
#include "msp.h"
#include <stdio.h>
void delayMs(int t);
void main(void){
I2C1_init();
while (1){
I2C1_Write(0x21, 0x14, 0x08);
delayMs(1000);
}
}
void delayMs(int t) {
int i, j;
for (j = 0; j < t; j++)
for (i = 750; i > 0; i--); /* Delay */
}
I2CComm.c
#include <I2CComm.h>
#include "msp.h"
#include <stdio.h>
/* initialize I2C to 100 kHz at 3 MHz system clock */
void I2C1_init()
{
EUSCI_B0->CTLW0 = 0x0001; // disable UCB1 for configuration
EUSCI_B0->CTLW0 = 0x0F81;
/* Bit15 : Own Address of 7-bit (0)
Bit 14 : Slave Address of 7-bit (0)
Bit 13: Single Master Environment (0)
Bit 12 : Reserved (0)
Bit 11 : Master Mode Select (1)
Bit 10-9 : EUSCI Mode (11) <- I2C Mode
Bit 8 : Synchronous Mode Enable (1)
Bit 7-6 : EUSCI Clock Source (10)
Bit 5 : Transmit ACK (0)
Bit 4 : Transmitter / Receiver (0)
Bit 3 : Transmit NACK (0)
Bit 2 : Transmit STOP (0)
Bit 1 : Transmit START (0)
Bit 0 : Software Reset Enable (1)
In hex is represented by "0FC1"
P1.7 -> SCL | P1.6 -> SDA*/
EUSCI_B0->BRW = 30; // 3 MHz / 30 = 100 kHz
P1->SEL0 |= 0xC0; /* P1.7 SCLK / P1.6 SDA */
P1->SEL1 &= ~0xC0;
EUSCI_B0->CTLW0 &= ~0x01; // enable UCB1 after configuration
printf("I2C Initialized.\n");
}
/* Write One Byte of Data */
void I2C1_Write(int slaveAddr, unsigned char reg, unsigned char data)
{
EUSCI_B0->I2CSA = slaveAddr; // setup slave address
EUSCI_B0->CTLW0 |= 0x0012; // Enable Transmitter and Generate START bit
while(!(EUSCI_B0->IFG & 0x02)); // Wait until it is ready to transmit <--- It gets stuck in this while loop
EUSCI_B0->TXBUF = reg; // Send Data to slave
while(!(EUSCI_B0->IFG & 0x02)); // Wait until it is ready to transmit
EUSCI_B0->TXBUF = data; // Send Data to slave
while(!(EUSCI_B0->IFG & 0x02)); // Wait until it is ready to transmit
EUSCI_B0->CTLW0 |= 0x04; // Generate STOP bit
while(EUSCI_B0->CTLW0 & 4); // Wait until STOP bit is sent
}
When i run the code, it gets stuck on the Interrupt Flag while loop as indicated on the code. If i replace that line with:
while(EUSCI_B0->CTLW0 & 2); // Wait until STOP bit is sent
it also gets stuck on that specific while loop. I have a oscilloscope connected to pins 1.6 and 1.7 and while the code is running i dont see any change (it is flat at ~0V). What am i doing wrong? Thank you