设备:
RSL10-SENSE-GEVK:RSL10 传感器开发套件
从原理图上看,地磁传感器和BHI160一起,官方没有例程代码,这里我分享一下,直接上代码吧
//-----------------------------------------------------------------------------
// Copyright (c) 2018 Semiconductor Components Industries LLC
// (d/b/a "ON Semiconductor"). All rights reserved.
// This software and/or documentation is licensed by ON Semiconductor under
// limited terms and conditions. The terms and conditions pertaining to the
// software and/or documentation are available at
// http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf ("ON Semiconductor Standard
// Terms and Conditions of Sale, Section 8 Software") and if applicable the
// software license agreement. Do not use this software and/or documentation
// unless you have carefully read and you agree to the limited terms and
// conditions. By using this software and/or documentation, you agree to the
// limited terms and conditions.
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <BDK.h>
#include <BSP_Components.h>
#include <BLE_Components.h>
#include <SoftwareTimer.h>
/** Converts measured orientation data into degrees.
*
* GREEN LED will be turned on if board heading is within 10 degrees of
* magnetic north (<=10 or >=350).
*/
void ao_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
float h = data->data_vector.x / 32768.0f * 360.0f;
float p = data->data_vector.y / 32768.0f * 360.0f;
float y = data->data_vector.z / 32768.0f * 360.0f;
if (h > 350.0f || h < 10.0f)
{
LED_On(LED_GREEN);
}
else
{
LED_Off(LED_GREEN);
}
printf("Orientation: h=%.1f� p=%.1f�, y=%.1f� (%d)\r\n", h, p, y, data->data_vector.status);
}
/** Converts measured linear acceleration data based on sensors dynamic range
* and prints it to debug terminal.
*
* Also turns on RED LED if total acceleration applied to the board is over 1g.
*/
void la_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
/* Linear Acceleration sensor values are scaled using dynamic range. */
uint16_t dyn_range = BHI160_NDOF_GetAccelDynamicRange();
float x = data->data_vector.x / 32768.0f * dyn_range;
float y = data->data_vector.y / 32768.0f * dyn_range;
float z = data->data_vector.z / 32768.0f * dyn_range;
if (fabsf(x) + fabsf(y) + fabsf(z) >= 1.0f)
{
LED_On(LED_RED);
}
else
{
LED_Off(LED_RED);
}
printf("Linear Accel: x=%.2f g, y=%.2f g, z=%.2f g\r\n", x, y, z);
}
void gyro_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
uint16_t dyn_range = BHI160_NDOF_GetGyroDynamicRange();
float x = data->data_vector.x / 32768.0f * dyn_range;
float y = data->data_vector.y / 32768.0f * dyn_range;
float z = data->data_vector.z / 32768.0f * dyn_range;
if (fabsf(z) > 45.0f)
{
LED_On(LED_BLUE);
}
else
{
LED_Off(LED_BLUE);
}
printf("Rate of Rotation: x=%.2f �/s, y=%.2f �/s, z=%.2f �/s\r\n", x, y, z);
}
void magn_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
uint16_t dyn_range = BHI160_NDOF_GetMagDynamicRange();
float x = data->data_vector.x / 32768.0f * dyn_range;
float y = data->data_vector.y / 32768.0f * dyn_range;
float z = data->data_vector.z / 32768.0f * dyn_range;
printf("magnet: x=%.2f g, y=%.2f g, z=%.2f g\r\n", x, y, z);
}
void bhi160_init()
{
int32_t retval;
/* Initialize all needed BDK components. */
BDK_BLE_Initialize();
BLE_BASS_Initialize(50,1);
BLE_ICS_Initialize(&CustomService_WriteInd);
/* Initialize all LEDs to eliminate any currents from disabled DIO pads. */
LED_Initialize(LED_RED);
LED_Initialize(LED_GREEN);
LED_Initialize(LED_BLUE);
/* Increase I2C bus speed to 400kHz. */
HAL_I2C_SetBusSpeed(HAL_I2C_BUS_SPEED_FAST);
/* Initialize BHI160.
* This may take a while depending on bus speed because RAM patch has to be
* uploaded into BHI160 to enable support for BMM150 magnetometer.
*
* Set temporary timer to see how long it took to initialize and load the
* RAM patch.
*/
LED_On(LED_GREEN);
{
printf("Initializing BHI160.\r\n");
struct SwTimer tim;
struct SwTimer_Duration tim_dur;
SwTimer_Initialize(&tim);
SwTimer_Start(&tim);
retval = BHI160_NDOF_Initialize();
ASSERT_DEBUG(retval == BHY_SUCCESS);
SwTimer_GetElapsed(&tim, &tim_dur);
SwTimer_Remove(&tim);
printf("BHI160 initialized in %lu seconds and %lu ns.\r\n",
tim_dur.seconds, tim_dur.nanoseconds);
}
LED_Off(LED_GREEN);
/* Enable desired virtual sensor outputs. */
retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_ORIENTATION, &ao_vector_cb, 5);
ASSERT_DEBUG(retval == BHY_SUCCESS);
retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_LINEAR_ACCELERATION, &la_vector_cb, 50);
ASSERT_DEBUG(retval == BHY_SUCCESS);
retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_RATE_OF_ROTATION, &gyro_vector_cb, 50);
ASSERT_DEBUG(retval == BHY_SUCCESS);
retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_MAGNETIC_FIELD, &magn_vector_cb, 50);
ASSERT_DEBUG(retval == BHY_SUCCESS);
printf("APP: Entering main loop.\r\n");
}
int main(void)
{
BDK_Initialize();
bhi160_init();
printf("\r\nAPP: BME680_BSEC example.\r\n");
printf("APP: Entering main loop.\r\n");
while (1)
{
/* Execute any events that occurred and refresh Watchdog. */
BDK_Schedule();
SYS_WAIT_FOR_INTERRUPT;
}
return 0;
}
引用: Jacktang 发表于 2021-6-14 10:53 整的不错 BHI160是号称业界功耗最低的,楼主有感觉么
家里没有示波器 无法测试