NRF52832 study notes (34)-use of tilt sensor SCL3300

1. Introduction

Official data sheet: https://pan.baidu.com/s/1Y0M4ImNsBvAvECy-yNUvxg Extraction code: itsr

  • Ultra low noise, high resolution 0.001°/√Hz
  • SPI digital interface
  • Superior mechanical damping characteristics
  • Operating temperature range: -40~125℃
  • Current consumption 1.2mA (power supply: 3.0~3.6V)
  • Mature capacitance 3D-MEMS technology

2. Hardware connection

Function port Pin
MISO 17
DAWDLE 18
CLK 20
CSN 19

Three, add SPI driver

View NRF52832 study notes (5)-SPI interface use

The official document recommends the SPI clock because SCK will affect the noise level of SCL3300. It is recommended that the clock frequency be 2MHz~4MHz for optimal performance.


Modify the SPI frequency to 4MHz,SPI_FREQUENCY_FREQUENCY_M4

Four, transplant files

Link: https://pan.baidu.com/s/1fS7LCpQW36QGIvJBoxP4gQ extraction code: 3opk
adds board_scl3300.c and board_scl3300.h to the Application folder of the project

4.1 board_scl3300.c

/*********************************************************************
 * INCLUDES
 */
#include "nrf_delay.h"

#include "board_scl3300.h"
#include "board_spi.h"

static void writeRegister(uint32_t cmd);
static void readRegister(void);
static void delayMs(uint32_t time);
static void delayUs(uint32_t time);

/*********************************************************************
 * LOCAL VARIABLES
 */									
static uint8_t s_readData[4] = {
    
    0};

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief SCL3300的初始化函数
 @param 无
 @return 无
*/
void SCL3300_Init(void)
{
    
    
    delayMs(10);
    
    writeRegister(CHANGE_TO_MODE4);                         // 选择倾角模式
    readRegister();

    delayMs(5);
    
    writeRegister(READ_STATUS_SUMMARY);                     // 清除状态
    readRegister();
    
    writeRegister(READ_STATUS_SUMMARY);
    readRegister();
    
    writeRegister(READ_STATUS_SUMMARY);
    readRegister();
    
    writeRegister(ENABLE_ANGLE_OUTPUTS);                    // 使能角度输出
    readRegister();
}

/**
 @brief 检查SPI通信,返回值第3字节为0xC1则通信正常
 @param 无
 @return 无
*/
void SCL3300_Test(void)
{
    
    
    writeRegister(READ_ID);
    readRegister();
    // 打印s_readData[2]
}

/**
 @brief 读取角度
 @param direction -[in] 要读取的方向X,Y,Z轴
 @return 角度数
*/
float SCL3300_ReadAngle(uint8_t direction)
{
    
    
    if(direction == ANG_X)
    {
    
    
        writeRegister(READ_ANG_X);
    }
    else if(direction == ANG_Y)
    {
    
    
        writeRegister(READ_ANG_Y);
    }
    else if(direction == ANG_Z)
    {
    
    
        writeRegister(READ_ANG_Z);
    }
    readRegister();
                                                            // SPI返回结构第1字节OP+RS,
    uint16_t angle = (s_readData[1] << 8) + s_readData[2];  // 第2和第3字节为Data
                                                            // 第4字节为CRC
    float fangle = (float)angle / 16384.0 * 90.0;           // 如3976/2^14*90=21.84°
    
    return fangle;
}


/*********************************************************************
 * LOCAL FUNCTIONS
 */
/**
 @brief 写SCL3300寄存器
 @param cmd -[in] 写入命令
 @return 无
*/
static void writeRegister(uint32_t cmd)
{
    
      
    uint8_t writeData[4] = {
    
    0};
    writeData[0] = (cmd >> 24) & 0xFF;
    writeData[1] = (cmd >> 16) & 0xFF;
    writeData[2] = (cmd >> 8) & 0xFF;
    writeData[3] = (cmd >> 0) & 0xFF;
    
    SPI_CS_LOW;
    
    SPI_ReadWriteData(writeData, NULL, 4);
    
    SPI_CS_HIGH;
    
    delayUs(10);
}

/**
 @brief 读SCL3300寄存器
 @param 无
 @return 无
*/
static void readRegister(void)
{
    
       
    SPI_CS_LOW;
    
    SPI_ReadWriteData(NULL, s_readData, 4);

    SPI_CS_HIGH;
    
    delayUs(10);
}

/**
 @brief 毫秒级延时函数
 @param time -[in] 延时时间(毫秒)
 @return 无
*/
static void delayMs(uint32_t time)
{
    
    
    nrf_delay_ms(time);
}

/**
 @brief 微秒级延时函数
 @param time -[in] 延时时间(微秒)
 @return 无
*/
static void delayUs(uint32_t time)
{
    
    
    nrf_delay_us(time);
}

/****************************************************END OF FILE****************************************************/

4.2 board_scl3300.h

#ifndef _BOARD_SCL3300_H_
#define _BOARD_SCL3300_H_

/*********************************************************************
 * INCLUDES
 */

