SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By olivia.g
#186704
I had problems changing my max_G to 8g, it is always remain as the default 2g.

I suspect that the address value was no changed because when I tried to output the address,it still remain as zero, the default value.

Here's the code for writing the address (I'm using eclipse):

void
MMA8452Q::reset()
{
/* enable accel*/
_reg1_expected = REG1_SLEEP_RATE_50HZ | REG1_ODR_800HZ | REG1_LNOISE_NORMAL | REG1_F_READ_NORMAL | REG1_ACTIVE;
_data_CFG |= DATA_CFG_FS_8G;
write_reg(ADDR_CTRL_REG1, _reg1_expected);
write_reg(ADDR_CTRL_REG4, REG4_INT_EN_DRDY);
write_reg(ADDR_DATA_CFG,_data_CFG);
accel_set_samplerate(MMA8452_ACCEL_DEFAULT_RATE);
accel_set_driver_lowpass_filter((float)MMA8452_ACCEL_DEFAULT_RATE, (float)MMA8452_ACCEL_DEFAULT_DRIVER_FILTER_FREQ);
_accel_read = 0;
accel_set_range(MMA8452_ACCEL_DEFAULT_RANGE_G);
}

int
MMA8452Q::accel_set_range(unsigned max_g)
{
uint8_t setbits = 0;
//uint8_t clearbits = DATA_CFG_FS_BITS;
float new_scale_g_digit = 0.0f;

if (max_g == 0)
max_g = 8;


if (max_g <= 2) {
_accel_range_m_s2 = 2.0f*ONE_G;
setbits |= DATA_CFG_FS_2G;
new_scale_g_digit = 0.976e-3f; // 1/1024
printf(" Less than 2G \n");

} else if (max_g <= 4) {
_accel_range_m_s2 = 4.0f*ONE_G;
setbits |= DATA_CFG_FS_4G;
new_scale_g_digit = 1.953e-3f; // 1/512
printf(" Less than 4G \n");

} else if (max_g <= 8) {
_accel_range_m_s2 = 8.0f*ONE_G; // 1/256
setbits |= DATA_CFG_FS_8G;
new_scale_g_digit = 3.906e-3f;
printf(" Less than 8G \n");

} else {
return -EINVAL;
}

int ret;
_accel_range_scale = new_scale_g_digit * ONE_G;

/*
* Send the command to set the range
*/
printf("setbits = %d\n",setbits );
printf("_data_CFG = %d\n",_data_CFG );
uint8_t range_bits_in = 9;

ret = read_reg(ADDR_DATA_CFG, range_bits_in);
printf("before range_bits_in= %d\n",range_bits_in );
if (OK != ret)
perf_count(_comms_errors);
ret = write_reg(ADDR_DATA_CFG,setbits);
usleep(1000000);
if (OK != ret)
{
perf_count(_comms_errors);
printf("write failed");
}
read_reg(ADDR_DATA_CFG, range_bits_in);
printf("after range_bits_in= %d\n",range_bits_in );
return !(range_bits_in == (setbits));

}
I once suspected my write_reg function is incorrect but upon double checking, it was verified that it work on other instances, except this time.

Any idea on this? Thanks in advance!

Olivia