[工业类传感器] [X-NUCLEO-IKS01A2测评] 移植IKS01A1例程

北方   2017-10-11 16:27 楼主
1、在mbed上的例程是基于X-NUCLEO-IKS01A1的,其中因为元件差别比较大,除了温湿度和压力传感器以外都是新的型号。所以在上一次评测中,只能使用温度数据的读出。
这次移植了该例程,并导入了新的库,测试成功。
捕获.JPG
2. 代码

  1. /* Includes */
  2. #include "mbed.h"
  3. #include "XNucleoIKS01A2.h"

  4. /* Instantiate the expansion board */
  5. static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance();

  6. /* Retrieve the composing elements of the expansion board */
  7. static GyroSensor *gyroscope = mems_expansion_board->acc_gyro;
  8. static MotionSensor *accelerometer = mems_expansion_board->accelerometer;
  9. static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
  10. static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
  11. static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
  12. static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
  13. static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;

  14. /* Helper function for printing floats & doubles */
  15. static char *printDouble(char* str, double v, int decimalDigits=2)
  16. {
  17.   int i = 1;
  18.   int intPart, fractPart;
  19.   int len;
  20.   char *ptr;

  21.   /* prepare decimal digits multiplicator */
  22.   for (;decimalDigits!=0; i*=10, decimalDigits--);

  23.   /* calculate integer & fractinal parts */
  24.   intPart = (int)v;
  25.   fractPart = (int)((v-(double)(int)v)*i);

  26.   /* fill in integer part */
  27.   sprintf(str, "%i.", intPart);

  28.   /* prepare fill in of fractional part */
  29.   len = strlen(str);
  30.   ptr = &str[len];

  31.   /* fill in leading fractional zeros */
  32.   for (i/=10;i>1; i/=10, ptr++) {
  33.     if(fractPart >= i) break;
  34.     *ptr = '0';
  35.   }

  36.   /* fill in (rest of) fractional part */
  37.   sprintf(ptr, "%i", fractPart);

  38.   return str;
  39. }


  40. /* Simple main function */
  41. int main() {
  42.   uint8_t id;
  43.   float value1, value2;
  44.   char buffer1[32], buffer2[32];
  45.   int32_t axes[3];
  46.   
  47.   printf("\r\n--- Starting new run ---\r\n");

  48.   humidity_sensor->read_id(&id);
  49.   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
  50.   pressure_sensor->read_id(&id);
  51.   printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
  52.   magnetometer->read_id(&id);
  53.   printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
  54.   gyroscope->read_id(&id);
  55.   printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
  56.   
  57.   wait(3);

  58.   while(1) {
  59.     printf("\r\n");

  60.     temp_sensor1->get_temperature(&value1);
  61.     humidity_sensor->get_humidity(&value2);
  62.     printf("HTS221: [temp] %7s°C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
  63.    
  64.     temp_sensor2->get_fahrenheit(&value1);
  65.     pressure_sensor->get_pressure(&value2);
  66.     printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));

  67.     printf("---\r\n");

  68.     magnetometer->get_m_axes(axes);
  69.     printf("LIS3MDL [mag/mgauss]:  %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);

  70.     accelerometer->get_x_axes(axes);
  71.     printf("LSM6DS0 [acc/mg]:      %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);

  72.     gyroscope->get_g_axes(axes);
  73.     printf("LSM6DS0 [gyro/mdps]:   %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);

  74.     wait(1.5);
  75.   }
  76. }


3. 分析
其中的创建instance是关键,这次的范例,在没有设定引脚的null参数下,也判读后创建,避免了自己引脚不熟悉的结果,其中I2C用的是D14和D15
static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance()
之后,需要声明传感器的读取变量,
/* Retrieve the composing elements of the expansion board */
static GyroSensor *gyroscope = mems_expansion_board->acc_gyro;
static MotionSensor *accelerometer = mems_expansion_board->accelerometer;
static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;

这些传感器都在声明变量的时候初始化了,因此,不必再设定init指令,
在显示的数据中,还不是很正确,因为很多参数没有正确量化。不过,这个例程,可以供仔细分析和调试了。

回复评论 (1)

汇总贴:X-NUCLEO-IKS01A2测评
https://bbs.eeworld.com.cn/thread-568428-1-1.html
个人汇总贴:
[X-NUCLEO-IKS01A2测评]—by 北方
https://bbs.eeworld.com.cn/thread-567445-1-1.html
玩板看这里: https://bbs.eeworld.com.cn/elecplay.html EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!
点赞  2017-11-23 11:28
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复