你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STWINKT1B评测】2.初步测试IIS3DWB振动传感器

[复制链接]
jundao721 发布时间:2024-7-30 21:42
      根据AN5444应用笔记介绍,IIS3DWB是一款高性能MEMS三轴振动传感器,具有超宽带宽低噪声等特点,SPI标准接口输出。具体的介绍可参考AN5444应用笔记。本次先对该传感器有个初步的数据读取测试,关于滤波等后面学习时再添加。
* }, i2 Q5 ?: k$ ~, n2 _7 E& {: ^, Q     原理图关于该部分的说明,如图1所示:
: Z& N/ W0 d4 X& Y1 E+ i6 ~, a 2-2.JPG . b1 V! [5 |' t/ f% @
图1; e3 l8 l( Y. L# w  m( z$ t

3 g. w. N8 ?& A7 {+ n* F    先使用CubeMx建立工程,时钟等正常配置即可,因为要使用SPI通讯,所以引脚配置如图2:
$ l, o8 r9 Y) |0 T3 c' [! ?
* ^" s, V7 H; `8 N( C1 ?$ A9 N" w& C 2-3.JPG 9 z% \1 ]" a4 N! y  b! G
2 ]- b( E' k) u: s2 i# i
图2
8 r+ u& W4 s, a5 N   SPI外设参数的基本配置如图3:9 |& R0 E8 v5 }% Q
2-4.JPG
! a) C) |7 _: a$ O' U
* K7 V8 K; U( A. O% W* {
图3
0 L2 z2 X  D1 @2 `1 S  然后生成项目。在官方提供的文件中有IIS3DWB的驱动,4个文件,如图4所示:2 H# W+ H: ~% b2 _4 k
2-5.JPG 3 o  w, L/ {& J  t9 g1 A

  M1 t& s# F. y图4
+ k2 I; W" R% m6 A8 [& Y  例程是参考官方github中提供的参考例程,修改了一下,增加了温度读取。初始化如下:
8 P0 B, f  i4 L4 \3 B0 [; C( B
  1. dev_ctx.write_reg = platform_write;
    $ i) i$ ^" @5 O# w& q. K0 L4 `2 J
  2. dev_ctx.read_reg = platform_read;
    , r9 l. W  e* l  N
  3. dev_ctx.handle = &hspi3;  N# z3 L: `' _8 k8 _% D

  4. ) F! M0 S9 `( r+ `5 Q8 ?
  5. HAL_Delay (10);' E7 a8 c% g9 a7 {) a
  6. iis3dwb_device_id_get(&dev_ctx, &whoamI);+ ^9 w) {2 f% C- z! n! O. n% w
  7. // 复位设备: a$ i! R* f8 M8 o
  8. iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);
    0 p. ]7 x* H& t
  9. // 配置加速度计& @; A' d( j+ _/ P. E  H
  10. iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
    . z! c+ g$ P! `' a9 c5 R0 A7 t
  11. iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);
    % f- z4 Y) k4 j; O/ C
  12. iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);
复制代码
2 l4 a1 B1 M/ m5 U, ~- i2 A
# V: v4 z- }2 }* U/ I
  对于加速度值和温度的读取程序如下:6 Z8 n- E3 q1 N0 Z3 X: H
  1.   // 读取加速度数据0 V: n8 D/ C' s) a% [, Z. z8 S+ p
  2.    
    7 [* g1 b7 {( k' H
  3.         iis3dwb_xl_flag_data_ready_get(&dev_ctx, &drdy);//读取状态值0 e" a5 r( G' o/ ^+ d
  4.         if (drdy) {
    ! L2 J; {( f8 P; V$ f) u
  5.            # Y  t6 u- i2 M7 A
  6.             iis3dwb_acceleration_raw_get(&dev_ctx, data_raw);
    & J5 F  h# T6 D- _
  7.             6 e1 T; @  L1 f: t
  8.             acceleration_mg[0] = iis3dwb_from_fs2g_to_mg(data_raw[0]);, a7 X) |% g* ^! \. F2 {
  9.             acceleration_mg[1] = iis3dwb_from_fs2g_to_mg(data_raw[1]);
    ! P0 X! Q6 Y3 c+ y  o" g3 ]
  10.             acceleration_mg[2] = iis3dwb_from_fs2g_to_mg(data_raw[2]);" E6 A& R2 ?) K* K
  11.         }9 F/ w4 z. n5 g2 E  T9 M
  12.                 iis3dwb_read_reg (&dev_ctx ,IIS3DWB_OUT_TEMP_L ,temp_data ,2);! _$ m9 _! S/ w9 m0 A4 M7 X% b
  13.                 temp_raw = (int16_t )((temp_data [1]<<8) | temp_data[0]);/ v8 p4 f% Y# N
  14.                 temp_cel = temp_raw /256+25;: S% N5 I$ I5 A

  15. ) U5 J0 z% L: l+ P; c4 f
  16.         HAL_Delay(1000); // 每秒读取一次数据
复制代码
  1. 6 l/ d2 n* v$ c2 x) O' \2 w8 T
复制代码
: o' L# V6 j& q+ d

+ `/ X& q; g; q+ @. }  测试结果如图5所示:
5 Z& E6 v/ V) B5 [/ R+ V 2-1.JPG 1 O( q+ M/ I7 ~3 \6 s; x: E3 l
- d0 I# `. Z/ a) x
图5
/ o5 Q7 |2 G8 `7 J0 aDevice ID和温度值是正确的,滤波,FIFO等功能的增加一边理解一边添加测试。
; p, O3 L, p& v
+ k6 l: d1 z" O
4 q0 C. m/ j/ ~. k9 o" h& \
收藏 评论12 发布时间:2024-7-30 21:42

举报

12个回答
Minerva_ 回答时间:2025-3-17 11:42:19

jundao721 发表于 2025-3-16 09:43
这是我SPI3的基本配置,程序的话初始化完后,延时十几毫秒,再去读whoamI,别的基本上都是官库里的,正常 ...

感谢博主指导,我已经调试完成,发现是在使用spi3时只在spi.h文件中声明了spi3,并没有在main.h中声明,在声明之后后获取了数据,再次感谢博主的答疑,辛苦您了(鞠躬)

image.png

STMCU-管管 回答时间:2024-10-18 09:39:30

感谢分享

你也是双下巴 回答时间:2024-10-18 10:09:24
做温湿度显示意味着穿戴设备这个STWINKT1B是不是可以做/ G& m, d2 u) R  i  M* M
养乐多 回答时间:2024-10-18 10:15:17
你也是双下巴 发表于 2024-10-18 10:09
" }# l/ Y' m1 \做温湿度显示意味着穿戴设备这个STWINKT1B是不是可以做

/ m# {/ ?2 H5 g) Q4 c& @$ `这个板子肯定可以吧,性能满强的
你也是双下巴 回答时间:2024-10-18 10:15:59

养乐多 发表于 2024-10-18 10:15
这个板子肯定可以吧,性能满强的

确实,大佬

ghost110 回答时间:2024-10-18 10:36:38

感谢分享,学习

Minerva_ 回答时间:2025-3-9 21:28:41

你好博主,麻烦问一下您配置这个振动传感器的时候是怎么配置的呢,我看官方的应用笔记上面说明片选在拉低的时候才能用spi,但我配置之后仍然无法读到数据(可以的话能麻烦您分享一下源码我学习一下吗,谢谢您😄 )

jundao721 回答时间:2025-3-14 18:03:31

Minerva_ 发表于 2025-3-9 21:28
你好博主,麻烦问一下您配置这个振动传感器的时候是怎么配置的呢,我看官方的应用笔记上面说明片选在拉 ...

[md]不知道是否调通了,我是参考的官方的例程,cubemx配置好SPI后,将传感器的.c 和.h文件移植过来,在main函数中初始化即可。源代码我得找找,有什么问题可以随时留言交流。

Minerva_ 回答时间:2025-3-15 21:50:36

jundao721 发表于 2025-3-14 18:03
不知道是否调通了,我是参考的官方的例程,cubemx配置好SPI后,将传感器的.c 和.h文件移植过来,在ma ...

[md]我在cubeMX中直接配置了IIS3DWB的驱动文件,然后在官方的例程中进行了修改,但是貌似读取不到设备的ID,麻烦您看一下我这里的配置是有什么问题吗,谢谢您image.png

![image.png](data/attachment/forum/202503/15/214729d6o77wdnrv2ewwqz.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png"

image.png

define CS_up_GPIO_Port GPIOE

define CS_up_Pin GPIO_PIN_14

/ Private variables ---------------------------------------------------------/

static int16_t data_raw_acceleration[3];

//static int16_t data_raw_temperature;

static float acceleration_mg[3];

//static float temperature_degC;

static uint8_t whoamI = 0;

//static uint8_t tx_buffer[1000];

extern SPI_HandleTypeDef hspi3;

/ Extern variables ----------------------------------------------------------/

/ Private functions ---------------------------------------------------------/

/*

  • WARNING:
  • Functions declare in this section are defined at the end of this file
  • and are strictly related to the hardware platform used.

*/

static int32_t platform_write(void handle, uint8_t reg, const uint8_t bufp,

                      uint16_t len);

static int32_t platform_read(void handle, uint8_t reg, uint8_t bufp,

                     uint16_t len);

//static void tx_com( uint8_t *tx_buffer, uint16_t len );

static void platform_delay(uint32_t ms);

//static void platform_init(void);

/ Main Example --------------------------------------------------------------/

void iis3dwb_read_data_polling(void)

{

stmdev_ctx_t dev_ctx;

/ Initialize mems driver interface /

dev_ctx.write_reg = platform_write;

dev_ctx.read_reg = platform_read;

dev_ctx.handle = &hspi3;

/ Init test platform /

// platform_init();

/ Wait sensor boot time /

platform_delay(BOOT_TIME);

/ Check device ID /

iis3dwb_device_id_get(&dev_ctx, &whoamI);

uint8_t ctrl_reg;

iis3dwb_read_reg(&dev_ctx, IIS3DWB_CTRL1_XL, &ctrl_reg, 1);

printf("CTRL1_XL: 0x%02X\n", ctrl_reg);

// if (whoamI != IIS3DWB_ID)

// while (1);

/ Restore default configuration /

iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);

// do {

// iis3dwb_reset_get(&dev_ctx, &rst);

// } while (rst);

/ Enable Block Data Update /

iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);

/ Set Output Data Rate /

iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);

/ Set full scale /

iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);

