2022-01-18 00:58:54 +08:00

135 lines
4.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*********************************************************************************************************************
* COPYRIGHT NOTICE
* Copyright (c) 2020,<2C><><EFBFBD>ɿƼ<C9BF>
* All rights reserved.
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QQȺ<51><C8BA><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA>824575535
*
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD>У<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>;<EFBFBD><CDBE>
* <20><>ӭ<EFBFBD><D3AD>λʹ<CEBB>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EBB1A3><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC>İ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @file MMA8451
* @company <09>ɶ<EFBFBD><C9B6><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD>޹<EFBFBD>˾
* @author <09><><EFBFBD>ɿƼ<C9BF>(QQ3184284598)
* @version <09>鿴doc<6F><63>version<6F>ļ<EFBFBD> <20>汾˵<E6B1BE><CBB5>
* @Software ADS v1.2.2
* @Target core TC264D
* @Taobao https://seekfree.taobao.com/
* @date 2020-3-23
* @note
<09><><EFBFBD>߶<EFBFBD><DFB6>
------------------------------------
SCL <20>鿴SEEKFREE_IIC<49>ļ<EFBFBD><C4BC>ڵ<EFBFBD>SEEKFREE_SCL<43><EFBFBD><EAB6A8>
SDA <20>鿴SEEKFREE_IIC<49>ļ<EFBFBD><C4BC>ڵ<EFBFBD>SEEKFREE_SDA<44><EFBFBD><EAB6A8>
------------------------------------
********************************************************************************************************************/
#include "zf_stm_systick.h"
#include "SEEKFREE_IIC.h"
#include "SEEKFREE_MMA8451.h"
//SA0<41><30><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>
int16 acc_x = 0, acc_y = 0, acc_z = 0;
//-------------------------------------------------------------------------------------------------------------------
// @brief L3G4200D<30>Լ캯<D4BC><ECBAAF>
// @param NULL
// @return void
// @since v1.0
// Sample usage: <09><><EFBFBD>øú<C3B8><C3BA><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>ģ<EFBFBD><C4A3>IIC<49>ij<EFBFBD>ʼ<EFBFBD><CABC>
//-------------------------------------------------------------------------------------------------------------------
void mma8451_self_check(void)
{
uint16 v;
do
{
v= simiic_read_reg(MMA8451_DEV_ADD, WHO_AM_I_REG, SIMIIC);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
//1 ģ<><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8>ʼ<EFBFBD><CABC><EFBFBD>
//2 <20><><EFBFBD>ߴ<EFBFBD><DFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>нӺ<D0BD>
//3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3.3V
//4 <20><><EFBFBD><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD>ģ<EFBFBD><C4A3>IIC<49>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}while((v != MMA8451Q_ID)&&(v != MMA8452Q_ID)&&(v != MMA8453Q_ID));
}
//-------------------------------------------------------------------------------------------------------------------
// @brief <20><>ʼ<EFBFBD><CABC>MMA8451<35><31><EFBFBD>ٶȼ<D9B6>
// @param NULL
// @return uint8 <09><><EFBFBD><EFBFBD>1<EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><30><CAA7>
// @since v1.0
// Sample usage: <09><><EFBFBD>øú<C3B8><C3BA><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>ģ<EFBFBD><C4A3>IIC<49>ij<EFBFBD>ʼ<EFBFBD><CABC>
//-------------------------------------------------------------------------------------------------------------------
uint8 mma845x_init(void)
{
simiic_init();
systick_delay_ms(STM0, 50);
mma8451_self_check();
simiic_write_reg(MMA8451_DEV_ADD, CTRL_REG1,ASLP_RATE_20MS+DATA_RATE_2500US);
simiic_write_reg(MMA8451_DEV_ADD, XYZ_DATA_CFG_REG, FULL_SCALE_2G); //2G
simiic_write_reg(MMA8451_DEV_ADD, CTRL_REG1, (ACTIVE_MASK+ASLP_RATE_20MS+DATA_RATE_5MS)&(~FREAD_MASK)); //<2F><><EFBFBD><EFBFBD>״̬ 14bit
return 0;
}
//-------------------------------------------------------------------------------------------------------------------
// @brief <20><>ȡMMA8451<35><31><EFBFBD>ٶȼ<D9B6><C8BC><EFBFBD><EFBFBD><EFBFBD>
// @param NULL
// @return void
// @since v1.0
// Sample usage: ִ<>иú<D0B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӳ鿴<D3B2><E9BFB4>Ӧ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//-------------------------------------------------------------------------------------------------------------------
void get_mma8451(void)
{
uint8 dat[6];
uint16 wx,wy,wz;
simiic_read_regs(MMA8451_DEV_ADD, OUT_X_MSB_REG, dat, 6, SIMIIC);
wx = (uint16)dat[0]<<8 | dat[1];
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>ԭʼ<D4AD><CABC>Ӧ<EFBFBD><D3A6>ֵ
if(dat[0]>0x7f) acc_x=-(int16)((~(wx>>2) + 1)&0X3FFF); //<2F><>λȡ<CEBB><C8A1><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD>Ч<EFBFBD>ַ<EFBFBD>
else acc_x=(wx>>2)&0X3FFF; //<2F><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>Ч<EFBFBD>ַ<EFBFBD>
wy = (uint16)dat[2]<<8 | dat[3];
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>ԭʼ<D4AD><CABC>Ӧ<EFBFBD><D3A6>ֵ
if(dat[2]>0x7f) acc_y=-(int16)((~(wy>>2) + 1)&0X3FFF); //<2F><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