This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430I2041: want to interface ds3231 with msp430i2041

Part Number: MSP430I2041

Tool/software: Code Composer Studio

I have successfully read the data from the ds3231 .But now i want to send this rtc data on UART but my code got stuck here

//Poll for transmit interrupt flag.     

while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) 

   {       

 ;   

 }

here is my Code:

void main(void)

{
uint8_t i;

uint8_t checksum;

//T_cycle=4098;//8192;// For 500Hz=8192, for 1Khz=4098
T_cycle=8192;
Duty=0;//Initially LAMP should be OFF

//D_cycle=(uint16_t)(40.98*Duty);
D_cycle=(uint16_t)(81.92*(100-Duty)); // (100-duty) as we have used Optocoupler
LAMP_ON=0;

Timer_A_outputPWMParam outputPWMConfig = {
TIMER_A_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
TIMER_A_CLOCKSOURCE_DIVIDER_4, // SMCLK/4 = 4.096 MHz
TIMER_PERIOD, // 8ms
TIMER_A_CAPTURECOMPARE_REGISTER_1, // Output on TA0.1
TIMER_A_OUTPUTMODE_RESET_SET, // Generate PWM
TIMER_DUTY_CYCLE // 75% Duty Cycle
};
Timer_A_initContinuousModeParam continuousModeConfig = {
TIMER_A_CLOCKSOURCE_ACLK, // ACLK Clock Source
TIMER_A_CLOCKSOURCE_DIVIDER_1, // ACLK/1 = 32kHz
TIMER_A_TAIE_INTERRUPT_ENABLE, // Enable Overflow ISR
TIMER_A_DO_CLEAR // Clear Counter
};

EUSCI_B_I2C_initMasterParam i2cConfig =
{
EUSCI_B_I2C_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
4096000, // SMCLK = 4.096MHz
EUSCI_B_I2C_SET_DATA_RATE_400KBPS, // Desired I2C Clock of 400kHz
0, // No byte counter threshold
EUSCI_B_I2C_NO_AUTO_STOP // No Autostop
};


MSP_GUI_System_setup();

// Initialize the EM Lib to default settings and start conversions
EMLibGUIApp_Init();

GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1,GPIO_PIN5,GPIO_SECONDARY_MODULE_FUNCTION);

Timer_A_outputPWM(TIMER_A0_BASE, &outputPWMConfig,T_cycle,D_cycle);

Timer_A_initContinuousMode(TIMER_A1_BASE, &continuousModeConfig);

Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_CONTINUOUS_MODE);

WDT_hold(WDT_BASE);


CS_setupDCO(CS_INTERNAL_RESISTOR);

// Setting SMCLK = DC0 / 4 = 4.096 MHz
CS_initClockSignal(CS_SMCLK, CS_CLOCK_DIVIDER_4);

// Setting P1.6 and P1.7 as I2C pins
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,GPIO_PIN6 | GPIO_PIN7,GPIO_PRIMARY_MODULE_FUNCTION);

// Setting up I2C communication at 400kHz using SMCLK
EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &i2cConfig);

// Settings slave address
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, SLAVE_ADDRESS);

// Enable the module for operation
EUSCI_B_I2C_enable(EUSCI_B0_BASE);

// __delay_cycles(50);
__delay_cycles(10000000);
// Enable needed I2C interrupts
EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0 | EUSCI_B_I2C_NAK_INTERRUPT);
EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0 | EUSCI_B_I2C_NAK_INTERRUPT);

EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_NAK_INTERRUPT);
EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_NAK_INTERRUPT);

__delay_cycles(10000000);
// WriteRTCByte(0x68,0x00,0x00,0x00,0x09,0x03,0x1E,0x0C,0x14);
// WriteRTCByte(0x68,0x00,0x00);
sec1=0;
sec1=ReadRTCByte(0x68,0x01);

// This is the Design Centers Main Loop

while(1){
//sec1=ReadRTCByte(0x68,0x00);

EMLibGUIApp_Engine();

EUSCI_A_UART_transmitData(0x0140,sec1);

}

}

