Working on MSP432 - Part 10 (I2C Communication, Interfacing AT24C16A)

Working on MSP432 - Part 10 (I2C Communication, Interfacing AT24C16A)

I2C is most commonly used in Embedded Systems. I2C can be used to interface sensors, memories like EEPROM, GPIO Expanders, etc. In this article we will see how to interface AT24C16A EEPROM to MSP432 board.

Below image shows MSP432P401R interfacing with the AT24C16A EEPROM.


EEPROM has the pin out shown in below image and the pins A0, A1, A2 are grounded on the board shown above. 


As the address pins are grounded, as per the address configuration shown below. The address shall be 0b01010000 which equals to 0x50


Write Protect pin of AT24C16A is to be grounded to allow writing to the EEPROM. This is controlled by firmware. 

The following is the pin mapping between the MSP432 board and AT24C16A EEPROM.


Following are the configurations done in the firmware:

Defining I2C Address

A macro is defined in the firmware to indicate the address of AT24C16A.

#define SLAVE_ADDR 0x50

---------------------

Using GPIO pin of MSP432 to control EEPROM

GPIO pin in P4 of MSP432 is used for controlling the WP pin of EEPROM. So, the direction of P4.6 is set to output using P4DIR register and Zero is output on the pin using the P4OUT register.

P4DIR |= 0x0020;
P4OUT &= 0xFFDF;

---------------------

Initializing the I2C port of MSP432

The most part in firmware is to initialize the I2C port of MSP432. We will be using the Enhanced Universal Serial communication Interface (eUSCI)  of MSP432 for I2C communication. As indicated in the table above P6.4, P6.5 of MSP432 shall be used for I2C communication. We are using the UCB1SDA/UCB1SCL pins of MSP432 as shown from the snapshot of the MSP432 schematic diagram.


Below are the steps to be carried out.

Configuring the port pins as secondary functionality, other than GPIO

P6->SEL0 |= 0x30;
P6->SEL1 &= ~0x30;

Disable UCB1 by keeping it in RESET until initialization is completed.

EUSCI_B1->CTLW0 |= 1;

Configure the I2C using the CTLW0 register - I2C Mode, Master, SMCLK as CLK, 7-bit address for slave, SYNC Mode

EUSCI_B1->CTLW0 = 0x0F81;

Set the I2C clock frequency using the BRW register, The prescalar value set into this register divides the clock input and generates the desired I2C clock.

EUSCI_B1->BRW = 15;

Once all the above configuration are done, enable the UCB1

EUSCI_B1->CTLW0 &= ~1;

With all the above steps, I2C is ready for communication.

---------------------

Below is the code for writing 1 Byte to EEPROM.

/* setup slave address */
EUSCI_B1->I2CSA = slaveAddr;   

/* enable transmitter */   
EUSCI_B1->CTLW0 |= 0x0010;

/* generate START and send slave address */   
EUSCI_B1->CTLW0 |= 0x0002;       

/* wait till it's ready to transmit */ 
while(!(EUSCI_B1->IFG & 2));      

/* send memory address to slave */
EUSCI_B1->TXBUF = memAddr;       

/* wait till it's ready to transmit */
while(!(EUSCI_B1->IFG & 2));  

/* send data to slave */
EUSCI_B1->TXBUF = *data;    

/* wait till last transmit is done */
while(!(EUSCI_B1->IFG & 2));   

/* send STOP */   
EUSCI_B1->CTLW0 |= 0x0004;    

 /* wait until STOP is sent */    
while(EUSCI_B1->CTLW0 & 4) ;     

Post a Comment

0 Comments