⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xyz_read_and_filter.c

📁 mtk 3D动力传感器驱动源码
💻 C
字号:
/*********************************************************************
 
  (c) copyright Freescale Semiconductor Ltd. 2008
  ALL RIGHTS RESERVED
 
********************************************************************
 Ver 2.0.1	Released Sep.2008
********************************************************************/


#include "MMA7455L.h" /* include register declarations */
#include "FSL_data.h" /* include type definations */

//#define RawDataLength 16

VUINT8 RawDataPointer2;
VINT8 Xraw2[RawDataLength], Yraw2[RawDataLength], Zraw2[RawDataLength];
VINT8 Xavg8_2, Yavg8_2, Zavg8_2;

VUINT8 XYZ_Read_and_Filter(void);
VUINT8 Data_Is_Ready(void);
void MMA7455L_ReadXYZ10(VINT16 *X, VINT16 *Y, VINT16 *Z);

extern VINT8 mma745x_IICRead(VUINT8 Reg_Addr);  /* read a byte from a register on MMA7455L */

VUINT8 XYZ_Read_and_Filter(void)
{
    VUINT8 i;
    VINT16 temp16;
    
    if(!Data_Is_Ready()) return 0;
    
    if((++RawDataPointer2)>=RawDataLength) RawDataPointer2 = 0;
    Xraw2[RawDataPointer2] = mma745x_IICRead(XOUT8);
    Yraw2[RawDataPointer2] = mma745x_IICRead(YOUT8);
    Zraw2[RawDataPointer2] = mma745x_IICRead(ZOUT8);
    
    for(i=0, temp16=0;i<RawDataLength;i++)
    {
        temp16 += Xraw2[i];
    }
    Xavg8_2 = (VINT8)(temp16/RawDataLength);
    for(i=0, temp16=0;i<RawDataLength;i++)
    {
        temp16 += Yraw2[i];
    }
    Yavg8_2 = (VINT8)(temp16/RawDataLength);
    for(i=0, temp16=0;i<RawDataLength;i++)
    {
        temp16 += Zraw2[i];
    }
    Zavg8_2 = (VINT8)(temp16/RawDataLength);
    
    return 1;
}

void MMA7455L_ReadXYZ10(VINT16 *X, VINT16 *Y, VINT16 *Z) 
{ //Read 6 bytes of X, Y and Z 10-bit data and convert to 16-bit signed data
    VINT16 temp;
    
    *X = (VINT16)mma745x_IICRead(XOUTL);              //Read one byte (lower 8 bits of 10-bit data) and store in a 16-bit address
    temp = (VINT16)mma745x_IICRead(XOUTH);            //Read the next byte (higher 2 bits of 10-bit data)
    *X += (temp<<8);                                  //Combine them to a 10-bit data
    if (*X&0x0200) *X |= 0xfc00;	                    //Change the 10-bit data into a 16-bit signed data
    
    *Y = (VINT16)mma745x_IICRead(YOUTL);              //Read one byte (lower 8 bits of 10-bit data) and store in a 16-bit address
    temp = (VINT16)mma745x_IICRead(YOUTH);            //Read the next byte (higher 2 bits of 10-bit data)
    *Y += (temp<<8);                                  //Combine them to a 10-bit data
    if (*Y&0x0200) *Y |= 0xfc00;                      //Change the 10-bit data into a 16-bit signed data
    
    *Z = (VINT16)mma745x_IICRead(ZOUTL);              //Read one byte (lower 8 bits of 10-bit data) and store in a 16-bit address
    temp = (VINT16)mma745x_IICRead(ZOUTH);            //Read the next byte (higher 2 bits of 10-bit data)
    *Z += (temp<<8);                                  //Combine them to a 10-bit data
    if (*Z&0x0200) *Z |= 0xfc00;                      //Change the 10-bit data into a 16-bit signed data
}


VUINT8 Data_Is_Ready(void)
{
    if(mma745x_IICRead(STATUS)&0x01) return 1;
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -