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

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

[复制链接]
jundao721 发布时间:2024-7-30 21:42
      根据AN5444应用笔记介绍,IIS3DWB是一款高性能MEMS三轴振动传感器,具有超宽带宽低噪声等特点,SPI标准接口输出。具体的介绍可参考AN5444应用笔记。本次先对该传感器有个初步的数据读取测试,关于滤波等后面学习时再添加。
5 n0 N* V3 S, e     原理图关于该部分的说明,如图1所示:% {- w* y# a* x. _. ]6 o, W
2-2.JPG
1 [, `8 Q2 @! m; |图1
0 t0 e9 i; ], I. ~, l2 m7 ]/ ]8 q; G$ o
    先使用CubeMx建立工程,时钟等正常配置即可,因为要使用SPI通讯,所以引脚配置如图2:
1 k! T3 N! k+ h3 @( D; T0 S
# ]! D, r, l- F5 u7 m 2-3.JPG * W  [" S8 l" n; J

( s+ r$ G/ H: z# F7 L9 q4 J, e图2" A8 q- `) s6 o" i" i6 I+ f: }, ]
   SPI外设参数的基本配置如图3:, Z# T" c; I1 K& M3 [7 ^# e0 _
2-4.JPG
1 F  E& A# L$ @0 S

1 Q8 k) V' b9 Q图3
% H- d: X1 d# G0 v5 }5 ~( N( j  然后生成项目。在官方提供的文件中有IIS3DWB的驱动,4个文件,如图4所示:6 l4 A, U2 Q- S* y
2-5.JPG
5 p8 v$ g& F4 V# I1 \( U( S

9 v8 @8 x$ T  D1 G8 B图4
' o# p0 L3 p1 G, f+ z$ G  例程是参考官方github中提供的参考例程,修改了一下,增加了温度读取。初始化如下:" b3 {$ D  `2 d5 \  X
  1. dev_ctx.write_reg = platform_write;
    " |7 g5 o. g$ V
  2. dev_ctx.read_reg = platform_read;
    8 K5 _7 U( R( U4 w/ u/ d) {/ U
  3. dev_ctx.handle = &hspi3;3 `( K/ g% g1 Y5 q  y4 o

  4. ) M8 O, r0 S2 K* p
  5. HAL_Delay (10);; E+ b+ V1 x& i
  6. iis3dwb_device_id_get(&dev_ctx, &whoamI);
    : I# j! Q8 l' I7 A2 H! h0 X+ `
  7. // 复位设备
    5 y; i( w! y% H, W6 a3 J/ P
  8. iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);- L/ v; D0 F/ i. r3 f) y& {8 B
  9. // 配置加速度计2 Q* {( @$ Y; G) b5 |7 Z7 t' B
  10. iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);+ n+ k# @7 z  v( Z) K
  11. iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);
    : X  e& ]( W9 m* X" N
  12. iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);
复制代码

2 a0 i) _7 ~1 b" J; A$ G* w$ L6 B
: p2 F9 A% L6 Q- @/ W2 t9 v  对于加速度值和温度的读取程序如下:
( I+ ^3 \! F( K8 [: u; _% i/ F5 n
  1.   // 读取加速度数据
    8 {% A& j1 E0 n$ m( m, s+ r
  2.    
    # q. {  @$ ^, G: I
  3.         iis3dwb_xl_flag_data_ready_get(&dev_ctx, &drdy);//读取状态值+ ?  u- z8 k) s& l# ~, A  {
  4.         if (drdy) {
    ! E+ P( R0 }2 Z" \/ ]
  5.            ) ~; I* X# ~+ u$ Q- |  o2 T
  6.             iis3dwb_acceleration_raw_get(&dev_ctx, data_raw);
      m( q9 |9 N* E$ o
  7.             ( @. w0 p$ B. [6 \9 z
  8.             acceleration_mg[0] = iis3dwb_from_fs2g_to_mg(data_raw[0]);
    + ]1 h' [1 {( V1 v' U
  9.             acceleration_mg[1] = iis3dwb_from_fs2g_to_mg(data_raw[1]);
    - E# m( Z& w# H- I. _
  10.             acceleration_mg[2] = iis3dwb_from_fs2g_to_mg(data_raw[2]);. J' `1 t0 C' }; I2 H
  11.         }4 w% _$ ^/ ]1 y  X7 N5 S
  12.                 iis3dwb_read_reg (&dev_ctx ,IIS3DWB_OUT_TEMP_L ,temp_data ,2);
    5 j8 {) L) I! B/ _2 K/ u9 w
  13.                 temp_raw = (int16_t )((temp_data [1]<<8) | temp_data[0]);9 P5 m# P+ r" [' h' b
  14.                 temp_cel = temp_raw /256+25;
    ) @- Q; i; T$ r$ F
  15. ( q" F4 A7 o7 Q, `* b3 I1 X
  16.         HAL_Delay(1000); // 每秒读取一次数据
复制代码

  1.   O6 r" l" K& J9 Q
复制代码
3 K0 l8 T0 j& L
: Y1 H! }: D7 `: ]+ @
  测试结果如图5所示:
" L' T! g" Q/ Y; F* ` 2-1.JPG
! F- f/ \$ |9 P, u8 X4 ?, B# }' Z: A! r
3 k! j8 C& K) |' z5 G1 V7 O$ e
图5' D/ g/ v9 U+ [& p/ N
Device ID和温度值是正确的,滤波,FIFO等功能的增加一边理解一边添加测试。. n" o( g. H$ e0 G$ h

% \' H4 J: |& j. J1 s: T' m
7 g0 I$ V- T) ?/ J* b' a
赞 收藏 评论13 发布时间:2024-7-30 21:42