void WriteRTCByte(uint8_t i2cAdd, uint8_t rtcAdd, uint8_t data)
{
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, i2cAdd);
//Set Transmit mode
EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
__delay_cycles(50);
EUSCI_B_I2C_masterSendMultiByteStart(EUSCI_B0_BASE, rtcAdd);
EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE, data);
EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
while(EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE));
}//end void WriteRTCByte(uint8_t i2cAdd, uint8_t rtcAdd, uint8_t data)


uint8_t ReadRTCByte(uint8_t i2cAdd, uint8_t rtcAdd)
{
uint8_t i2cByte;

//Specify slave address
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, i2cAdd);
//Set Transmit mode
EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
__delay_cycles(50);
EUSCI_B_I2C_masterSendMultiByteStart(EUSCI_B0_BASE, rtcAdd);
while(EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE));

//Specify slave address
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, i2cAdd);
//Set receive mode
EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
i2cByte = EUSCI_B_I2C_masterReceiveSingle(EUSCI_B0_BASE);
EUSCI_B_I2C_masterReceiveMultiByteStop(EUSCI_B0_BASE);
__delay_cycles(50);
while(EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE));
return(i2cByte);
}//end uint8_t ReadRTCB

  • Hi Ashwini,

    Thanks for your post and welcome to E2E! When posting your code, it's easier to read if you insert it using the Syntax Highlighter.

    Ashwini Jadhav said:
    I have successfully read the data from the ds3231 .But now i want to send this rtc data on UART but my code got stuck here

    /Poll for transmit interrupt flag.     
    
    while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) 
    
       {       
    
     ;   
    
     }

    What file contains this code? I suspect it's found in an I2C-related file which would indicate an issue with the I2C communication rather than UART communication.

    It also looks like you're adding your code into the EMDC-generated code which is great. However, I'd be careful about adding too many things at once. It may be good to get the I2C communication working before adding it into the EMDC project to make things easier to debug.

    Regards,

    James

  • Hi Sir,

    Yes it is I2c Related code.

    I debug the code & i found that whenever i comment/disable this function

    void EMLibGUIApp_Init(void)
    {
        EM_message_code code;
    
        // Initialize the Energy Measurement Library
        code = EM_configureEnergyMeasurement(&gEmSWConfig, (EM_SW_Lib_Result_Handle *) &gEmSWResult);
    
        if(code != EM_message_code_no_error)
        {
            // Error occurred
            while(1);
        }
    
        // Initialize run time calibration values
        EM_initalizeCalibrationValues((uint8_t*)g_emPhaseCalibration,
                                      (uint8_t*)g_savedEmPhaseCalibration);
    
        // Initialize DC Filter
        EM_initDCFilters(&gEmSWConfig, (EM_SW_Lib_Result_Handle *) &gEmSWResult);
    
        // HAL_ADC_init
        HAL_ADC_init(&halAdcConfig);
    
        // Delay ~200us for 1.2V ref to settle
        __delay_cycles(3200);
    
        // Start the conversion
        HAL_ADC_startConversion(&halAdcConfig);
    
        /* turn on global interrupts */
        __enable_interrupt();
    }

    then code works fine for rtc as well as it is continously sending rtc data on uart.But if i enable this function then not communicating with RTC

  • Hello,

    You're making good progress. When you comment out that portion of code (specifically the last line of code), you're disabling the global interrupts. So, your code probably works fine because you're initiating the I2C communication and using polling and on the UART side, you're just sending data which doesn't require an ISR.

    When you enable this function and thus the global interrupts, other things start happening such as the SD ADC and UART TX ISRs that capture, calculate and send the metrology data to the host. Perhaps there are timing issues or conflicts with the EMDC code if you're using polling versus interrupts.

    Going back to your initial post, I'm not sure why the code was ending up there when it looks like you just set up the I2C but didn't transmit anything. Those I2C commands seem to be commented out but perhaps one or more of those commands were uncommented.

    Regards,

    James