Justice_Gao 发表于 2021-6-13 17:44

大赛分享三:RSL10蓝牙SoC之BMM150地磁传感器数据采集

<p>设备:</p>

<p>RSL10-SENSE-GEVK:RSL10 传感器开发套件</p>

<p></p>

<p>从原理图上看,地磁传感器和BHI160一起,官方没有例程代码,这里我分享一下,直接上代码吧</p>

<pre>
<code class="language-cpp">//-----------------------------------------------------------------------------
// 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&amp;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 &lt;stdio.h&gt;
#include &lt;BDK.h&gt;
#include &lt;BSP_Components.h&gt;
#include &lt;BLE_Components.h&gt;
#include &lt;SoftwareTimer.h&gt;

/** Converts measured orientation data into degrees.
*
* GREEN LED will be turned on if board heading is within 10 degrees of
* magnetic north (&lt;=10 or &gt;=350).
*/
void ao_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
    float h = data-&gt;data_vector.x / 32768.0f * 360.0f;
    float p = data-&gt;data_vector.y / 32768.0f * 360.0f;
    float y = data-&gt;data_vector.z / 32768.0f * 360.0f;

    if (h &gt; 350.0f || h &lt; 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-&gt;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-&gt;data_vector.x / 32768.0f * dyn_range;
    float y = data-&gt;data_vector.y / 32768.0f * dyn_range;
    float z = data-&gt;data_vector.z / 32768.0f * dyn_range;

    if (fabsf(x) + fabsf(y) + fabsf(z) &gt;= 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-&gt;data_vector.x / 32768.0f * dyn_range;
    float y = data-&gt;data_vector.y / 32768.0f * dyn_range;
    float z = data-&gt;data_vector.z / 32768.0f * dyn_range;

    if (fabsf(z) &gt; 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-&gt;data_vector.x / 32768.0f * dyn_range;
    float y = data-&gt;data_vector.y / 32768.0f * dyn_range;
    float z = data-&gt;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(&amp;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(&amp;tim);
      SwTimer_Start(&amp;tim);

      retval = BHI160_NDOF_Initialize();
      ASSERT_DEBUG(retval == BHY_SUCCESS);

      SwTimer_GetElapsed(&amp;tim, &amp;tim_dur);
      SwTimer_Remove(&amp;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, &amp;ao_vector_cb, 5);
    ASSERT_DEBUG(retval == BHY_SUCCESS);

    retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_LINEAR_ACCELERATION, &amp;la_vector_cb, 50);
    ASSERT_DEBUG(retval == BHY_SUCCESS);

    retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_RATE_OF_ROTATION, &amp;gyro_vector_cb, 50);
    ASSERT_DEBUG(retval == BHY_SUCCESS);

    retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_MAGNETIC_FIELD, &amp;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;
}
</code></pre>

<p>&nbsp;</p>

Jacktang 发表于 2021-6-14 10:53

<p>整的不错</p>

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

Justice_Gao 发表于 2021-6-14 12:05

Jacktang 发表于 2021-6-14 10:53
整的不错

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

<p>家里没有示波器 无法测试</p>
页: [1]
查看完整版本: 大赛分享三:RSL10蓝牙SoC之BMM150地磁传感器数据采集