社区导航

 

搜索
查看: 520|回复: 1

[原创] 1、基于Nucleo-L476L的HTS221等全部传感器的驱动

[复制链接]

236

TA的帖子

0

TA的资源

一粒金砂(中级)

Rank: 2

发表于 2019-7-15 15:39 | 显示全部楼层 |阅读模式
本帖最后由 北方 于 2019-7-16 14:51 编辑

1、基于Nucleo-STM32L476L的Arduino实现NUCLEO-IKS01A3的全部传感器驱动实现。

    初始计划是采用MKR1000,但是在使用和下载的过程中,这个开发板给变砖了。

所以只好先拿出吃灰的Nucleo-STM32L476L先实现以下基本的arduino驱动。在完成这个基本评测后再移植到其他arduino开发板上使用。

针对不同的传感器,参照以下具体代码逐个说明和实现。

 

    这个过程中首先需要使用开发板管理器安装arduino开发板,

这样就可以设置开发板了,

然后安装NUCLEO-IKS01A3的传感器驱动,

这样就可以再程序中直接使用驱动库了。

驱动和安装说明可以从如下链接获得,https://github.com/stm32duino/Arduino_Core_STM32

这个是官方的arduino驱动。

2、传感器驱动代码和说明

2.1 首先导入HTS221Sensor.h,这个是HTS221的驱动,设定I2C的引脚后就可以直接访问HTS221传感器,程序读取传感器的数据,转换后通过串口输出。

代码如下:

#include <HTS221Sensor.h>

#define I2C2_SCL    PB10
#define I2C2_SDA    PB11

// Components.
HTS221Sensor  *HumTemp;
TwoWire *dev_i2c;

void setup() {
  // Led.
  pinMode(LED_BUILTIN, OUTPUT);
  // Initialize serial for output.
  Serial.begin(9600);

  // Initialize I2C bus.
  dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
  dev_i2c->begin();

  // Initlialize components.
  HumTemp = new HTS221Sensor (dev_i2c);
  HumTemp->Enable();
}

void loop() {
  // Led blinking.
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);

  // Read humidity and temperature.
  float humidity, temperature;
  HumTemp->GetHumidity(&humidity);
  HumTemp->GetTemperature(&temperature);

  // Output data.
  Serial.print("Hum[%]: ");
  Serial.print(humidity, 2);
  Serial.print(" | Temp[C]: ");
  Serial.println(temperature, 2);
}

输出结果如下,


因为本传感器只检查温度和湿度,所以使用照片显示结果,

 

2.2  3D加速度+3D陀螺仪    LSM6DS +Nucleo-STM32L476L读取传感器数据

首先导入驱动程序

#include <LSM6DSOSensor.h>

同上,先设定I2C的接口,然后把变化传感器数据输出到串口

代码如下:



#include <LSM6DSOSensor.h>

#ifdef ARDUINO_SAM_DUE
#define DEV_I2C Wire1
#elif defined(ARDUINO_ARCH_STM32)
#define DEV_I2C Wire
#elif defined(ARDUINO_ARCH_AVR)
#define DEV_I2C Wire
#else
#define DEV_I2C Wire
#endif
#define SerialPort Serial

#define INT_1 4

LSM6DSOSensor *accGyr;

//Interrupts.
volatile int mems_event = 0;

char report[256];

void INT1Event_cb();
void sendOrientation();

void setup() {
  // Led.
  pinMode(LED_BUILTIN, OUTPUT);
  // Initialize serial for output.
  SerialPort.begin(115200);

  // Initialize I2C bus.
  DEV_I2C.begin();
  
  //Interrupts.
  attachInterrupt(INT_1, INT1Event_cb, RISING);

  accGyr = new LSM6DSOSensor (&DEV_I2C);
  accGyr->Enable_X();
  accGyr->Enable_6D_Orientation(LSM6DSO_INT1_PIN);
}

void loop() {
  if (mems_event)
  {
    mems_event=0;
    LSM6DSO_Event_Status_t status;
    accGyr->Get_X_Event_Status(&status);
    if (status.D6DOrientationStatus)
    {
      sendOrientation();
      // Led blinking.
      digitalWrite(LED_BUILTIN, HIGH);
      delay(100);
      digitalWrite(LED_BUILTIN, LOW);  
    }
  }
}

void INT1Event_cb()
{
  mems_event = 1;
}

