# BMA456 Sensor API
## Introduction
This package contains Bosch Sensortec's BMA456 sensor API
## Sensor API integration
Inside your project, include files *bma4_defs.h*, *bma4.h*, *bma4.c*, *bma456.c* and *bma456.h*.
If you are using an auxiliary magnetometer, either the BMM150 or the AKM9916, include to respective source and include, *aux_bmm150.c* and *aux_bmm150.h*, or, *aux_akm9916.c* and *aux_akm9916.h*.
In your code, include header *bma456.h*
``` c
#include "bma456.h"
```
## User guide
### Initializing sequence
First application setup examples algorithms:
After correct power up by setting the correct voltage to the appropriate external pins, the
BMA456 enters automatically into the Power On Reset (POR) sequence. In order to properly
make use of the BMA456, certain steps from host processor side are needed.
a. Reading the chip id.
b. Performing initialization sequence.
c. Checking the correct status of the interrupt feature engine.
``` c
uint16_t main(void) {
uint16_t rslt = BMA4_OK;
uint8_t init_seq_status = 0;
/* Declare an instance of the BMA456 device */
struct bma4_dev dev;
/* Modify the parameters */
dev.dev_addr = BMA4_I2C_ADDR_PRIMARY;
dev.interface = BMA4_I2C_INTERFACE;
dev.bus_read = USER_i2c_read;
dev.bus_write = USER_i2c_write;
dev.delay = USER_delay_ms;
dev.read_write_len = 8;
dev.resolution = 12;
dev.feature_len = BMA456_FEATURE_SIZE;
/* a. Reading the chip id. */
rslt |= bma456_init(&dev);
/* b. Performing initialization sequence.
c. Checking the correct status of the initialization sequence.
*/
rslt |= bma456_write_config_file(&dev);
return rslt;
}
```
### Sensor API initialization for the I2C protocol
``` c
uint16_t main(void) {
uint16_t rslt = BMA4_OK;
/* Declare an instance of the BMA456 device */
struct bma4_dev dev;
/* Modify the parameters */
dev.dev_addr = BMA4_I2C_ADDR_PRIMARY;
dev.interface = BMA4_I2C_INTERFACE;
dev.bus_read = USER_i2c_read;
dev.bus_write = USER_i2c_write;
dev.delay = USER_delay_ms;
dev.read_write_len = 8;
/* Initialize the instance */
rslt |= bma456_init(&dev);
return rslt;
}
```
### Configuring the accelerometer
``` c
uint16_t main(void) {
uint16_t rslt = BMA4_OK;
/* Initialize the device instance as per the initialization example */
/* Enable the accelerometer */
bma4_set_accel_enable(ACCEL_ENABLE, &dev);
/* Declare an accelerometer configuration structure */
struct bma4_accel_config accel_conf;
/* Assign the desired settings */
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
accel_conf.range = BMA4_ACCEL_RANGE_2G;
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
/* Set the configuration */
rslt |= bma4_set_accel_config(&accel_conf, &dev);
return rslt;
}
```
### Reading out the accelerometer data
``` c
uint16_t main(void) {
uint16_t rslt = BMA4_OK;
/* Initialize the device instance as per the initialization example */
/* Configure the accelerometer */
/* Declare an instance of the sensor data structure */
struct bma4_accel sens_data;
/* Loop forever */
while (1) {
/* Read the sensor data into the sensor data instance */
rslt |= bma4_read_accel_xyz(&sens_data, &dev);
/* Exit the program in case of a failure */
if (rslt != BMA4_OK)
return rslt;
/* Use the data */
printf("X: %d, Y: %d, Z: %d\n", sens_data.x, sens_data.y, sens_data.z);
/* Pause for 10ms, 100Hz output data rate */
USER_delay_ms(10);
}
return rslt;
}
```
### Configuring reading out sensor data from the FIFO buffer
``` c
uint16_t main(void) {
uint16_t rslt = BMA4_OK;
/* Initialize the device instance as per the initialization example */
/* Configure the accelerometer */
/* Setup and configure the FIFO buffer */
/* Declare memory to store the raw FIFO buffer information */
uint8_t fifo_buff[255];
struct bma4_fifo_frame fifo_frame;
/* Modify the FIFO buffer instance and link to the device instance */
fifo_frame.data = fifo_buff;
fifo_frame.length = 255;
fifo_frame.fifo_data_enable = BMA4_ENABLE;
fifo_frame.fifo_header_enable = BMA4_ENABLE;
/* Disable the advanced power save mode to configure the FIFO buffer */
rslt |= bma4_set_advance_power_save(BMA4_DISABLE, &dev);
/* Configure the FIFO buffer */
rslt |= bma4_set_fifo_config((BMA4_FIFO_ACCEL | BMA4_FIFO_HEADER), BMA4_ENABLE, &dev);
/* Declare instances of the sensor data structure */
struct bma4_accel sens_data[36]; // 255 bytes / ~7bytes per frame = 36 instances
uint16_t n_instances, i;
/* Loop forever */
while (1) {
/* Read data from the sensor FIFO buffer */
rslt |= bma4_read_fifo_data(&fifo_frame, &dev); // Read FIFO data
/* Exit the program in case of a failure */
if (rslt != BMA4_OK)
return rslt;
/* Reset the maximum number of required sensor data instances */
n_instances = 36;
/* Parse the FIFO until there are less frames than requested */
while (n_instances == 36) {
/* Parse the FIFO buffer and extract required number of accelerometer data frames */
rslt |= bma4_extract_accel(sens_data, &n_instances, &fifo_frame, &dev);
/* Exit the program in case of a failure */
if (rslt != BMA4_OK)
return rslt;
/* Use the accelerometer data frames */
for (i = 0; i < n_instances; i++)
printf("X:%d, Y:%d, Z:%d\n", sens_data[i].x, sens_data[i].y, sens_data[i].z);
}
// At 100Hz, The FIFO buffer will have 36 frames ready in (1 / 100) * 36 = ~0.36s
USER_delay_ms(360);
}
return rslt;
}
```
### Enabling and mapping line interrupt to that of BMA456 sensor interrupt
This example shows mapping of a line interrupt with two feature interrupts simultaneously in
variant BMA456.
Note: There are two interrupts - feature interrupt and hardware interrupts. You can map more
than one interrupt with a single line interrupt. If a feature interrupt is mapped with a line
interrupt, one can map the other line interrupt with that of hardware interrupt. This example
can be done for other variants as well.
``` c
uint16_t main(void)
{
uint16_t result = BMA4_OK;
/* Variable to define two interrupt lines */
uint8_t int_line[2] = {BMA4_INTR1_MAP, BMA4_INTR2_MAP};
/* Variable to define feature interrupts to be mapped*/
uint16_t int_map = (BMA456_STEP_CNTR_INT | BMA456_SINGLE_TAP_INT);
/* Initialize the device instance as per the initialization example */
/* Configure the accelerometer as per the example */
/* Mapping line interrupt 1 with that of two sensor feature interrupts -
* Step counter and single tap interrupt */
result = bma456_map_interrupt(int_line[0], int_map, BMA4_ENABLE, &dev);
if(result == BMA4_OK) {
printf("Interrupt mapping successful\r\n");
}
else {
printf("Interrupt mapping failed\r\n");
printf("Error code = %d\r\n", result);
}
return result;
}
```
### Reading interrupt status register
This example is in continuation of the previous example: Enabling and mapping line interrupt.
After the interrupts are mapped, interrupt status register is read in an interrupt service
routine to per
BMA456-Sensor-API-master.zip
需积分: 5 192 浏览量
2021-07-30
14:18:57
上传
评论
收藏 132KB ZIP 举报
游走在代码中的鱼
- 粉丝: 4
- 资源: 5
最新资源
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- htmlzwbjq_downyi.com.zip
- 无头单向非循环链表的实现(Test.c)
- 无头单向非循环链表的实现(SList.c)
- 浏览器重定向插件更新文件
- SSA-BP麻雀算法优化BP神经网络多特征分类预测(Matlab实现完整源码和数据)
- 粒子群算法优化BP神经网络PSO-BP的MATLAB代码(数值预测)
- 基于Springboot的一起看书平台.zip
- 无头单向非循环链表的实现(SList.h)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