/*********************************************************************
 * DEFINITIONS
 */
#define ANG_X                   0
#define ANG_Y                   1
#define ANG_Z                   2

//******************************************************************/
//                          传感数据                                /
//******************************************************************/
#define READ_ACC_X              0x040000F7
#define READ_ACC_Y              0x080000FD
#define READ_ACC_Z              0x0C0000FB
#define READ_TEMPERATURE        0x140000EF
#define READ_ANG_X              0x240000C7
#define READ_ANG_Y              0x280000CD
#define READ_ANG_Z              0x2C0000CB

//******************************************************************/
//                          自我检测                                /
//******************************************************************/
#define READ_STO                0x100000E9

//******************************************************************/
//                          状态                                    /
//******************************************************************/
#define READ_STATUS_SUMMARY     0x180000E5

//******************************************************************/
//                          错误标志                                /
//******************************************************************/
#define READ_ERR_FLAG1          0x1C0000E3
#define READ_ERR_FLAG2          0x200000C1

//******************************************************************/
//                          命令                                    /
//******************************************************************/
#define READ_CMD                0x340000DF
#define CHANGE_TO_MODE1         0xB400001F  // 默认模式,1.8g full-scale 40 Hz 1st order low pass filter
#define CHANGE_TO_MODE2         0xB4000102  // 23.6g full-scale 70 Hz 1st order low pass filter
#define CHANGE_TO_MODE3         0xB4000225  // Inclination mode 13 Hz 1st order low pass filter
#define CHANGE_TO_MODE4         0xB4000338  // Inclination mode 13 Hz 1st order low pass filter Low noise mode
#define SET_POWER_DOWN_MODE     0xB400046B
#define WAKE_UP                 0xB400001F
#define SW_RESET                0xB4002098

//******************************************************************/
//                          角度控制                                /
//******************************************************************/
#define ENABLE_ANGLE_OUTPUTS    0xB0001F6F

//******************************************************************/
//                          芯片ID                                  /
//******************************************************************/
#define READ_ID                 0x40000091

//******************************************************************/
//                          串行块                                  /
//******************************************************************/
#define READ_SERIAL1            0x640000A7
#define READ_SERIAL2            0x680000AD

//******************************************************************/
//                          选择BANK                                /
//******************************************************************/
#define READ_CURRENT_BANK       0x7C0000B3
#define SWITCH_TO_BANK_0        0xFC000073 // 默认,读bank1后自动返回
#define SWITCH_TO_BANK_1        0xFC00016E

/*********************************************************************
 * API FUNCTIONS
 */
void SCL3300_Init(void);
void SCL3300_Test(void);
float SCL3300_ReadAngle(uint8_t direction);

#endif /* _BOARD_SCL3300_H_ */

Five, initialization process

Wait for 10ms after power-on -> set measurement mode -> wait for 5ms -> read status -> enable angle output.

Six, inclination conversion formula

Seven, API call

Need to include the header file board_scl3300.h

SCL3300_Init

Features SCL3300 initialization function
Function definition void SCL3300_Init(void)
parameter no
return no

SCL3300_Test

Features Check SPI communication, the third byte of the return value is 0xC1, then the communication is normal
Function definition void SCL3300_Test(void)
parameter no
return no

SCL3300_ReadAngle

Features Reading angle
Function definition float SCL3300_ReadAngle(uint8_t direction)
parameter direction: the direction X, Y, Z axis to be read
return Number of angles

8. Examples of use

8.1 Initialization

1) Add header files

#include "board_scl3300.h"
#include "board_spi.h"

2) Add initialization code (in the main function of main.c)

/**@brief Function for application main entry.
 */
int main(void)
{
    
    
	bool erase_bonds;

    /*-------------------------- 外设驱初始化 ---------------------------*/
	// Initialize.
    log_init();																	// 日志驱动初始化																	
    timers_init();																// 定时器驱动初始化(在此加入自定义定时器)
    SPI_Init();                                                                 // SPI驱动初始化
    SCL3300_Init();                                                             // SCL3300三轴倾角驱动初始化
    ···
    ···
/*-------------------------- 开启应用 ---------------------------*/
	// Start execution.
    NRF_LOG_INFO("Template example started."); 
    ···
}

8.2 Reading angle

float angX = SCL3300_ReadAngle(ANG_X);
NRF_LOG_INFO("X:" NRF_LOG_FLOAT_MARKER , NRF_LOG_FLOAT(angX));
float angY = SCL3300_ReadAngle(ANG_Y);
NRF_LOG_INFO("Y:" NRF_LOG_FLOAT_MARKER , NRF_LOG_FLOAT(angY));
float angZ = SCL3300_ReadAngle(ANG_Z);
NRF_LOG_INFO("Z:" NRF_LOG_FLOAT_MARKER "\n", NRF_LOG_FLOAT(angZ));

Nine, measurement direction


Written by Leung on October 26, 2020

• Reference: High-precision three-axis accelerometer SCL3300 drive code

Guess you like

Origin blog.csdn.net/qq_36347513/article/details/109293854