[工业类传感器] [X-NUCLEO-IKS01A2测评] 用Nucleo-STM32L476和MEMS board X-NUCLEO-IK...编程

北方   2017-9-29 10:21 楼主
1、从零开始创建一个项目, 选择import,但是因为IKS01A2比较新,只有IKS01A1的库,其实是可以用来测试的。只要对比其中的区别就好,因为识别i2c设备是通过I2C线路的,其实主板并不知道是什么设备。 捕获 (1).JPG 2、创建后的文件结构如下, 捕获.JPG 3、文件代码如下,
  1. /* Includes */
  2. #include "mbed.h"
  3. #include "x_nucleo_iks01a1.h"
  4. /* Instantiate the expansion board */
  5. static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
  6. /* Retrieve the composing elements of the expansion board */
  7. static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
  8. static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
  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. printf("\r\n--- Starting new run ---\r\n");
  47. humidity_sensor->read_id(&id);
  48. printf("HTS221 humidity & temperature = 0x%X\r\n", id);
  49. pressure_sensor->read_id(&id);
  50. printf("LPS25H pressure & temperature = 0x%X\r\n", id);
  51. magnetometer->read_id(&id);
  52. printf("LIS3MDL magnetometer = 0x%X\r\n", id);
  53. gyroscope->read_id(&id);
  54. printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
  55. wait(3);
  56. while(1) {
  57. printf("\r\n");
  58. temp_sensor1->get_temperature(&value1);
  59. humidity_sensor->get_humidity(&value2);
  60. printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
  61. temp_sensor2->get_fahrenheit(&value1);
  62. pressure_sensor->get_pressure(&value2);
  63. printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
  64. printf("---\r\n");
  65. magnetometer->get_m_axes(axes);
  66. printf("LIS3MDL [mag/mgauss]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
  67. accelerometer->get_x_axes(axes);
  68. printf("LSM6DS0 [acc/mg]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
  69. gyroscope->get_g_axes(axes);
  70. printf("LSM6DS0 [gyro/mdps]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
  71. wait(1.5);
  72. }
  73. }
4、点击Compile,直接生成可执行文件。把这个文件拖入自动添加的nucleo盘符,就可以flash和自动执行了。 捕获.JPG 本帖最后由 北方 于 2017-9-29 10:21 编辑

回复评论 (1)

汇总贴在此:
[X-NUCLEO-IKS01A2测评]—by 北方
https://bbs.eeworld.com.cn/forum. ... 7445&fromuid=536508
(出处: 电子工程世界-论坛)
玩板看这里: https://bbs.eeworld.com.cn/elecplay.html EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!
点赞  2017-10-2 15:55
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复