void sendOrientation()
{
  uint8_t xl = 0;
  uint8_t xh = 0;
  uint8_t yl = 0;
  uint8_t yh = 0;
  uint8_t zl = 0;
  uint8_t zh = 0;

  accGyr->Get_6D_Orientation_XL(&xl);
  accGyr->Get_6D_Orientation_XH(&xh);
  accGyr->Get_6D_Orientation_YL(&yl);
  accGyr->Get_6D_Orientation_YH(&yh);
  accGyr->Get_6D_Orientation_ZL(&zl);
  accGyr->Get_6D_Orientation_ZH(&zh);

  if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |  *             | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |________________| \r\n" );
  }

  else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |             *  | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |________________| \r\n" );
  }

  else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |  *             | " \
                      "\r\n |________________| \r\n" );
  }

  else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |             *  | " \
                      "\r\n |________________| \r\n" );
  }

  else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
  {
    sprintf( report, "\r\n  __*_____________  " \
                      "\r\n |________________| \r\n" );
  }

  else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |________________| " \
                      "\r\n    *               \r\n" );
  }

  else
  {
    sprintf( report, "None of the 6D orientation axes is set in LSM6DSO - accelerometer.\r\n" );
  }

  SerialPort.print(report);
}

通过串口演示读取加速度计的变化位置,

主要代码

    sprintf( report, "\r\n  ________________  " \
                      "\r\n |________________| " \
                      "\r\n    *               \r\n" );


2.3 3D磁力计    LIS2MD + Nucleo-STM32L476L 读取传感器数据

首先导入驱动程序

#include <LIS2DW12Sensor.h>

同上,先设定I2C的接口,然后把变化传感器数据输出到串口

代码类似上面,具体读取再后面的综合数据输出中展示。


2.4 3D加速度    LIS2DW12+Nucleo-STM32L476L 读取传感器数据

首先导入驱动程序

同上,先设定I2C的接口,然后把变化传感器数据输出到串口

代码如下:

#include <LIS2DW12Sensor.h>

#ifdef ARDUINO_SAM_DUE
#define DEV_I2C Wire1
#elif defined(ARDUINO_ARCH_STM32)
#define DEV_I2C Wire
#elif defined(ARDUINO_ARCH_AVR)
#define DEV_I2C Wire
#else
#define DEV_I2C Wire
#endif
#define SerialPort Serial

// Components.
LIS2DW12Sensor *accelero;

//Interrupts.
volatile int mems_event = 0;

char report[256];

void INT1Event_cb();
void sendOrientation();

void setup() {
  // Led.
  pinMode(LED_BUILTIN, OUTPUT);
  // Initialize serial for output.
  SerialPort.begin(115200);

  // Initialize I2C bus.
  DEV_I2C.begin();

  //Interrupts.
  attachInterrupt(A3, INT1Event_cb, RISING);

  // Initlialize components.
  accelero = new LIS2DW12Sensor(&DEV_I2C);
  accelero->Enable_X();

  // Enable 6D Orientation.
  accelero->Enable_6D_Orientation();
}

void loop() {
  if (mems_event)
  {
    mems_event = 0;
    LIS2DW12_Event_Status_t status;
    accelero->Get_Event_Status(&status);
    if (status.D6DOrientationStatus)
    {
      // Send 6D Orientation
	  sendOrientation();
      
      // Led blinking.
      digitalWrite(LED_BUILTIN, HIGH);
      delay(100);
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

void INT1Event_cb()
{
  mems_event = 1;
}

void sendOrientation()
{
  uint8_t xl = 0;
  uint8_t xh = 0;
  uint8_t yl = 0;
  uint8_t yh = 0;
  uint8_t zl = 0;
  uint8_t zh = 0;
  
  accelero->Get_6D_Orientation_XL(&xl);
  accelero->Get_6D_Orientation_XH(&xh);
  accelero->Get_6D_Orientation_YL(&yl);
  accelero->Get_6D_Orientation_YH(&yh);
  accelero->Get_6D_Orientation_ZL(&zl);
  accelero->Get_6D_Orientation_ZH(&zh);
  
  if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |  *             | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |________________| \r\n" );
  }
  
  else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |             *  | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |________________| \r\n" );
  }
  
  else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |  *             | " \
                      "\r\n |________________| \r\n" );
  }
  
  else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |             *  | " \
                      "\r\n |________________| \r\n" );
  }
  
  else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
  {
    sprintf( report, "\r\n  __*_____________  " \
                      "\r\n |________________| \r\n" );
  }
  
  else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
  {
    sprintf( report, "\r\n  ________________  " \
                      "\r\n |________________| " \
                      "\r\n    *               \r\n" );
  }
  
  else
  {
    sprintf( report, "None of the 6D orientation axes is set in LIS2DW12 - accelerometer.\r\n" );
  }
  
  SerialPort.print(report);
}
用图形的符号表示地磁的方向,

