//==============================================================================
// Connection of module ADXL345.
//
// GND <<<>>> GND.
// VCC <<<>>> 3.3 V.
// ADXL345_CS <<<>>> You can change this pin.
// ADXL345_INT1 <<<>>> You can change this pin.
// ADXL345_INT2 <<<>>> You can change this pin.
// ADXL345_SDO <<<>>> Connect to SDI MCU.
// ADXL345_SDI <<<>>> Connect to SDO MCU.
// ADXL345_SCK <<<>>> Connect to SCK MCU.
//==============================================================================
/* The next lines shoul be at the main file */
//#define ADXL345_CS_TRIS TRISDbits . TRISD4
//#if defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
// #define ADXL345_CS PORTDbits . RD4
//#elif defined ( _PIC18F45K22_H_ ) || defined ( _PIC18F44K22_H_ ) || defined ( _PIC18F24K22_H_ )
// #define ADXL345_CS LATDbits . LATD4
//#endif
//
///* Uncomment whathever Protocol using */
//#define ADXL345_SPI_PROTOCOL
////#define ADXL345_I2C_PROTOCOL
void setUpADXL345 ( void )
{
#if defined ADXL345_SPI_PROTOCOL
// Set the control pìns as Outputs with the TRISx Macro.
ADXL345_CS_TRIS = OUTPUT;
// Set the logic leves on control pins.
ADXL345_CS = HIGH;
#endif
// functions commented "//", jsut in case need it, as Reset values are '0s'.
// Single/Double TAP Registers.
// writeADXL345 ( THRESH_TAP, CLEAR );
// writeADXL345 ( DUR, CLEAR );
// writeADXL345 ( LATENT, CLEAR );
// writeADXL345 ( WINDOW, CLEAR );
// writeADXL345 ( TAP_AXES, SUPPRESS_DOUBLE_TAP_EN | TAP_X_DISABLE |
// TAP_Y_DISABLE | TAP_Z_DISABLE );
// Offset Regsiters.
// writeADXL345 ( OFSX, CLEAR );
// writeADXL345 ( OFSY, CLEAR );
// writeADXL345 ( OFSZ, CLEAR );
// BW Rate Regsiters.
// writeADXL345 ( BW_RATE, 0x0D );
writeADXL345 ( BW_RATE, LOW_POWER_NORMAL | DATA_RATE_800HZ );
// Activity and Inactivity Regsiters.
// writeADXL345 ( THRESH_ACT, CLEAR );
// writeADXL345 ( THRESH_INACT, CLEAR );
// writeADXL345 ( TIME_INACT, CLEAR );
// writeADXL345 ( ACT_INACT_CTL, ACT_DC_COUPLED | ACT_X_DISABLE | ACT_Y_DISABLE |
// ACT_Z_DISABLE | INACT_DC_COUPLED | INACT_X_DISABLE | INACT_Y_DISABLE |
// INACT_Z_DISABLE );
// Free Fall Regsiters.
// writeADXL345 ( THRESH_FF, CLEAR );
// writeADXL345 ( TIME_FF, CLEAR );
// POWER_CTL Register (0x2D).
writeADXL345 ( POWER_CTL, LINK_BOTH | AUTO_SLEEP_ON | MEASURE_ON |
SLEEP_OFF | WAKEUP_4HZ );
// writeADXL345 ( ACT_INACT_CTL, CLEAR );
// INT_ENABLE Registers(0x2E).
writeADXL345 ( INT_ENABLE, INT_DATA_READY_OFF | INT_SINGLE_TAP_OFF | INT_DOUBLE_TAP_OFF |
INT_ACTIVITY_OFF | INT_INACTIVITY_OFF | INT_FREE_FALL_OFF | INT_WATERMARK_OFF |
INT_OVERRUN_OFF );
// INT_MAP Registers(0x2F).
writeADXL345 ( INT_MAP, DATA_READY_PIN_INT1 | FREE_FALL_PIN_INT2 );
// writeADXL345 ( DATA_FORMAT, 0x09 );
writeADXL345 ( DATA_FORMAT, CLEAR | SPI_4W | SELF_TEST_DIS | INT_INVERT_L |
FULL_RES_10BITS | JUSTIFY_RIGHT | RANGE_2G );
// FIFO_CTL Register (0x38).
writeADXL345 ( FIFO_CTL, FIFO_MODE_FIFO | EVENT_ON_INT2 | 0x06 );
__delay_us ( 10 );
writeADXL345 ( POWER_CTL, MEASURE_ON );
}
void writeADXL345 ( unsigned char registerVal, unsigned char val )
{
#if defined ( ADXL345_I2C_PROTOCOL )
StartI2C ( );
// WriteI2C ( ADXL345_ADDRESS | 0x00 );
I2CAddressCommand ( ADXL345_ADDRESS >> 1, I2C_WRITE );
WriteI2C ( registerVal );
WriteI2C ( val );
StopI2C ( );
#elif defined ( ADXL345_SPI_PROTOCOL )
ADXL345_CS = LOW;
writeByteSPI ( registerVal );
writeByteSPI ( val );
ADXL345_CS = HIGH;
#else
#warning "You should at leats pick one of the SMPP Proteocol (I2C or SPI) Mode."
#warning "At the main file, uncomment the #define ADXL345_xxx_PROTOCOL to use."
#endif
}
unsigned char readArray [ ];
unsigned char readADXL345 ( unsigned char registerVal )
{
unsigned char val = CLEAR;
#if defined ADXL345_I2C_PROTOCOL
StartI2C ( );
WriteI2C ( ADXL345_ADDRESS );
WriteI2C ( registerVal );
RestartI2C ( );
// WriteI2C ( ADXL345_ADDRESS | 0x01 );
I2CAddressCommand ( ADXL345_ADDRESS >> 1, I2C_READ );
val = ReadI2C ( );
StopI2C ( );
#elif defined ADXL345_SPI_PROTOCOL
ADXL345_CS = LOW;
writeByteSPI ( registerVal | 0x80 );
val = readByteSPI ( );
ADXL345_CS = HIGH;
#else
#warning "You should at leats pick one of the SMPP Proteocol (I2C or SPI) Mode."
#warning "At the main file, uncomment the #define ADXL345_xxx_PROTOCOL to use."
#endif
return val;
}
// It takes about 1.17ms runing microcontroller at 8MHz.
unsigned int x, z, y;
void readThreeAxisAccelerometer ( void )
{
unsigned char acelerometerArray [ 6 ];
unsigned char tempArrayVal = CLEAR;
for ( unsigned char i = DATAX0; i <= DATAZ1; i++ )
{
acelerometerArray [ tempArrayVal ] = readADXL345 ( i );
tempArrayVal++;
}
x = acelerometerArray [ 0 ] | acelerometerArray [ 1 ] << 0x08;
y = acelerometerArray [ 2 ] | acelerometerArray [ 3 ] << 0x08;
z = acelerometerArray [ 4 ] | acelerometerArray [ 5 ] << 0x08;
#ifdef ADXL345_DEBUG
printf ( " x= %d, y= %d, z=%d ", x
, y
, z
); delay_ms ( 100 );
#endif
}