/* Configure filtering chain(No aux interface)

  • Accelerometer low pass filter path

*/

iis3dwb_xl_filt_path_on_out_set(&dev_ctx, IIS3DWB_LP_ODR_DIV_100);

/ Read samples in polling mode (no int) /

while (1) {

uint8_t reg;

/ Read output only if new xl value is available /

iis3dwb_xl_flag_data_ready_get(&dev_ctx, &reg);

if (reg) {

/ Read acceleration field data / // memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));

iis3dwb_acceleration_raw_get(&dev_ctx, data_raw_acceleration);

acceleration_mg[0] =

iis3dwb_from_fs2g_to_mg(data_raw_acceleration[0]);

acceleration_mg[1] =

iis3dwb_from_fs2g_to_mg(data_raw_acceleration[1]);

acceleration_mg[2] =

iis3dwb_from_fs2g_to_mg(data_raw_acceleration[2]);

}

HAL_Delay(100); // iis3dwb_temp_flag_data_ready_get(&dev_ctx, &reg);

//

// if (reg) {

// / Read temperature data /

// memset(&data_raw_temperature, 0x00, sizeof(int16_t));

// iis3dwb_temperature_raw_get(&dev_ctx, &data_raw_temperature);

// temperature_degC = iis3dwb_from_lsb_to_celsius(data_raw_temperature);

// sprintf((char *)tx_buffer,

// "Temperature [degC]:%6.2f\r\n", temperature_degC);

//// tx_com(tx_buffer, strlen((char const *)tx_buffer));

// }

}

}

