adc/temp integer
This commit is contained in:
parent
3b3b00a9f8
commit
bd76380a65
25
lis3dh.c
25
lis3dh.c
@ -373,27 +373,19 @@ int lis3dh_reset(lis3dh_t *lis3dh) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* read all 3 ADCs and convert readings depending on power mode
|
/* read all 3 ADCs and convert readings depending on power mode
|
||||||
st 1 float count is equal to 1 millivolt */
|
st 1 lsb is equal to 1 millivolt */
|
||||||
int lis3dh_read_adc(lis3dh_t *lis3dh) {
|
int lis3dh_read_adc(lis3dh_t *lis3dh) {
|
||||||
uint8_t data[6];
|
uint8_t data[6];
|
||||||
int err = 0;
|
|
||||||
uint8_t shift;
|
uint8_t shift;
|
||||||
float divisor;
|
int err = 0;
|
||||||
|
|
||||||
/* read adc{1,2,3} LSB and MSB */
|
|
||||||
err |= lis3dh->dev.read(REG_OUT_ADC1_L, data, 6);
|
err |= lis3dh->dev.read(REG_OUT_ADC1_L, data, 6);
|
||||||
|
|
||||||
if (lis3dh->cfg.mode == LIS3DH_MODE_LP) {
|
shift = (lis3dh->cfg.mode == LIS3DH_MODE_LP) ? 8 : 6;
|
||||||
shift = 8;
|
|
||||||
divisor = 256.0;
|
|
||||||
} else {
|
|
||||||
shift = 6;
|
|
||||||
divisor = 1024.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
lis3dh->adc.adc1 = 1200.0 + (400.0 * (float)(((int16_t)(data[1] | (data[0] << 8))) >> shift) / divisor);
|
lis3dh->adc.adc1 = 1200 + (400 * ((data[1] | data[0] << 8) >> shift) >> (16 - shift));
|
||||||
lis3dh->adc.adc2 = 1200.0 + (400.0 * (float)(((int16_t)(data[3] | (data[2] << 8))) >> shift) / divisor);
|
lis3dh->adc.adc2 = 1200 + (400 * ((data[3] | data[2] << 8) >> shift) >> (16 - shift));
|
||||||
lis3dh->adc.adc3 = 1200.0 + (400.0 * (float)(((int16_t)(data[5] | (data[4] << 8))) >> shift) / divisor);
|
lis3dh->adc.adc3 = 1200 + (400 * ((data[5] | data[4] << 8) >> shift) >> (16 - shift));
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@ -403,12 +395,13 @@ int lis3dh_read_adc(lis3dh_t *lis3dh) {
|
|||||||
in increments of plus or negative 1 unit celsius.
|
in increments of plus or negative 1 unit celsius.
|
||||||
the reported temperature is stored inplace of adc3
|
the reported temperature is stored inplace of adc3
|
||||||
temp sensing is always in 8-bit mode
|
temp sensing is always in 8-bit mode
|
||||||
operating range: -40 to 85 celsius */
|
operating range: -40 to 85 celsius
|
||||||
|
1 lsb = 1 deg C */
|
||||||
int lis3dh_read_temp(lis3dh_t *lis3dh) {
|
int lis3dh_read_temp(lis3dh_t *lis3dh) {
|
||||||
uint8_t data;
|
uint8_t data;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
err |= lis3dh->dev.read(REG_OUT_ADC3_H, &data, 1);
|
err |= lis3dh->dev.read(REG_OUT_ADC3_H, &data, 1);
|
||||||
lis3dh->adc.adc3 = (float)((int8_t)data) + 25.0;
|
lis3dh->adc.adc3 = (int8_t)data + 25;
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
9
lis3dh.h
9
lis3dh.h
@ -235,11 +235,14 @@ struct lis3dh_config {
|
|||||||
* same as ODR
|
* same as ODR
|
||||||
* Output:
|
* Output:
|
||||||
* actual value in mV
|
* actual value in mV
|
||||||
|
* 1 lsb = 1 mv
|
||||||
|
* Temperature:
|
||||||
|
* 1 lsb = 1 deg C
|
||||||
*/
|
*/
|
||||||
struct lis3dh_adc {
|
struct lis3dh_adc {
|
||||||
float adc1;
|
int16_t adc1;
|
||||||
float adc2;
|
int16_t adc2;
|
||||||
float adc3;
|
int16_t adc3;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* accel data not read from FIFO is put here */
|
/* accel data not read from FIFO is put here */
|
||||||
|
2
main.c
2
main.c
@ -91,7 +91,7 @@ int main() {
|
|||||||
quit("temp()", &lis);
|
quit("temp()", &lis);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("ADC1: %04.04f mV, temp: %.0f\n", lis.adc.adc1, lis.adc.adc3);
|
printf("ADC1: %d mV, temp: %d\n", lis.adc.adc1, lis.adc.adc3);
|
||||||
|
|
||||||
/* unregister interrupt */
|
/* unregister interrupt */
|
||||||
if (int_unregister(GPIO_INTERRUPT_PIN)) {
|
if (int_unregister(GPIO_INTERRUPT_PIN)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user