这两天测试了一下板载的IIS2DH加速度传感器,这是一款低功耗的3轴加速度计,低功耗时能达到2uA,在工业领域广泛应用。从原理图上看,也是连接了SPI3接口,如图1所示:8 `+ ~6 f1 z7 O
2 g5 X' A5 W6 D( o) |' b2 f
8 U' l4 J$ Y( x
! D; ^2 t: c. Q6 I4 O图1
! Y$ H* ~# R$ x) X& M/ c: p( `/ L 除了INT2和CS引脚需要重新配置,其他的配置和上一篇是相同的,在CubeMx中很方便重新建立工程文件。想着测试应该是很顺利的,但是能读Device ID,不能读取数据,一度不知道什么原因。隔了一天发现,是CS引脚在程序中搞错了,很多问题往往都是粗心大意导致的。来看软件,初始定义还是参考官方的例程,库也是使用官方的,这样测试比较节省时间。* v( {! P2 E' D6 g; D8 c& w# j
初始化如下:/ r1 Y' ^1 o" h
- dev_ctx .write_reg = platform_write ;6 Z" H# X) ?- m% ?; h
- dev_ctx .read_reg = platform_read ;
# J! c3 H) c& n - dev_ctx .handle = &hspi3 ;
8 z V+ T: {. Z6 l$ w9 A& G - HAL_Delay(10);+ }% ?& q9 f0 S: T# X
-
( l4 L% B- i2 ^$ I+ f* o. w+ L) [9 @ - // IIS2DH_ReadID(&Obj ,&whoamI);
& s' N$ G8 g$ }* p - iis2dh_device_id_get(&dev_ctx, &whoamI);
" a- s( h x+ ]# i8 Q3 D6 ~' F - //
2 I2 [/ D, M g8 x - // IIS2DH_Init(&Obj);/ _# K+ `7 B4 f9 C4 R
4 |' K7 o5 V+ G5 E- t- iis2dh_block_data_update_set(&dev_ctx , PROPERTY_ENABLE);
7 ^) J7 i7 T: f% u* x$ S( u9 | - /* Set Output Data Rate to 1Hz */+ ?! ^* p$ v2 ~: ]
- iis2dh_data_rate_set(&dev_ctx , IIS2DH_ODR_1Hz);
_: M$ }$ D0 j, C) o/ N - /* Set full scale to 2g */3 `3 O7 {: D E$ k; U" O9 X
- iis2dh_full_scale_set(&dev_ctx , IIS2DH_2g);
2 L! Y4 w9 y' U; W - // /* Enable temperature sensor */$ ]3 L" J+ n( O7 M
- iis2dh_temperature_meas_set(&dev_ctx, IIS2DH_TEMP_ENABLE);$ T% A, v0 E7 h X- v
- // /* Set device in continuous mode with 12 bit resol. */
" U# M3 u4 q7 O* a7 T! w( B L, [ - iis2dh_operating_mode_set(&dev_ctx , IIS2DH_HR_12bit);
复制代码
! j8 V1 j; F; o0 L0 {6 F' W+ N
* m- X/ S) y) _1 F7 D读取加速度数据如下:
. l7 |- h! C/ k" D- iis2dh_xl_data_ready_get(&dev_ctx , &ddbyte );
) h) a3 s$ U! L6 a( r7 ~' ~: P - if(ddbyte )! `1 Z2 A8 Z5 y+ W! z+ }
- {
. j# i2 v; `: n5 ] - /* Read accelerometer data */) S+ c5 q" a* a9 A7 U+ y8 q" m( A! C
- memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
. K. q& Z0 t: C# {9 T$ n -
, @& x1 m) t; n! O H' {4 m1 p( b# V - iis2dh_acceleration_raw_get(&dev_ctx , data_raw_acceleration);
. B c' }3 |" m7 a8 e -
) x' w1 I( z' k2 b6 F - acceleration_mg[0] =iis2dh_from_fs2_hr_to_mg(data_raw_acceleration[0]);# x5 {. @, C1 q7 N( S$ ]; s
- 5 J2 [% w+ g" U& C& q, M1 W
- acceleration_mg[1] =iis2dh_from_fs2_hr_to_mg(data_raw_acceleration[1]);
+ M' ` u. J# X) ?. j B, l' [ -
& t0 F% s2 {) j& n - acceleration_mg[2] = iis2dh_from_fs2_hr_to_mg(data_raw_acceleration[2]);
- g4 K3 ]* J0 E! c% x) k - }
复制代码 / q$ [2 a7 e9 U7 d
2 d; v' h5 Y) ?- Q
读取温度数据如下:
" }5 O3 W" C9 }6 E6 u
3 f! G* Z6 H, x( s$ u0 W# M Hiis2dh_temp_data_ready_get(&dev_ctx , &ddbyte );1 R" D9 \: d" N% i
6 k8 I+ I/ H i1 e* x* f( S0 a
if (ddbyte) 6 n ^4 _$ \. }9 o
{( F3 [+ c6 t$ g' b6 g
/* Read temperature data */+ E/ \' {, H; ^! F
memset(&data_raw_temperature, 0x00, sizeof(int16_t));! z: }3 [4 A5 R" L
iis2dh_temperature_raw_get(&dev_ctx, &data_raw_temperature);
* W2 Y" ]8 l! Y1 s+ c6 J temperature_degC =iis2dh_from_lsb_hr_to_celsius(data_raw_temperature);, D5 Z+ Q6 Y+ ]# H$ I+ O& z/ @, E
}
7 W- @5 P- g9 k9 W G" z8 v1 f$ {6 x6 x: o R( W* S0 X
测量结果如图2所示:% S5 y9 ^0 t; `
: v6 s: z7 T% [$ b x
9 m( L( p9 N5 e# z# W8 ]图2
9 W4 T( y; @7 R0 v. N7 L+ |, B8 E有一些功能还没测试,要深入学习才有所得。3 f6 x7 I3 \: Y
|