概述
做个笔录,最近项目使用了此款gsensor,实现了简单示例
一、环境:
硬件平台(RTL8762DK)
IDE:keil5.29
1、原理图
二、代码:
1、sc7a20.h
/**
*********************************************************************************************************
* Copyright(c) 2018, Realtek Semiconductor Corporation. All rights reserved.
*********************************************************************************************************
* @file io_adc.h
* @brief
* @details
* @author yuan
* @date 2018-12-07
* @version v1.0
*********************************************************************************************************
*/
#ifndef __SC7A20_H
#define __SC7A20_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "rtl876x_i2c.h"
#include "board.h"
#include "gsensor.h"
/* Defines ------------------------------------------------------------------*/
/***Before using the driver, configure it according to the actual cable connection******/
/**The SDO pin of SC7A20 is grounded to 0****************/
/**The SDO pin of SC7A20 is connected to power supply: 1****************/
#define SL_SC7A20_SDO_VDD_GND 0
/*****************************************/
/***Before using the driver, configure the driver based on the actual IIC***/
/**The IIC interface address type of the SC7A20 is 7bits: 0****/
/**The IIC interface address type of the SC7A20 is 8bits: 1****/
#define SL_SC7A20_IIC_7BITS_8BITS 0
/*****************************************/
#if SL_SC7A20_SDO_VDD_GND==0
#define SL_SC7A20_IIC_7BITS_ADDR 0x18
#define SL_SC7A20_IIC_8BITS_WRITE_ADDR 0x30
#define SL_SC7A20_IIC_8BITS_READ_ADDR 0x31
#else
#define SL_SC7A20_IIC_7BITS_ADDR 0x19
#define SL_SC7A20_IIC_8BITS_WRITE_ADDR 0x32
#define SL_SC7A20_IIC_8BITS_READ_ADDR 0x33
#endif
#if SL_SC7A20_IIC_7BITS_8BITS==0
#define SL_SC7A20_IIC_ADDRESS SL_SC7A20_IIC_7BITS_ADDR
#else
#define SL_SC7A20_IIC_WRITE_ADDRESS SL_SC7A20_IIC_8BITS_WRITE_ADDR
#define SL_SC7A20_IIC_READ_ADDRESS SL_SC7A20_IIC_8BITS_READ_ADDR
#endif
//Reg Table
#define SC7A20_CHIP_ID_ADDR 0x0F
#define SC7A20_CHIP_ID 0x11
#define SC7A20_ADDRESS SL_SC7A20_IIC_ADDRESS
#define SC7A20_CTRL_REG1 0x20
#define SC7A20_CTRL_REG2 0x21
#define SC7A20_CTRL_REG3 0x22
#define SC7A20_CTRL_REG4 0x23
#define SC7A20_CTRL_REG5 0x24
#define SC7A20_CTRL_REG6 0x25
#define SC7A20_ADDR_STATUS_REG 0x27
#define SC7A20_REG_X_OUT_LOW 0x28
#define SC7A20_REG_X_OUT_HIGH 0x29
#define SC7A20_REG_Y_OUT_LOW 0x2A
#define SC7A20_REG_Y_OUT_HIGH 0x2B
#define SC7A20_REG_Z_OUT_LOW 0x2C
#define SC7A20_REG_Z_OUT_HIGH 0x2D
#define SC7A20_INT1_CONF_REG 0x30
#define SC7A20_INT1_STATUS_REG 0x31
#define SC7A20_INT1_THR_REG 0x32
#define SC7A20_INT1_TIME_REG 0x33
#define SC7A20_INT2_CONF_REG 0x34
#define SC7A20_INT2_STATUS_REG 0x35
#define SC7A20_INT2_THR_REG 0x36
#define SC7A20_INT2_TIME_REG 0x37
#define SC7A20_CLICK_CFG_REG 0x38
#define SC7A20_CLICK_SRC_REG 0x39
#define SC7A20_CLICK_THS_REG 0x3A
#define SC7A20_TIME_LIMIT_REG 0x3B
#define SC7A20_TIME_LATENCY_REG 0x3C
#define SC7A20_TIME_WINDOW_REG 0x3D
uint8_t sc7a20_write(uint8_t regAddr, uint8_t data);
uint8_t sc7a20_read(uint8_t regAddr, uint8_t *buf);
void sc7a20_gsensor_init(void);
uint8_t sc7a20_id_get(void);
void sc7a20_outdata_get(void);
int16_t drv_12bitComplement1(uint8_t msb, uint8_t lsb);
int16_t drv_12bitComplement2(uint8_t msb, uint8_t lsb);
#ifdef __cplusplus
}
#endif
#endif
2、sc7a20.c
/**
*********************************************************************************************************
* Copyright(c) 2018, Realtek Semiconductor Corporation. All rights reserved.
*********************************************************************************************************
* @file io_adc.c
* @brief This file provides demo code of adc continuous mode.
* @details
* @author yuan
* @date 2018-12-07
* @version v1.0
*********************************************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "sc7a20.h"
/* Globals ------------------------------------------------------------------*/
static uint8_t sc7a20_write_reg(uint8_t regAddr, uint8_t data)
{
return i2c_write(regAddr, data);
}
static uint8_t sc7a20_read_reg(uint8_t regAddr, uint8_t *buf)
{
return i2c_read(regAddr, buf, 1);
}
static uint8_t sc7a20_multiRead_reg(uint8_t regAddr, uint8_t *buf, uint8_t len)
{
return i2c_read(regAddr, buf, len);
}
uint8_t sc7a20_id_get(void)
{
uint8_t sc7a20_chip_id = 0;
uint8_t reg_addr = SC7A20_CHIP_ID_ADDR;
//I2C_RepeatRead(I2C0, ®_addr, 1, &sc7a20_chip_id, 1);
sc7a20_read_reg(reg_addr, &sc7a20_chip_id);
return sc7a20_chip_id;
}
void sc7a20_outdata_get(void)
{
uint8_t reg_addr = SC7A20_REG_X_OUT_LOW;
//I2C_RepeatRead(I2C0, ®_addr, 1, GSensor_Data.OutData, 6);
sc7a20_multiRead_reg(reg_addr|0x80, GSensor_Data.OutData, 6);
}
int16_t drv_12bitComplement1(uint8_t msb, uint8_t lsb)
{
// 量程8g 4mg/digit
return (((int16_t)(msb << 8 | lsb)) >> 4) << 2;
}
int16_t drv_12bitComplement2(uint8_t msb, uint8_t lsb)
{
uint16_t temp = 0;
int16_t accd = 0;
temp &= 0x0000;
temp |= msb;
temp <<= 8;
temp &= 0xff00;
temp |= lsb;
// 负
if (temp & 0x8000)
{
temp >>= 4;
temp |= 0xf000;
}
else
{
temp >>= 4;
temp &= 0x0fff;
}
accd = temp * 1;
return accd << 2;
}
void drv_sc7a20tr_read(int16_t *accel_X, int16_t *accel_Y, int16_t *accel_Z