// *************************************************************************************************
// @fn          as_start
// @brief       Power-up and initialize acceleration sensor
// @param       none
// @return      none
// *************************************************************************************************
void as_start(uint8_t mode) {
	volatile uint16_t Counter_u16;
	uint8_t bConfig, bStatus;


	// Initialize SPI interface to acceleration sensor
	AS_SPI_CTL0 |= UCSYNC | UCMST | UCMSB // SPI master, 8 data bits,  MSB first,
			| UCCKPH; //  clock idle low, data output on falling edge
	AS_SPI_CTL1 |= UCSSEL1; // SMCLK as clock source
	AS_SPI_BR0 = AS_BR_DIVIDER; // Low byte of division factor for baud rate
	AS_SPI_BR1 = 0x00; // High byte of division factor for baud rate
	AS_SPI_CTL1 &= ~UCSWRST; // Start SPI hardware

	// Initialize interrupt pin for data read out from acceleration sensor
	AS_INT_IES &= ~AS_INT_PIN; // Interrupt on rising edge

#ifdef AS_DISCONNECT	  
	// Enable interrupt 
	AS_INT_DIR &= ~AS_INT_PIN; // Switch INT pin to input
	AS_SPI_DIR &= ~AS_SDI_PIN; // Switch SDI pin to input
	AS_SPI_REN |= AS_SDI_PIN; // Pulldown on SDI pin
	AS_SPI_SEL |= AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN; // Port pins to SDO, SDI and SCK function
	AS_CSN_OUT |= AS_CSN_PIN; // Deselect acceleration sensor
	AS_PWR_OUT |= AS_PWR_PIN; // Power on active high
#endif

	// Delay of >5ms required between switching on power and configuring sensor
	timer0_delay(10);

	// Initialize interrupt pin for data read out from acceleration sensor
	AS_INT_IFG &= ~AS_INT_PIN; // Reset flag
	AS_INT_IE |= AS_INT_PIN; // Enable interrupt


	// Reset sensor
	as_write_register(0x04, 0x02);
	as_write_register(0x04, 0x0A);
	as_write_register(0x04, 0x04);

	// Wait 5 ms before starting sensor output
	timer0_delay(5);


	// Configure sensor and start to sample data
		switch (mode) {
		case FALL_MODE:
			if (as_config.range == 2) {
				bConfig = 0x80;

				if (as_config.sampling == SAMPLING_100_HZ)
					bConfig |= 0x0A;
				else if (as_config.sampling == SAMPLING_400_HZ)
					bConfig |= 0x0C;
			} else if (as_config.range == 8) {
				bConfig = 0x00;
				if (as_config.sampling == SAMPLING_100_HZ)
					bConfig |= 0x0A;
				else if (as_config.sampling == SAMPLING_400_HZ)
					bConfig |= 0x0C;
			}
			// fall time as long as possible 150 msec at 100 Hz
			write_FFTMR(as_config.MDFFTMR);
			//threshold for computation
			write_FFTHR(as_config.FFTHR);

			break;
		case MEASUREMENT_MODE:
			// Configure sensor and start to sample data
			if (as_config.range == 2) {
				bConfig = 0x80;
				if (as_config.sampling == SAMPLING_100_HZ)
					bConfig |= 0x02;
				else if (as_config.sampling == SAMPLING_400_HZ)
					bConfig |= 0x04;
			} else if (as_config.range == 8) {
				bConfig = 0x00;
				if (as_config.sampling == SAMPLING_40_HZ)
					bConfig |= 0x06;
				else if (as_config.sampling == SAMPLING_100_HZ)
					bConfig |= 0x02;
				else if (as_config.sampling == SAMPLING_400_HZ)
					bConfig |= 0x04;
			}
			break;
		case ACTIVITY_MODE:
			// Configure sensor and start to sample data
			if (as_config.range == 2) {
				bConfig = 0x80;
				if (as_config.sampling == SAMPLING_10_HZ)
					bConfig |= 0x08;
			} else if (as_config.range == 8) {
				bConfig = 0x00;
				if (as_config.sampling == SAMPLING_10_HZ)
					bConfig |= 0x08;
			}
			bConfig |= MDET_EXIT<<5;
			// fall time as long as possible 150 msec at 100 Hz
			write_MDTMR(as_config.MDFFTMR);
			//check if lower than 571 mgrav
			//write_MDTHR(sAccel.MDTHR);
			write_MDTHR(as_config.MDTHR);
			break;
		default:
			bConfig = 0x80;
			break;

		}

	// Wait 2 ms before entering modality to settle down
	timer0_delay(2);

	//write the configuration
	as_write_register(ADDR_CTRL, bConfig);

	// Wait 2 ms before entering modality to settle down
	timer0_delay(2);
}
Example #2
0
void change_mode(uint8_t mode)
{
	uint8_t bConfig = 0x00;

/* Configure sensor and start to sample data */
	switch (mode) {
	case FALL_MODE:
		if (as_config.range == 2) {
			bConfig = 0x80;

			if (as_config.sampling == SAMPLING_100_HZ)
				bConfig |= 0x0A;
			else if (as_config.sampling == SAMPLING_400_HZ)
				bConfig |= 0x0C;
		} else if (as_config.range == 8) {
			bConfig = 0x00;

			if (as_config.sampling == SAMPLING_100_HZ)
				bConfig |= 0x0A;
			else if (as_config.sampling == SAMPLING_400_HZ)
				bConfig |= 0x0C;
		}

		/* fall time as long as possible 150 msec at 100 Hz */
		write_FFTMR(as_config.MDFFTMR);
		/* threshold for computation */
		write_FFTHR(as_config.FFTHR);

		break;

	case MEASUREMENT_MODE:

		/* Configure sensor and start to sample data */
		if (as_config.range == 2) {
			bConfig = 0x80;

			if (as_config.sampling == SAMPLING_100_HZ)
				bConfig |= 0x02;
			else if (as_config.sampling == SAMPLING_400_HZ)
				bConfig |= 0x04;
		} else if (as_config.range == 8) {
			bConfig = 0x00;

			if (as_config.sampling == SAMPLING_40_HZ)
				bConfig |= 0x06;
			else if (as_config.sampling == SAMPLING_100_HZ)
				bConfig |= 0x02;
			else if (as_config.sampling == SAMPLING_400_HZ)
				bConfig |= 0x04;
		}

		break;

	case ACTIVITY_MODE:

		/* Configure sensor and start to sample data */
		if (as_config.range == 2) {
			bConfig = 0x80;

			if (as_config.sampling == SAMPLING_10_HZ)
				bConfig |= 0x08;
		} else if (as_config.range == 8) {
			bConfig = 0x00;

			if (as_config.sampling == SAMPLING_10_HZ)
				bConfig |= 0x08;
		}

		bConfig |= MDET_EXIT << 5;
		/* fall time as long as possible 150 msec at 100 Hz */
		write_MDTMR(as_config.MDFFTMR);
		/* check if lower than 571 mgrav */
		/* write_MDTHR(sAccel.MDTHR); */
		write_MDTHR(as_config.MDTHR);
		break;

	default:
		bConfig = 0x80;
		break;

	}

	/* Wait 2 ms before entering modality to settle down */
	timer0_delay(2, LPM3_bits);

	/* write the configuration */
	as_write_register(ADDR_CTRL, bConfig);

	/* Wait 2 ms before entering modality to settle down */
	timer0_delay(2, LPM3_bits);

}