参见代码

    sprintf( report, "\r\n  ________________  " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |                | " \
                      "\r\n |  *             | " \
                      "\r\n |________________| \r\n" );

 

2.5 气压传感器    LPS22HH   + Nucleo-STM32L476L读取传感器数据

首先导入驱动程序

同上,先设定I2C的接口,然后把变化传感器数据输出到串口

代码类似,具体参见下一贴。


2.6 温度传感器    STTS751+  Nucleo-STM32L476L读取传感器数据

首先导入驱动程序#include <STTS751Sensor.h>

在程序中,首先定义i2c的引脚和接线,然后读取传感器数据,并输出到串口。

 

实现代码如下,

#include <STTS751Sensor.h>

#ifdef ARDUINO_SAM_DUE
#define DEV_I2C Wire1
#elif defined(ARDUINO_ARCH_STM32)
#define DEV_I2C Wire
#elif defined(ARDUINO_ARCH_AVR)
#define DEV_I2C Wire
#else
#define DEV_I2C Wire
#endif
#define SerialPort Serial

#define INT_1 A4

//Interrupts.
volatile int mems_event = 0;

uint8_t high = 0, low = 0;
uint32_t previous_tick;
float temperature = 0;

STTS751Sensor *Temp;

void INT1Event_cb()
{
  mems_event = 1;
}

void setup() {
  // Led.
  pinMode(LED_BUILTIN, OUTPUT);
  // Initialize serial for output.
  SerialPort.begin(115200);

  // Initialize I2C bus.
  DEV_I2C.begin();
  
  //Interrupts.
  attachInterrupt(INT_1, INT1Event_cb, FALLING);

  Temp = new STTS751Sensor(&DEV_I2C);
  Temp->Enable();
  Temp->SetOutputDataRate(4.0f);
  Temp->SetLowTemperatureThreshold(22.0f);
  Temp->SetHighTemperatureThreshold(28.0f);
  Temp->SetEventPin(1);
  Temp->GetTemperatureLimitStatus(NULL, NULL, NULL);

  previous_tick=millis();
}

void loop() {
  if (mems_event)
  {
    mems_event=0;
    uint8_t highTemp = 0, lowTemp = 0;
    Temp->GetTemperatureLimitStatus(&highTemp, &lowTemp, NULL);
    if (highTemp){
      high = 1;
      low = 0;
    }
    if (lowTemp){
      low = 1;
      high = 0;
    }
    
    Temp->GetTemperature(&temperature);
    // Led blinking.
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
    digitalWrite(LED_BUILTIN, LOW);   
  }
  uint32_t current_tick = millis();
  if ((current_tick - previous_tick) >= 2000){
    if (!high && !low){
      Temp->GetTemperature(&temperature);
    }
    SerialPort.print("Temp[C]: ");
    SerialPort.print(temperature, 2);
    if (high){
      SerialPort.println(" High temperature detected!(>28C) ");
      high = 0;
    } else if (low) {
      SerialPort.println(" Low temperature detected!(<22C) ");
      low = 0;
    } else {
      SerialPort.println();
    }
    previous_tick = millis();
  }
}

程序下载和串口输出如下,

3、实现视频不单独演示,详见下一帖综合展示。

 

 

 



此内容由EEWORLD论坛网友北方原创,如需转载或用于商业用途需征得作者同意并注明出处

来源:EEWorld ST传感器与低功耗无线技术论坛板块,转载请附上链接
3.PNG
4.PNG
1.PNG
2.PNG
2130643943.jpg
660470877.jpg
99250324.jpg
1623872904.jpg
6.PNG
7.PNG
10.PNG
8.PNG


回复

使用道具 举报

1706

TA的帖子

4

TA的资源

一粒金砂(高级)

Rank: 3Rank: 3

发表于 2019-7-15 18:16 | 显示全部楼层

好吧,比我快一部,板子还没有到;



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

关闭

站长推荐上一条 /2 下一条

  • 论坛活动 E手掌握

    扫码关注
    EEWORLD 官方微信

  • EE福利  唾手可得

    扫码关注
    EE福利 唾手可得

Archiver|手机版|小黑屋|电子工程世界 ( 京ICP证 060456 )

GMT+8, 2019-8-25 11:22 , Processed in 0.107400 second(s), 17 queries , Gzip On, MemCache On.

快速回复 返回顶部 返回列表