2370|2

98

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

大赛分享三:RSL10蓝牙SoC之BMM150地磁传感器数据采集 [复制链接]

 

设备:

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;
}

 

最新回复

整的不错 BHI160是号称业界功耗最低的,楼主有感觉么   详情 回复 发表于 2021-6-14 10:53
点赞 关注
 
 

回复
举报

6802

帖子

0

TA的资源

五彩晶圆(高级)

沙发
 

整的不错

BHI160是号称业界功耗最低的,楼主有感觉么

点评

家里没有示波器 无法测试  详情 回复 发表于 2021-6-14 12:05
 
 
 

回复

98

帖子

0

TA的资源

一粒金砂(中级)

板凳
 
Jacktang 发表于 2021-6-14 10:53 整的不错 BHI160是号称业界功耗最低的,楼主有感觉么

家里没有示波器 无法测试

 
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表