SC7A20(士兰微-加速度传感器)示例

本文档记录了在RTL8762DK硬件平台上,使用keil5.29 IDE,实现SC7A20加速度传感器的简单示例。包括了硬件连接、驱动代码编写、I2C通信接口的使用,并分享了运行结果,以助于其他开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

        做个笔录,最近项目使用了此款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, &reg_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, &reg_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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ch_champion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值