/* Watchdog Timer interrupt service routine. The function prototype * tells the compiler that this will service the Watchdog Timer, and * then the function follows. * */ void run() { //Transmit process Setup_TX(ADXL_345); TransmitOne(0x32); // Request Data from ADXL345 while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent //Receive process Setup_RX(ADXL_345); Receive(); while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent acc[0] = ((((int)RxBuffer[1]) << 8) | RxBuffer[0]) << 6; acc[1] = ((((int)RxBuffer[3]) << 8) | RxBuffer[2]) << 6; acc[2] = ((((int)RxBuffer[5]) << 8) | RxBuffer[4]) << 6; // now You have XYZ axis reading in x1,x2,x3 variable....Bingo... you can play with it as you like.... // Below if sense x and y angle and Red led is on if its more then 45 or less then -45... // you can put your own condition here... if ((acc[0] > 0x2000) || (acc[1] > 0x2000) || (acc[0] < -0x2000) || (acc[1] < -0x2000)) { P1OUT |= BIT0; // red led on } else { P1OUT &= ~BIT0; // red led off } Setup_UART(); /* UARTSendArray("sample\n", 7); */ /* UARTSendInt(acc[0]); */ /* UARTSendInt(acc[1]); */ /* UARTSendInt(acc[2]); */ hmm(); }
/******************************************************************** * Function: unsigned char READ_I2C(unsigned char Slave_Add,unsigned char Add) * * PreCondition: None * * Input: I2C slave address, Address from where data is to be read * * Output: Returns a byte as data read from I2C slave * * Side Effects: None * * Overview: Reads a byte from I2C slave from specified address *******************************************************************/ unsigned char READ_I2C(unsigned char Slave_Add,unsigned char Add) { Setup_TX(Slave_Add); RPT_Flag = 1; PTxData = &Add; // TX array start address TXByteCtr = 1; // Load TX byte counter while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent RPT_Flag = 0; Setup_RX(Slave_Add); PRxData = &RxBuffer; // Start of RX buffer RXByteCtr = 1; // Load RX byte counter while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTXSTT; // I2C start condition while (UCB0CTL1 & UCTXSTT); // Start condition sent? UCB0CTL1 |= UCTXSTP; // No Repeated Start: stop condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent return RxBuffer; }
void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT P3SEL |= 0x06; // Assign I2C pins to USCI_B0 while(1) { //Transmit process Setup_TX(); RPT_Flag = 1; Transmit(); while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent //Receive process Setup_RX(); Receive(); while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent } }
/******************************************************************** * Function: unsigned char READ_I2C(unsigned char Slave_Add,unsigned char Add) * * PreCondition: None * * Input: I2C slave address, Address from where data is to be read * * Output: Returns a byte as data read from I2C slave * * Side Effects: None * * Overview: Reads a byte from I2C slave from specified address *******************************************************************/ unsigned char READ_I2C(unsigned char Slave_Add,unsigned char Add) { Setup_TX(Slave_Add); RPT_Flag = 1; PTxData = &Add; // TX array start address TXByteCtr = 1; // Load TX byte counter while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent RPT_Flag = 0; Setup_RX(Slave_Add); PRxData = &RxBuffer; // Start of RX buffer RXByteCtr = 1; // Load RX byte counter while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTXSTT; // I2C start condition while (UCB0CTL1 & UCTXSTT); // Start condition sent? UCB0CTL1 |= UCTXSTP; // No Repeated Start: stop condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent return RxBuffer; } ///******************************************************************** // * Function: void WriteI2CMultipleByte(unsigned char Slave_Add,unsigned char Add,unsigned char Cnt_Byte, unsigned char *ptrToData) // * // * PreCondition: None // * // * Input: I2C slave address, Address from where data is to be written, Number of bytes to be written, // * Address of the buffer from where data is to be read and written to I2C slave device // * // * Output: None // * // * Side Effects: None // * // * Overview: Writes more than a byte to I2C slave device // *******************************************************************/ void WriteI2CMultipleByte(unsigned char Slave_Add,unsigned char Add,unsigned char Cnt_Byte, unsigned char *ptrToData) { Setup_TX(Slave_Add); RPT_Flag = 0; TxBuffer[0] = Add; memcpy(TxBuffer+1, ptrToData, Cnt_Byte*sizeof(unsigned char)); PTxData = TxBuffer; // TX array start address TXByteCtr = Cnt_Byte+1; // Load TX byte counter while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent //Issue stop condition } ///******************************************************************** // * Function: void ReadI2CMultipleByte(unsigned char Slave_Add,unsigned char Add,unsigned char Cnt_Byte, unsigned char *ptrToData) // * // * PreCondition: None // * // * Input: I2C slave address, Address from where data is to be read, Number of bytes to be read, // * Address of the buffer from where the read data(from I2C slave device) is to be written // * // * Output: None // * // * Side Effects: None // * // * Overview: Reads more than a byte from I2C slave device // *******************************************************************/ void ReadI2CMultipleByte(unsigned char Slave_Add,unsigned char Add,unsigned char Cnt_Byte, unsigned char *ptrToData) { Setup_TX(Slave_Add); RPT_Flag = 1; PTxData = &Add; // TX array start address TXByteCtr = 1; // Load TX byte counter while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent Setup_RX(Slave_Add); RPT_Flag = 1; PRxData = ptrToData; // Start of RX buffer RXByteCtr = Cnt_Byte - 1; while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTXSTT; // I2C start condition while (UCB0CTL1 & UCTXSTT); // Start condition sent? __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent }
void USCI_SLAVE_SETUP(void){ P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0 P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0 Setup_RX(); Receive(); }
void USCI_SLAVE_SETUP(void){ P3SEL |= 0x06; // Assign I2C pins to USCI_B0 Setup_RX(); Receive(); }