过节吃完饭,回来继续测试板载传感器。本次测试ISM330DHCX,3轴加速度+3轴陀螺仪传感器,读Datasheet发现,是带嵌入式机器学习内核的,这部分还没有测试,趁这会先把基本功能测试一下。读完datasheet,了解个大概,看原理图,如图1所示: m O1 r2 b$ h1 t
/ q# S9 Y8 q$ i5 m# @
! C! n6 e) D& l8 J+ I图1
, H2 V. F4 y! q$ M$ M6 R, N依然使用的SPI接口,根据前面的IC测试,思路搬过来使用。依然使用CubeMx现把框架搭一下,引脚定义如图2所示: w$ j# v5 n( A# u% F4 N: z
7 u. g( `8 p+ R6 r
$ J" u2 R6 z0 M4 h- n, ^3 J图2
/ p& d, `1 A" \# v" v7 ?. b下面就是添加所需要的初始化代码,IC初始化如下:1 o" q9 e) j5 ?, b
- /* Start device configuration. */4 N6 F' N+ y2 M, P
- ism330dhcx_device_conf_set(&dev_ctx, PROPERTY_ENABLE);
2 W; J9 B l) k, R2 [7 {' Y& x - /* Enable Block Data Update */
% D; u; K2 D& `. f9 g8 Y - ism330dhcx_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);9 b( V* w7 D% Y+ ]) B/ T
- /* Set Output Data Rate */
4 R5 |) j. F5 `- B - ism330dhcx_xl_data_rate_set(&dev_ctx, ISM330DHCX_XL_ODR_12Hz5);
) Y. }8 F: ]2 K* e - ism330dhcx_gy_data_rate_set(&dev_ctx, ISM330DHCX_GY_ODR_12Hz5);
9 K' T4 b5 `- l" E$ }( A2 R - /* Set full scale */
+ y/ G1 Q! R: p9 N8 C - ism330dhcx_xl_full_scale_set(&dev_ctx, ISM330DHCX_2g);
, e3 d' T4 l* _- m0 y - ism330dhcx_gy_full_scale_set(&dev_ctx, ISM330DHCX_2000dps);. W. d( q# P+ K( H- R. G9 L
- /* Configure filtering chain(No aux interface)* u) @/ y7 g; l. G- F
- *0 M3 B; `, l- F/ P
- * Accelerometer - LPF1 + LPF2 path
0 _" Q' J! }8 F - */
6 ?" F; N+ a/ M+ N - ism330dhcx_xl_hp_path_on_out_set(&dev_ctx, ISM330DHCX_LP_ODR_DIV_100);
- X6 d% p- a( h$ Y0 F/ `, X - ism330dhcx_xl_filter_lp2_set(&dev_ctx, PROPERTY_ENABLE);
复制代码
$ |: B& r1 D$ j3 \$ R* n d在while中添加读取数据的code,如下所示:2 b* x5 G3 g) I K( X" W- [3 Y
- ism330dhcx_xl_flag_data_ready_get(&dev_ctx, &dreg);/ f( ^. \/ b8 l9 Q @ T- x: Y
- if(dreg)# L- L) T8 m1 u k; X. j& d
- {
8 h- i& F$ W; ]; `3 Y - /* Read acceleration field data */
, E6 d; D( L+ \- g9 ~) r. l - memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
3 E* F7 W+ Y$ U; e - ism330dhcx_acceleration_raw_get(&dev_ctx, data_raw_acceleration);
# B7 }1 V% Q. ]: }. V4 M- I - acceleration_mg[0] =$ X9 P d( Z8 ]$ v$ C" w. L& x; P" S9 ?
- ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[0]);6 {/ ~! v- h9 L: C6 _, P2 h# X9 h
- acceleration_mg[1] =1 P) N+ d6 r' c1 T4 c4 C( P5 b
- ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[1]);
) k* `0 y3 m K - acceleration_mg[2] =
9 R* G; l, h8 D/ W - ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[2]);
$ U$ { J4 Z7 i6 e - }
& b$ A) j( b) e5 x# _! m' E$ h2 l - ism330dhcx_gy_flag_data_ready_get(&dev_ctx, &dreg);* e. ]3 x7 p) ^4 d/ C# B
1 i" U# Z& C+ c( i5 x6 d; e- if (dreg) {
# v1 h8 @. D* w. {3 {; S; E, F( U - /* Read angular rate field data */1 \7 l" B* k( c+ G; g7 I
- memset(data_raw_angular_rate, 0x00, 3 * sizeof(int16_t));, T% d6 @% T* W4 \9 I" g. c
- ism330dhcx_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate);
4 y* |8 c0 X# |" ?( o V - angular_rate_mdps[0] =
% Z( ~8 {% c' @% c# j/ i5 o+ e - ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[0]);
6 u3 z6 T8 y9 h* Y - angular_rate_mdps[1] =" G+ \3 S( _) z1 K3 P
- ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[1]);
9 O/ [, q2 h" G; Y" c- E# y+ L W* ? - angular_rate_mdps[2] =
" F/ N; N+ r/ \! t8 G* Q$ O' e( [ - ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[2]);3 o# x0 P! D0 i3 ]( U5 O9 n# e
- " R0 t) ~ N3 Y- N5 q: r7 V" D
- }7 T* X. E& }% h- }2 B$ k$ Q
' a, N6 W1 R7 Q- M7 O6 `, s0 E! E- ism330dhcx_temp_flag_data_ready_get(&dev_ctx, &dreg);
" s0 y8 e& i. _$ J* ?. l9 k2 Q
3 d7 v; @' a# v( P- if (dreg) {
4 g7 O6 d' o/ r - /* Read temperature data */& e9 |! z1 _7 Z( G- G
- memset(&data_raw_temperature, 0x00, sizeof(int16_t));$ U- _8 _- f; Y! S2 L
- ism330dhcx_temperature_raw_get(&dev_ctx, &data_raw_temperature);
" _) `4 [+ j% G- C# C; b1 T - temperature_degC = ism330dhcx_from_lsb_to_celsius(
~* B( c$ x- G2 e7 l, ` - data_raw_temperature);
. {0 U+ w, d( I, b& r# A - ; J' k( r' O9 c2 ~' P1 @
- }
复制代码
; Z! X" G. J2 b; ]3 ]. a C& n% ?3 u$ Z
* Y, s2 q& k8 T7 e B6 Y! p测试失败,显示hardware_fault,硬件错误。想了想,是晚上有点恍惚,把时钟配置错了,板载的外部时钟是16M,而cubemx中的默认时钟是8M,修改过来,重新生成项目,debug,测试结果如下;1 k& M ?. f& m. j- [1 d) k1 }
' W6 q& H" l6 e$ c. W, a5 D/ y" C
+ {6 X0 n5 y; y: A! @( Z) \( T图3
9 A6 p! F2 }: |
* F$ g2 P( H! U: d& C/ C- Y
7 q; g- \! S1 j% e1 s2 \7 i& [( O6 I这段时间总是感觉知识浮于基础,得往深处继续挖掘学习。下次试一试Nano edge AI。3 u8 }0 I- `5 `- c
6 l; \ O2 [2 j! G3 G: t |