/*

  • @brief Write generic device register (platform dependent)
  • @param handle customizable argument. In this examples is used in
  • order to select the correct sensor bus handler.
    • @param reg register to write
    • @param bufp pointer to data to write in register reg
    • @param len number of consecutive register to write
    • */

static int32_t platform_write(void handle, uint8_t reg, const uint8_t bufp,

                      uint16_t len)

{

//#ifdef STEVAL_MKI109V3

//

// if (handle == &hspi2) {

HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET);

HAL_SPI_Transmit(handle, &reg, 1, 1000);

HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);

HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET); // }

//

//#elif defined(SPC584B_DIS)

// / Add here the SPC5 write SPI interface /

//#endif

return 0;

}

/*

  • @brief Read generic device register (platform dependent)
  • @param handle customizable argument. In this examples is used in
  • order to select the correct sensor bus handler.
    • @param reg register to read
    • @param bufp pointer to buffer that store the data read
    • @param len number of consecutive register to read
    • */

static int32_t platform_read(void handle, uint8_t reg, uint8_t bufp,

                     uint16_t len)

{

//#ifdef STEVAL_MKI109V3

//

// if (handle == &hspi2) {

/ Read command /

reg |= 0x80;

HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET);

HAL_SPI_Transmit(handle, &reg, 1, 1000);

