Example #1
0
/**
 * End the transmission and transmit the tx buffer's contents over the bus
 */
void DWire::endTransmission(bool sendStop) {

	if (!*pTxBufferIndex) {
		return;
	}

	// Wait until any ongoing (incoming) transmissions are finished
	//while ( MAP_I2C_isBusBusy(module) == EUSCI_B_I2C_BUS_BUSY )
	//    ;

	this->sendStop = sendStop;

	// Send the start condition and initial byte
	(*pTxBufferSize) = *pTxBufferIndex;
	(*pTxBufferIndex)--;

	// Send the first byte, triggering the TX interrupt
	MAP_I2C_masterSendMultiByteStart(module, pTxBuffer[0]);
}
int main(void)
{
    volatile uint32_t ii;

    /* Disabling the Watchdog */
    MAP_WDT_A_holdTimer();

    /* Select Port 1 for I2C - Set Pin 6, 7 to input Primary Module Function,
     *   (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
     */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
            GPIO_PIN6 + GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
    sendStopCondition = false;
    sendToSlaveAddress2 = false;

    /* Initializing I2C Master to SMCLK at 400kbs with no autostop */
    MAP_I2C_initMaster(EUSCI_B0_MODULE, &i2cConfig);

    /* Specify slave address. For start we will do our first slave address */
    MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_1);

    /* Set Master in receive mode */
    MAP_I2C_setMode(EUSCI_B0_MODULE, EUSCI_B_I2C_TRANSMIT_MODE);

    /* Enable I2C Module to start operations */
    MAP_I2C_enableModule(EUSCI_B0_MODULE);

    /* Enable and clear the interrupt flag */
    MAP_I2C_clearInterruptFlag(EUSCI_B0_MODULE,
            EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);

    //Enable master Receive interrupt
    MAP_I2C_enableInterrupt(EUSCI_B0_MODULE,
            EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);
    MAP_Interrupt_enableInterrupt(INT_EUSCIB0);
    
    while (1)
    {

        /* Load Byte Counter */
        TXByteCtr = 4;

        /* Making sure the last transaction has been completely sent out */
        while (MAP_I2C_masterIsStopSent(EUSCI_B0_MODULE) == EUSCI_B_I2C_SENDING_STOP);

        /* Sending the initial start condition for appropriate
         *   slave addresses */
        if(sendToSlaveAddress2)
        {
            MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_2);
        }
        else
        {
            MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_1);
        }

        MAP_I2C_masterSendMultiByteStart(EUSCI_B0_MODULE, TXData++);

        while(sendStopCondition == false)
        {
            /* Go to sleep while data is being transmitted */
            MAP_Interrupt_enableSleepOnIsrExit();
            MAP_PCM_gotoLPM0InterruptSafe();
        }

        sendStopCondition = false;
    }
}