Hi,
My project involves an nRF52840 DK connected to an accelerometer on a breadboard, connecting to my Mac via USB-micro.
I'm editing the twi_scanner SES project so that my accelerometer can communicate with the DK via the TWI protocol. I've been reading the MC6XX development guide and have successfully linked the accelerometer header and c files to my main project in SES. I've included a URL of the depository including the relevant header and c files, as well as the development guide that I used to configure the accelerometer: https://github.com/mcubemems/mCube_mc36xx_mcu_driver/commit/dac7f2563d96c4cf4832043f591efcd588406fb3#diff-e8ae2a21c5c57fe1152156108c92aad842f38a4e18b01ebb6ad72aa4760f65d9
I'm stuck on how to initialise the accelerometer to be able to read the X, Y and Z values for it. When I build, connect J-Link, download the project and launch the program using Putty, the code outputs "Read Fail" which I implemented in the code below, instead of the X, Y and Z values.
I'm a beginner to developing with Nordic devices and configuring the accelerometer and I need this solved very urgently so if anyone can help me I'd be extremely grateful! I think the guide contains everything I need to know but it's not explained in a very user-friendly way. Page 14 states how to initialise the accelerometer but I'm not sure what else I'll need to do to read the X, Y and Z values? Thanks.
I've included my project's main c file below:
/**
* Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
/**
*/
#include
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "m_drv_mc36xx.h"
#include "m_drv_interface.h"
#include "m_drv_console.h"
#include "m_drv_mc_utility.h"
/* TWI instance ID. */
#define TWI_INSTANCE_ID 0 // create a ID constant
#define M_DRV_MC36XX_AXES_NUM 3
#define M_DRV_MC36XX_FIFO_DEPTH 32
// create a handle which will point to TWI instance, in this case its TWI_0
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
// a function to initialize the twi(i2c)
void twi_init(void)
{
ret_code_t err_code; // a variable to hold error code
// Create a struct with configurations and pass the values to these configurations.
const nrf_drv_twi_config_t twi_config = {
.scl = 26, // scl connected to pin 22, you can change it to any other pin
.sda = 27, // sda connected to pin 23, you can change it to any other pin
.frequency = NRF_DRV_TWI_FREQ_100K, // set the communication speed to 100K, we can select 250k or 400k as well
.interrupt_priority = APP_IRQ_PRIORITY_HIGH, // Interrupt priority is set to high, keep in mind to change it if you are using a soft-device
.clear_bus_init = false // automatic bus clearing
};
err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL); // initialize the twi
APP_ERROR_CHECK(err_code); // check if any error occured during initialization
nrf_drv_twi_enable(&m_twi); // enable the twi comm so that its ready to communicate with the sensor
}
/**
* @brief Function for main application entry.
*/
int main(void)
{
ret_code_t err_code; // a variable to hold error code value
uint8_t address = 0x4C; // address of the sensor
uint8_t sample_data = 0x00; // sample data initialized with 0 value.
// initialize the Logger so that we can print msgs on the logger
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("Application Started");
NRF_LOG_FLUSH(); // flushing is necessary if deferred is set to 1(check this video tutorial to know it better)
twi_init(); // call the twi initialization function
// read some data from the sensor
err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
if(err_code == NRF_SUCCESS) // if reading data was successful
{
NRF_LOG_INFO("Successfully detected a device at address: 0x%x", address); // let the users know its working
}
// flushing is important, if you set the deffered to 1, if deffered is set to 0 then we don't need to flush the log buffer.
while (true)
{
//M_DRV_MC36XX_Init ( );
//M_DRV_MC36XX_ConfigRegRngResCtrl( );
//M_DRV_MC36XX_SetSampleRate( );
M_DRV_MC36XX_SetMode (E_M_DRV_MC36XX_MODE_CWAKE);
float _faOutput[1][M_DRV_MC36XX_AXES_NUM];
int M_DRV_MC36XX_ReadData(float _faOutput[M_DRV_MC36XX_FIFO_DEPTH][M_DRV_MC36XX_AXES_NUM],
int nNumOfSample);
M_DRV_MC36XX_ReadData (&_faOutput[0][0], 1);
if (1==M_DRV_MC36XX_ReadData(_faOutput,1))
{
NRF_LOG_INFO("%f %f %f ",
_faOutput[0][M_DRV_MC36XX_AXIS_X],
_faOutput[0][M_DRV_MC36XX_AXIS_Y],
_faOutput[0][M_DRV_MC36XX_AXIS_Z]);
NRF_LOG_FLUSH();
}
else{
NRF_LOG_INFO("Read Fail");
NRF_LOG_FLUSH();
}
}
}
/** @} */