Mucit23
Newbie level 3
- Joined
- May 24, 2011
- Messages
- 4
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,329
Hi friends,
I'm using pic16F877 with ADXL345 Accelerometer. I'm running ADXL345 in I2C mode. I can communicate with the sensor.
For example, when I read the registers DEVICE_ID, I'm reading the value 0xe5
When I read the data from the sensor, Every time I get the value 0x00.
This is My ADXL345 library,
main code,
Result
**broken link removed**
What could be the problem?
Thank you for help.
I'm using pic16F877 with ADXL345 Accelerometer. I'm running ADXL345 in I2C mode. I can communicate with the sensor.
For example, when I read the registers DEVICE_ID, I'm reading the value 0xe5
When I read the data from the sensor, Every time I get the value 0x00.
This is My ADXL345 library,
Code:
// ADXL345 Registers
#define ADXL345_DEVID 0x00 // R Device ID
#define ADXL345_THRESH_TAP 0x1D // R/W Tap threshold
#define ADXL345_OFSX 0x1E // R/W X-axis offset
#define ADXL345_OFSY 0x1F // R/W Y-axis offset
#define ADXL345_OFSZ 0x20 // R/W Z-axis offset
#define ADXL345_DUR 0x21 // R/W Tap duration
#define ADXL345_Latent 0x22 // R/W Tap latency
#define ADXL345_Window 0x23 // R/W Tap window
#define ADXL345_THRESH_ACT 0x24 // R/W Activity threshold
#define ADXL345_THRESH_INACT 0x25 // R/W Inactivity threshold
#define ADXL345_TIME_INACT 0x26 // R/W Inactivity time
#define ADXL345_ACT_INACT_CTL 0x27 // R/W Axis enable control for activity and inactivity detection
#define ADXL345_THRESH_FF 0x28 // R/W Free-fall threshold
#define ADXL345_TIME_FF 0x29 // R/W Free-fall time
#define ADXL345_TAP_AXES 0x2A // R/W Axis control for single tap/double tap
#define ADXL345_ACT_TAP_STATUS 0x2B // R Source of single tap/double tap
#define ADXL345_BW_RATE 0x2C // R/W Data Rate and power mode control
#define ADXL345_POWER_CTL 0x2D // R/W Power-saving features control
#define ADXL345_INT_ENABLE 0x2E // R/W Interrupt enable control
#define ADXL345_INT_MAP 0x2F // R/W Interrupt mapping control
#define ADXL345_INT_SOURCE 0x30 // R Source of interrupts
#define ADXL345_DATA_FORMAT 0x31 // R/W Data format control
#define ADXL345_DATAX0 0x32 // R X-Axis Data 0
#define ADXL345_DATAX1 0x33 // R X-Axis Data 1
#define ADXL345_DATAY0 0x34 // R Y-Axis Data 0
#define ADXL345_DATAY1 0x35 // R Y-Axis Data 1
#define ADXL345_DATAZ0 0x36 // R Z-Axis Data 0
#define ADXL345_DATAZ1 0x37 // R Z-Axis Data 1
#define ADXL345_FIFO_CTL 0x38 // R/W FIFO control
#define ADXL345_FIFO_STATUS 0x39 // R FIFO status
#define ADXL345_OUTPUTS 0x32
void adxl345_init(){
i2c_start();
i2c_write(0xA6);
//delay for ack in the slave device
//Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
i2c_write(ADXL345_DATA_FORMAT);
i2c_write(0x01);
i2c_stop();
delay_ms(10);
i2c_start();
i2c_write(0xA6);
i2c_write(ADXL345_FIFO_CTL);
i2c_write(0x00);//FIFO inactive
i2c_stop();
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
delay_ms(10);
i2c_start();
i2c_write(0xA6);
i2c_write(ADXL345_BW_RATE);
i2c_write(0x0c);
i2c_stop();
delay_ms(10);
i2c_start();
i2c_write(0xA6);
i2c_write(ADXL345_POWER_CTL);
i2c_write(0x08); //Measurement mode
i2c_stop();
}
int adxl345_read(int add){
int retval;
i2c_start();
i2c_write(0xA6);
i2c_write(add);
i2c_start();
i2c_write(0xA7);
retval=i2c_read(0);
i2c_stop();
return retval;
}
main code,
Code:
#include <16F877A.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES HS
#use delay(crystal=10000000)
#use I2C(master, sda=PIN_C4, scl=PIN_C3, slow)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
#include <lcd_driver.c>
#include <ADXL345.c>
int8 accel_data[6];
int16 xaxis=0,yaxis=0,zaxis=0;
unsigned int8 temp=0;
void main()
{
set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0x00);
set_tris_d(0x00);
set_tris_e(0x00);
output_a(0x00);
output_b(0x00);
output_c(0x00);
output_d(0x00);
output_e(0x00);
output_float(pin_c3);
output_float(pin_c4);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
setup_spi(SPI_DISABLED);
delay_ms(500);
lcd_init();
adxl345_init();
printf(lcd_putc,"\f");
lcd_gotoxy(1,1);
temp=adxl345_read(ADXL345_DEVID );
printf(lcd_putc,"ADXL345_ID=%02X",temp);
while(TRUE)
{
accel_data[0]=adxl345_read(32); //Read X axis(LSB)
accel_data[1]=adxl345_read(33); //Read X axis(MSB)
accel_data[2]=adxl345_read(34); //Read Y axis(LSB)
accel_data[3]=adxl345_read(35); //Read Y axis(MSB)
accel_data[4]=adxl345_read(36); //Read Z axis(LSB)
accel_data[5]=adxl345_read(37); //Read Z axis(MSB)
xaxis=(int16)((accel_data[1]<<8)|accel_data[0]);
yaxis=(int16)((accel_data[3]<<8)|accel_data[2]);
zaxis=(int16)((accel_data[5]<<8)|accel_data[4]);
lcd_gotoxy(1,2);
printf(lcd_putc,"X%03ld Y%03ld Z%03ld",xaxis, yaxis, zaxis);
delay_ms(100);
output_toggle(pin_a0);
}
}
Result
**broken link removed**
What could be the problem?
Thank you for help.