Part Number: MSP432P401R
Tool/software: Code Composer Studio
Hi, I am new to coding in CCS and using the MSP432p401r. For my final project, I need to interface the TCS34725 color sensor with the MSP432p401r microcontroller. For some reason, my code, which follows an example already posted, does not initialize the I2C or read any values from the sensor. I have posted my code below for you to view. Any help will be appreciated, I'm honestly so lost on how to proceed on this!! PS (Ignore the commented sections, some of that was from previous code I had written but did not work). Thank you so much in advance!
#include "msp.h"
#include "driverlib.h"
#include "stdio.h"
/**
* main.c
*/
//#define MEAS_DELAY 200000;
//#define TEMP_TIMEOUT 1000;
//
//#define TEMP_REG_ADDRESS 0X16;
//#define SLAVE_ADDRESS 0X29;
//// #define SENSOR_ADDRESS 0X52; //this is after the bit is shifted one to the left
//#define CONFIG_REG_ADDRESS 0X0D;
//#define CONFIG_ACTIVATE 0X03;
//#define CONFIG_DEACTIVATE 0X00;
//#define CONFIG_REG_SIZE 1;
//
//const eUSCI_I2C_MasterConfig i2cConfig = {
// EUSCI_B_I2C_CLOCKSOURCE_SMCLK,
// 3000000,
// EUSCI_B_I2C_SET_DATA_RATE_400KBPS,
// 0,
// EUSCI_B_I2C_NO_AUTO_STOP
// };
//
//void main(void)
//{
// WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
//
// MAP_CS_setDCOFrequency(3E+6);
// MAP_CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
// // initialize I2C
// initI2C();
//
// // set read functionality for I2C
// colReadReg(0X29, 0X17, 0X16, 0X19, 0X18, 0X1B, 0X1A, 0, 0, 0);
//
//}
//
//
//void initI2C(void){
// // sets the GPIO pin corresponding to SCL on the sensor as an output
// MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P6, GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);
// // sets the GPIO pin corresponding to SDA on the sensor as an output
// MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P6, GPIO_PIN4, GPIO_PRIMARY_MODULE_FUNCTION);
// // initializes the I2C Master to SMCLK at 400kbps with no autostop
// MAP_I2C_initMaster(EUSCI_B0_BASE, &i2cConfig);
// // set Master in receive mode
// MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
// // enable I2C Module to start operations
// MAP_I2C_enableModule(EUSCI_B0_BASE);
// printf("I've finished initializing the I2C.");
//}
//
//int colReadReg(uint8_t device_add, uint8_t reg_red_high, uint8_t reg_red_low, uint8_t reg_green_high, uint8_t reg_green_low, uint8_t reg_blue_high, uint8_t reg_blue_low, uint16_t *value1, uint16_t *value2, uint16_t *value3){
// // in our case, we have three results (red, green, blue values) and 6 different registers (high/low byte registers) that we need to read/write from
// int result1_high = false; // result from red register storing high byte
// int result1_low = false; // result from red register storing low byte
// int result2_high = false; // result from green register storing high byte
// int result2_low = false; // result from green register storing low byte
// int result3_high = false; // result from blue register storing high byte
// int result3_low = false; // result from blue register storing low byte
// printf("I set all of the result variables.");
// uint8_t red_high = 0; // we are sending one byte of data corresponding to the red high byte storing register over to the I2C
// uint8_t red_low = 0; // we are sending one byte of data corresponding to the red low byte storing register over to the I2C
// uint8_t green_high = 0; // we are sending one byte of data corresponding to the green high byte storing register over to the I2C
// uint8_t green_low = 0; // we are sending one byte of data corresponding to the green low byte storing register over to the I2C
// uint8_t blue_high = 0; // we are sending one byte of data corresponding to the blue high byte storing register over to the I2C
// uint8_t blue_low = 0; // we are sending one byte of data corresponding to the blue low byte storing register over to the I2C
// printf("I set all of the byte variables.");
// int i = 0;
// // sets the I2C address of the device we wish to talk to
// MAP_I2C_setSlaveAddress(EUSCI_B0_BASE,device_add);
// // set write mode to initially write to the sensor with the three different registers
// MAP_I2C_setMode(EUSCI_B0_BASE,EUSCI_B_I2C_TRANSMIT_MODE);
// // write START + address + register to the sensor (for each register)
//
// // red register (high)
// result1_high = MAP_I2C_masterSendSingleByteWithTimeout(EUSCI_B0_BASE, reg_red_high, );
// if (result1_high == false){
// printf("The result1_high variable value is: ", result1_high);
// }
// // Continue only if the write was successful
// if (result1_high == true){
// printf("I've entered the result1_high if condition.");
// // set read mode
// MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
//
// // Restart
// MAP_I2C_masterReceiveStart(EUSCI_B0_BASE);
//
// // Read a single byte of data from the red register (high) of the sensor and stop
// red_high = MAP_I2C_masterReceiveSingle(EUSCI_B0_BASE);
//
//// // Parse the red register (high) data
//// *value1_high = (uint16_t)(red_high[0]);
// }
//
// // red register (low)
// result1_low = MAP_I2C_masterSendSingleByteWithTimeout(EUSCI_B0_BASE, reg_red_low, 5);
//
// // Continue only if the write was successful
// if (result1_low == true){
// // set read mode
// MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
//
// // Restart
// MAP_I2C_masterReceiveStart(EUSCI_B0_BASE);
//
// // Read a single byte of data from the red register (low) of the sensor and stop
// red_low = MAP_I2C_masterReceiveSingle(EUSCI_B0_BASE);
//
// // Parse the red register data (high and low bytes together)
// *value1 = (uint16_t)(red_high << 8 | red_low);
// }
//
// // green register (high)
// result2_high = MAP_I2C_masterSendSingleByteWithTimeout(EUSCI_B0_BASE, reg_green_high, 5);
//
// // Continue only if the write was successful
// if (result2_high == true){
// // set read mode
// MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
//
// // Restart
// MAP_I2C_masterReceiveStart(EUSCI_B0_BASE);
//
// // Read a single byte of data from the green register (high) of the sensor and stop
// green_high = MAP_I2C_masterReceiveSingle(EUSCI_B0_BASE);
//
//// // Parse the red register data
//// *value2_high = (uint16_t)(green_high[0]);
// }
//
// // green register (low)
// result2_low = MAP_I2C_masterSendSingleByteWithTimeout(EUSCI_B0_BASE, reg_green_low, 5);
//
// // Continue only if the write was successful
// if (result2_low == true){
//
// // set read mode
// MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
//
// // Restart
// MAP_I2C_masterReceiveStart(EUSCI_B0_BASE);
//
// // Read a single byte of data from the green register (high) of the sensor and stop
// green_low = MAP_I2C_masterReceiveSingle(EUSCI_B0_BASE);
//
// // Parse the green register data (high and low bytes together)
// *value2 = (uint16_t)(green_high << 8 | green_low);
// }
//
// // blue register (high)
// result3_high = MAP_I2C_masterSendSingleByteWithTimeout(EUSCI_B0_BASE, reg_blue_high, 5);
//
// // Continue only if the write was successful
// if (result3_high == true){
//
// // set read mode
// MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
//
// // Restart
// MAP_I2C_masterReceiveStart(EUSCI_B0_BASE);
//
// // Read a single byte of data from the green register (high) of the sensor and stop
// blue_high = MAP_I2C_masterReceiveSingle(EUSCI_B0_BASE);
//
//// // Parse the red register data
//// *value3_high = (uint16_t)(blue_high[0]);
// }
//
// // blue register (low)
// result3_low = MAP_I2C_masterSendSingleByteWithTimeout(EUSCI_B0_BASE, reg_blue_low, 5);
//
// // Continue only if the write was successful
// if (result3_low == true){
//
// // set read mode
// MAP_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
//
// // Restart
// MAP_I2C_masterReceiveStart(EUSCI_B0_BASE);
//
// // Read a single byte of data from the green register (high) of the sensor and stop
// blue_low = MAP_I2C_masterReceiveSingle(EUSCI_B0_BASE);
//
// // Parse the blue register data (high and low bytes together)
// *value3 = (uint16_t)(blue_high << 8 | blue_low);
// }
//
// return result1_high, result1_low, result2_high, result2_low, result3_high, result3_low;
// }
//
//
#define MEAS_DELAY 200000 // delay between temp measurements
#define TEMP_TIMEOUT 1000 // timeout in ms for I2C calls
// For TCS34725
#define RED_REG_ADDR_LOW 0x16
#define CLEAR_REG_ADDR_LOW 0x14
#define GREEN_REG_ADDR_LOW 0x18
#define BLUE_REG_ADDR_LOW 0x1A
#define SLAVE_ADDR 0x29
#define ID_REG_ADDR 0x92
//#define CONFIG_REG_ADDR 0x0D
#define ENABLE_REG_ADDR 0x80
//#define CONTROL_REG_ADDR 0x8F
//#define RGBC_TIMING_REG_ADDR 0x81
//#define GAIN_1 0x00
//#define TIMING 0x00
//#define ID_REG_ADDR 0x92
#define CONFIG_ACTIVATE 0x03
#define CONFIG_DEACTIVATE 0x00
#define CONFIG_REG_SIZE 1
/* Byte swap of 16-bit register value */
// These macros swap the high and low bytes of 16-bit argument v
#define HI_UINT16(a) (((a) >> 8) & 0xFF)
#define LO_UINT16(a) ((a) & 0xFF)
#define SWAP(v) ( (LO_UINT16(v) << 8) | HI_UINT16(v) )
int configRegWrite(uint8_t ui8Addr, uint8_t ui8Reg, uint8_t *Data, uint8_t ui8ByteCount) ;
int tempReadReg (uint8_t address, uint8_t reg, uint16_t *value) ;
void initializeI2C(void) ;
/* I2C Master Configuration Struct */
volatile eUSCI_I2C_MasterConfig i2cConfig =
{
EUSCI_B_I2C_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
3000000, // Use clock of 3MHz
EUSCI_B_I2C_SET_DATA_RATE_400KBPS, // Desired I2C Clock of 400khz
0, // No byte counter threshold
EUSCI_B_I2C_NO_AUTO_STOP
};
void main(void)
{
uint8_t red = 0, clear = 0, green = 0, blue = 0 ;
uint8_t id_correct = 0;
float tObjAmb, tObjTemp ;
int success = 0 ;
int id_success = 0;
int i = 0 ;
//Stop watchdog timer
MAP_WDT_A_holdTimer();
//Disabling interrupts
MAP_Interrupt_disableMaster();
// This is so that LED blinks every time we take a measurement
// initialize P1.0 and make it output (P1.0 built-in LED1)
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1, GPIO_PIN0,GPIO_PRIMARY_MODULE_FUNCTION);
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
/* Initialize i2c */
initializeI2C();
/* Initialize temperature sensor */
uint16_t writeVal = CONFIG_ACTIVATE ;
//uint16_t writeVal2 = GAIN_1;
//uint16_t writeVal3 = TIMING;
// writeVal = SWAP(writeVal);
configRegWrite(SLAVE_ADDR, ENABLE_REG_ADDR, (uint8_t*)&writeVal, CONFIG_REG_SIZE);
//configRegWrite(SLAVE_ADDR, CONTROL_REG_ADDR, (uint8_t)*&writeVal2, CONFIG_REG_SIZE);
//configRegWrite(SLAVE_ADDR, RGBC_TIMING_REG_ADDR,(uint8_t)*&writeVal3, CONFIG_REG_SIZE);
//SYSTEM CLOCK SETUP
// Set DCO clock source frequency
MAP_CS_setDCOFrequency(3E+6);
// Tie SMCLK to DC
MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
while(1)
{
// Delay between conversions
for(i = 0; i < MEAS_DELAY; i++){}
// Read temp
MAP_Interrupt_disableMaster();
// read the ID register to check if the value from it was 0x44, this means we are successfully communicating with the device
//id_success = tempReadReg(SLAVE_ADDR, ID_REG_ADDR, &id_correct);
//if (id_correct == 0x44)
//{
//printf("ID read successfully");
//}
// first read the low byte
red = tempReadReg(SLAVE_ADDR,RED_REG_ADDR_LOW,&red) ;
printf("Success: %d\n", success);
MAP_Interrupt_enableMaster();
printf("%f\r\n", red);
//clear = tempReadReg(SLAVE_ADDR,CLEAR_REG_ADDR_LOW, &clear);
//printf("%f\r\n", clear);
//
//green = tempReadReg(SLAVE_ADDR,GREEN_REG_ADDR_LOW, &green);
//printf("%f\r\n", green);
//
//blue = tempReadReg(SLAVE_ADDR,BLUE_REG_ADDR_LOW, &blue);
//printf("%f\r\n", blue);
}
}
int configRegWrite(uint8_t ui8Addr, uint8_t ui8Reg, uint8_t *Data, uint8_t ui8ByteCount)
{
int i = 0 ;
/* Load device slave address */
MAP_I2C_setSlaveAddress(EUSCI_B1_BASE, ui8Addr);
// Set write mode.
MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
/* Send start bit and register */
MAP_I2C_masterSendMultiByteStart(EUSCI_B1_BASE, ui8Reg);
// Delay
for(i = 0; i < 2000; i++){}
/* Now write one or more data bytes */
while(1)
{
// Delay
for(i = 0; i < 2000; i++){}
/* While we still have data to send */
if(ui8ByteCount > 0 )
{
MAP_I2C_masterSendMultiByteNext(EUSCI_B1_BASE, *Data++);
}
else
{
MAP_I2C_masterSendMultiByteStop(EUSCI_B1_BASE);
MAP_I2C_clearInterruptFlag(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
return(true);
}
ui8ByteCount--;
}
}
int tempReadReg (uint8_t address, uint8_t reg, uint16_t *value)
{
int result = false; // return value
uint8_t temp[2] = {0,0}; // data to send over I2C
int i = 0 ;
GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0); // toggle LED1
// Set the I2C address of the device we wish to talk to.
MAP_I2C_setSlaveAddress(EUSCI_B1_BASE, address);
// Set write mode.
MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
// Write START + address + register to the sensor.
result = MAP_I2C_masterSendSingleByteWithTimeout(EUSCI_B1_BASE, reg, TEMP_TIMEOUT);
// Delay (needed to get reliable data from MCP9808)
//for(i = 0; i < 1000; i++){}
// Continue only if the write was successful.
if (result == true)
{
// Set read mode.
MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_RECEIVE_MODE);
// RESTART.
MAP_I2C_masterReceiveStart(EUSCI_B1_BASE);
// Read two bytes from the sensor, STOP.
temp[0] = MAP_I2C_masterReceiveSingle(EUSCI_B1_BASE);
result = MAP_I2C_masterReceiveMultiByteFinishWithTimeout(EUSCI_B1_BASE, &(temp[1]), TEMP_TIMEOUT);
// Parse the sensor's data.
*value = (uint16_t)(temp[0] << 8 | temp[1]);
}
GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0); // toggle LED1
return result;
}
void initializeI2C(void)
{
/* Select I2C function for I2C_SCL & I2C_SDA */
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1, GPIO_PIN7,
GPIO_PRIMARY_MODULE_FUNCTION);
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1, GPIO_PIN6,
GPIO_PRIMARY_MODULE_FUNCTION);
/* Initializing I2C Master to SMCLK at 400kbs with no-autostop */
MAP_I2C_initMaster(EUSCI_B1_BASE, (const eUSCI_I2C_MasterConfig *)&i2cConfig);
/* Enable I2C Module to start operations */
MAP_I2C_enableModule(EUSCI_B1_BASE);
printf("Initialized the I2C right now.");
}