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

📄 sensor_mma7455.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
字号:
/**********************************************************************
* $Id$		Sensor_mma7455.c			2012-03-22
*//**
* @file		Sensor_mma7455.c
* @brief	MMA7455 acceleration sensor driver (I2C data mode)
* @version	1.0
* @date		22. March. 2012
* @author	NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/

#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc177x_8x_libcfg.h"
#else
#include "lpc177x_8x_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _I2C
#include "sensor_mma7455.h"
#include "lpc177x_8x_i2c.h"
#include "lpc177x_8x_pinsel.h"

#ifndef abs
#define abs(x)  (x > 0) ? x:(-x)
#endif

/*********************************************************************//**
 * @brief 		Read/Write data to MMA7455
 * @param[in]	 txdata  point to buffer of data which will be sent.
 * @param[in]   txlen     the length of transmit buffer
 * @param[in]  rxdata point to receive buffer
 * @param[in]  rxlen     the length of receive buffer
 * @return 		None
 **********************************************************************/
MMA7455_Status_t MMA7455_ReadWrite(uint8_t* txdata, uint32_t txlen,
	                                      uint8_t* rxdata, uint32_t rxlen)
{
	I2C_M_SETUP_Type i2cData;
	
	i2cData.sl_addr7bit = MMA7455_ADDR;
	i2cData.tx_length = txlen;
    i2cData.tx_data = txdata;
    i2cData.rx_data = rxdata;
	i2cData.rx_length = rxlen;
	i2cData.retransmissions_max = 3;	
	
	if (I2C_MasterTransferData(I2C_0, &i2cData, I2C_TRANSFER_POLLING) == SUCCESS)
	{		
		return MMA7455_PASS;
	}

	return MMA7455_ERR;
}

/*********************************************************************//**
 * @brief 		MMA7455 init
 * @param[in]	 None
 * @return 	 MMA7455_Status_t
 **********************************************************************/
MMA7455_Status_t MMA7455_Init(void)
{
  unsigned char Data[2];
  MMA7455_Status_t ret;
  MMA7455_Data_t acc_data, offset, old_offset;
  uint8_t g_val;
  uint32_t cnt;

  //Init I2C module as master
  PINSEL_ConfigPin (0, 27, 1);
  PINSEL_ConfigPin (0, 28, 1);
  I2C_Init(I2C_0, MMA7455_SPEED);
  I2C_Cmd(I2C_0,I2C_MASTER_MODE, ENABLE);

  // Set Mode
  Data[0] = MMA7455_MODE_ADDR;
  Data[1] = MMA7455_MODE_SENS_64|MMA7455_MODE_MEASUREMENT;

  ret = MMA7455_ReadWrite(&Data[0], 2, NULL, 0);
  if(ret != MMA7455_PASS)
    return ret;
  
  Data[0] = MMA7455_CTR1_ADDR;
  Data[1] = MMA7455_CTR1_BANDWIDTH_125;
  ret = MMA7455_ReadWrite(&Data[0], 2, NULL, 0);
  if(ret != MMA7455_PASS)
    return ret;

  // Calibrate
  g_val = 63;
      
  for(cnt = 0; cnt < MMA7455_CALIB_N_TIMES; cnt++)
  {
      // Get current values of X, Y, Z
      ret = MMA7455_Get10bitData(&acc_data);
      if(ret != MMA7455_PASS)
        return ret;

       // [0,0,+1g] position
      if(acc_data.AccX == 0 &&
         acc_data.AccY == 0 &&
         acc_data.AccZ == g_val)
         break;

      // Get current offset
      ret = MMA7455_GetOffData(&old_offset);
      if(ret != MMA7455_PASS)
        return ret;

      
      offset.AccX =  acc_data.AccX * (-2);   // shift X. Ofs X has 

⌨️ 快捷键说明

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