HAL_SPI_Receive(handle, bufp, len, 1000);

HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET); // }

//#elif defined(SPC584B_DIS)

// / Add here the SPC5 read SPI interface /

//#endif

return 0;

}

image.png

jundao721 回答时间:2025-3-16 09:43:42
Minerva_ 发表于 2025-3-15 21:50. ?5 o. c/ s% S8 ^/ g& r
[md]我在cubeMX中直接配置了IIS3DWB的驱动文件,然后在官方的例程中进行了修改,但是貌似读取不到设备的I ...
$ W; v& x% h; {! U3 v
IIS3DWB SPI.png 这是我SPI3的基本配置,程序的话初始化完后,延时十几毫秒,再去读whoamI,别的基本上都是官库里的,正常读写。
0 A8 _) B4 i: N* F
Minerva_ 回答时间:2025-3-16 19:52:13

jundao721 发表于 2025-3-16 09:43
这是我SPI3的基本配置,程序的话初始化完后,延时十几毫秒,再去读whoamI,别的基本上都是官库里的,正常 ...

那想问一下您在platform_write以及platform_read这个函数中的CS_up_GPIO_Port,以及CS_up_Pin是GPIOF pin5还是GPIOE pin14呢?我在csdn上看到一个博主这里是GPIOE pin14

image.png

jundao721 回答时间:2025-3-17 11:37:31
Minerva_ 发表于 2025-3-16 19:52+ Q' W1 V; W% `. m6 y
那想问一下您在platform_write以及platform_read这个函数中的CS_up_GPIO_Port,以及CS_up_Pin是GPIOF  ...

& M, w) n  e) t! l: D$ A6 t# F4 y9 U2 i) G" H+ t
[md]CS我用的PF5,
5 d$ r4 f: N& t7 t6 p: \+ L" H$ Y7 m8 r* s
```* E. W7 ]4 V0 a4 k7 e
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
( N/ X. {1 h' c3 Z% F# n5 L9 H{
: S: g" J% W9 s  d$ t+ B  a//    uint8_t tx_buffer[1 + len];% ?) J1 J8 _- K5 U3 t
//    tx_buffer[0] = reg;
; `( R* x" k) K0 O; d9 _4 G//    memcpy(&tx_buffer[1], bufp, len);
9 y8 n7 |. H, T# J
; Q( ?( P6 y5 P8 j6 _2 t    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_5, GPIO_PIN_RESET); // 选择合适的CS引脚
& V+ D5 o' H3 i% s$ J: x* D0 V9 I4 O//    HAL_StatusTypeDef status = HAL_SPI_Transmit(handle, tx_buffer, len + 1, HAL_MAX_DELAY);3 \' l+ i! M1 h, K0 J

# ]2 K) U8 Q9 }7 D1 i( ?         HAL_SPI_Transmit(handle, &reg, 1, 1000);7 g; ^  T' p3 b' @( p
    HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);4 W1 C7 M- m2 r+ U2 m2 b: S
    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_5, GPIO_PIN_SET); // 选择合适的CS引脚9 t  i0 c# i3 a' V5 J8 [3 V7 \& H

. h/ {4 a$ g+ P/ d//    return (status == HAL_OK) ? 0 : -1;8 g8 N# R2 i5 c) Z  h0 N' Y
        return  0;
, C( `, N0 v$ Q  V) Y4 j, g7 e}
- ^% _& e  y# T" @' Q1 N```$ T1 O+ W6 ?# U' P: B

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版