aboutsummaryrefslogtreecommitdiffhomepage
path: root/workspace/ts100/src/MMA8652FC.c
blob: 98a55e200ad9443446faae9b36098ef2387906b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * MMA8652FC.*
 * Files for the built in accelerometer from NXP.
 * This sets the unit up in motion detection mode with an interrupt on movement
 * This interrupt is fed to PB5 which catches it via EXTI5
 *
 * http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8652FC.pdf
 *
 * EXTI Motion config setup values lifted from AN4070from NXP
 *
 * Ben V. Brown - <ralim@ralimtek.com>
 */
#include <stdio.h>
#include "Bios.h"
#include "MMA8652FC.h"
#include "I2C.h"

void I2C_RegisterWrite(uint8_t reg, uint8_t data) {
	u8 tx_data[2];
	tx_data[0] = reg;
	tx_data[1] = data;
	I2C_PageWrite(tx_data, 2, DEVICE_ADDR);
}

uint8_t I2C_RegisterRead(uint8_t reg) {
	u8 tx_data[3];
	tx_data[0] = reg;
	I2C_PageRead(tx_data, 1, DEVICE_ADDR, reg);
	return tx_data[0];
}

void StartUp_Accelerometer(uint8_t sensitivity) {
	I2C_RegisterWrite(CTRL_REG2, 0); //Normal mode
	I2C_RegisterWrite( CTRL_REG2, 0x40);	// Reset all registers to POR values
	delayMs(2);		// ~1ms delay
	I2C_RegisterWrite(FF_MT_CFG_REG, 0x78);	// Enable motion detection for X and Y axis, latch enabled
	uint8_t sens = 9*7+1;
	sens -= 7 * sensitivity;

	I2C_RegisterWrite(FF_MT_THS_REG, sens);		// Set threshold
	I2C_RegisterWrite(FF_MT_COUNT_REG, 0x01);	// Set debounce to 100ms

	I2C_RegisterWrite( CTRL_REG4, 0x04);		// Enable motion interrupt
	I2C_RegisterWrite( CTRL_REG5, 0x04);// Route motion interrupts to INT1 ->PB5 ->EXTI5
	I2C_RegisterWrite( CTRL_REG1, 0x19);		// ODR=100 Hz, Active mode
}