举报

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

<br /> <br />

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

image.png

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

感谢分享

你也是双下巴 回答时间:2024-10-18 10:09:24
做温湿度显示意味着穿戴设备这个STWINKT1B是不是可以做
! _. H& D4 W! V7 o' {. O& m
养乐多 回答时间:2024-10-18 10:15:17
你也是双下巴 发表于 2024-10-18 10:097 ^, Q& w, Y6 P  T( B1 @1 q* L3 r" t
做温湿度显示意味着穿戴设备这个STWINKT1B是不是可以做

( u1 L' w! q: u  ^! R这个板子肯定可以吧,性能满强的
你也是双下巴 回答时间:2024-10-18 10:15:59

<br /> <br />

确实,大佬

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

感谢分享,学习

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

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

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

<br /> <br />

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

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

<br /> <br />

我在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:501 L6 }$ ~' `6 t
[md]我在cubeMX中直接配置了IIS3DWB的驱动文件,然后在官方的例程中进行了修改,但是貌似读取不到设备的I ...

6 w: E. r) j' j  S9 R7 M3 O; W IIS3DWB SPI.png 这是我SPI3的基本配置,程序的话初始化完后,延时十几毫秒,再去读whoamI,别的基本上都是官库里的,正常读写。; Q6 s- q! [$ |4 A( |7 U
Minerva_ 回答时间:2025-3-16 19:52:13

<br /> <br />

那想问一下您在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:527 ?  }9 m* Q0 w" Z4 e
那想问一下您在platform_write以及platform_read这个函数中的CS_up_GPIO_Port,以及CS_up_Pin是GPIOF  ...
, A4 ~1 q% K, L0 g

/ @' ]9 X. F! b[md]CS我用的PF5," R/ \) R: i" |) ~" v  u; y0 u/ e
1 ?4 Z) f0 y- U: F* T
```! X, x8 i! L+ d5 {4 d
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
* i/ y/ y7 a& Q( R8 X. O' @+ @{. W( h: j8 N. Q
//    uint8_t tx_buffer[1 + len];4 \$ F6 g. C- t4 Y+ Q
//    tx_buffer[0] = reg;) ~3 R$ K# @9 ^; ]) ?
//    memcpy(&tx_buffer[1], bufp, len);
' l4 t9 e8 w8 g# g
) o+ U# O/ X' }- I  k0 I; K! {; L    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_5, GPIO_PIN_RESET); // 选择合适的CS引脚  D! ~5 z7 ?# _& b: K
//    HAL_StatusTypeDef status = HAL_SPI_Transmit(handle, tx_buffer, len + 1, HAL_MAX_DELAY);
5 m% G6 ~) M6 U5 k7 S. P- V7 f. o$ {3 J
         HAL_SPI_Transmit(handle, &reg, 1, 1000);
6 w4 X) z' `. r    HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);
! Y' g& d/ z" \" o; c, ]    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_5, GPIO_PIN_SET); // 选择合适的CS引脚
3 G0 c; T, ^# Z; U( f4 t* o( o7 q- O* b0 S* J; N2 `- B0 t
//    return (status == HAL_OK) ? 0 : -1;3 q1 _1 v2 s( X. B+ M; Z) e/ w
        return  0;7 l' c; R, k$ Y2 X+ R
}- X% b: \/ q! Z7 c8 z# z
```
6 r' ^( H; t6 ~2 q4 B
Shi,Er 回答时间:2025-8-4 10:45:24

您好,请问根据时序图来看,CPOL空闲时应该是高电平,为什么在您这里配置选择LOW。但是,在实例中只有CPOL选择低电平的时候采用数据输出,高电平没有,这与数据手册中的SPI时序图不相符,请问这是怎么回事?

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版