Ejemplo n.º 1
0
int main(void)
{
  // LED
  P1DIR |= BIT0;
  /* P1OUT |= BIT0; */

  // UART
  BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
  DCOCTL = CALDCO_1MHZ; // Set DCO to 1MHz

  // Configure hardware UART
  P1SEL |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
  P1SEL2 |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
    
  Setup_UART();
  UARTSendArray("Hello\n", 6);
  __delay_cycles(1000);

  // ADXL345
  P1SEL  |= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0
  P1SEL2 |= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0

  // Init sequence for ADXL345
  //Transmit process
  Setup_TX(ADXL_345);
  Transmit(0x2D,0x00);                    // STUCK
  while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent

  //Transmit process
  Setup_TX(ADXL_345);
  Transmit(0x2D,0x10);
  while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent

  //Transmit process
  Setup_TX(ADXL_345);
  Transmit(0x2D,0x08);
  while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent
  

  /* Set watchdog timer interval to 1000ms (requires external crystal to work) */
  WDTCTL = WDT_ADLY_1_9;

  /* "Interrupt enable 1" for the Watchdog Timer interrupt */
  IE1 |= WDTIE;

  /* Go into low power mode 3, general interrupts enabled */
  __bis_SR_register( LPM3_bits + GIE );

  /* Do nothing...forever */
  for( ; ; ) { }
}
Ejemplo n.º 2
0
int main(void)
{
  // LED
  P1DIR |= BIT0;
  P1OUT |= BIT0;
  P4DIR |= BIT7;
  P4OUT |= BIT7;

  /*
  // UART
  BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
  DCOCTL = CALDCO_1MHZ; // Set DCO to 1MHz
  */

  // Configure hardware UART
  //P3SEL  |= BIT3 + BIT4;

  //Setup_UART();
  //UARTSendArray("Hello\n", 6);
  //__delay_cycles(1000);

  // ADXL345: MISO (SCL) 1.6->3.1, MOSI (SDA) 1.7->3.0
  P3SEL  |= BIT0 + BIT1;                     // Assign I2C pins to USCI_B0

  // Init sequence for ADXL345
  //Transmit process
  Setup_TX(ADXL_345);
  Transmit(0x2D,0x00);                    // STUCK
  while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent

  //Transmit process
  Setup_TX(ADXL_345);
  Transmit(0x2D,0x10);
  while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent

  //Transmit process
  Setup_TX(ADXL_345);
  Transmit(0x2D,0x08);
  while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent


  WDTCTL = WDT_ADLY_1_9;

  SFRIE1 |= WDTIE;

  //printf("start\n");
  __bis_SR_register( LPM3_bits + GIE );

  /* Do nothing...forever */
  for( ; ; ) { }
}
Ejemplo n.º 3
0
/* 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();
}
Ejemplo n.º 4
0
/********************************************************************
 * 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;
}
/**
  * @brief  Receive data from bq27510 through i2c bus
  * @param  
  * @retval  0 : Operation normal
  *         -1 : Could not read data
  */
int USCI_I2C_READ(char *buffer, int num, int cmd)
{
    char *PRxData;
    int i=0;

    Setup_TX();
    i2c_start();

    /* Send command to bq27510 */
    if (UCB0IFG & UCTXIFG) {
        UCB0TXBUF = cmd;
    }

    for (i=0; i<1000; i++) {
    	if (UCB0IFG & UCTXIFG)
    		break;
    }


    /* I2C could not send command
     * So we could not read data from bq27510
     * So we just return
     */
    if (i == 1000) {
    	return -1;
    }
    /* Start of RX buffer */
    PRxData = buffer;
    UCB0CTL1 &= ~UCTR;

    /* Send restart to read from bq27510 register */
    i2c_start();

    while (num > 0) {
    	for (i=0; i<1000; i++) {
			if (UCB0IFG & UCRXIFG) {
				/* Load TX buffer */
				*PRxData = UCB0RXBUF;

				/* Decrement TX byte counter */
				num--;
				PRxData++;
				UCB0IFG &= ~UCRXIFG;
				break;
			}
    	}

    	if (i == 1000) {
    	   	return -1;
    	}
    }

    i2c_stop();
    /* Clear USCI_B0 TX int flag */
    UCB0IFG &= ~UCTXIFG;

    return 0;
}
Ejemplo n.º 6
0
/********************************************************************
 * Function:        bit CHECK_I2C_ACK(unsigned char Slave_Add)
 *
 * PreCondition:    None
 *
 * Input:           I2C slave address
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Returns 0 if ACK received or 1
 *******************************************************************/
bit CHECK_I2C_ACK(unsigned char Slave_Add)
{
	Setup_TX(Slave_Add);
	RPT_Flag = 1;
	TXByteCtr = 0; // 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
	return 1;
}
Ejemplo n.º 7
0
/********************************************************************
 * Function:        void WRITE_I2C(unsigned char Slave_Add,unsigned char Add, unsigned char Val)
 *
 * PreCondition:    None
 *
 * Input:           I2C slave address, Address where data is to be written, Data to be written
 *
 * Output:          None
 *					
 * Side Effects:    None
 *
 * Overview:        Writes a byte to I2C slave at specified address
 *******************************************************************/
void WRITE_I2C(unsigned char Slave_Add,unsigned char Add, unsigned char Val)
{
	Setup_TX(Slave_Add);
	RPT_Flag = 0;
	TxBuffer[0] = Add;
	TxBuffer[1] = Val;
	PTxData = TxBuffer; // TX array start address
	TXByteCtr = 2; // 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
}
Ejemplo n.º 8
0
/********************************************************************
 * 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
}
Ejemplo n.º 9
0
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
    }
}
Ejemplo n.º 10
0
/********************************************************************
 * 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

}