今天调了一天,一开始地址写0x1c,i2c出错。之后改成0x38.就没问题了。但是读出的数据有问题,所有的都是ff。一直找不到问题。望各位帮看看。
/* MMA8451.c
*
* Copyright (c) 2012, Texas Instruments Incorporated
* All rights reserved.
* -- COPYRIGHT-- */
// F. Chen
// Texas Instruments Inc.
// Dec. 2012
// Built with CCS Version: 5.2.1 and IAR Embedded Workbench Version: 5.51.1
//******************************************************************************
#include "i2c.h"
#include "gpio.h"
#include "include.h"
#include "math.h"
#define I2C1_WRITE_ADDRESS 0x38
#define I2C1_READ_ADDRESS 0x38
unsigned char Stop = 0;
unsigned char MMA845x_flag = 0;
unsigned char ReadOutData[6]; // Read out from MMA845x
signed int Gx, Gy, Gz; // X, Y, Z
/*******************************************************************************
** I2C Configeration
******************************************************************************/
void I2C_RegWrite(unsigned char Reg, unsigned char data)
{
uint8_t TxData[2] = {Reg,data};
while(HAL_I2C_Master_Transmit(&hi2c1,I2C1_WRITE_ADDRESS,(uint8_t*)TxData,2,1000) != HAL_OK)
{
if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
}
unsigned char I2C_RegRead(uint8_t REG_Address)
{
uint8_t RxBuffer;
while(HAL_I2C_Master_Transmit(&hi2c1,I2C1_WRITE_ADDRESS,®_Address,1,500) != HAL_OK)
{
if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
if(HAL_I2C_Master_Receive(&hi2c1,I2C1_READ_ADDRESS,&RxBuffer,1,500) != HAL_OK)
{
if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
return RxBuffer;
}
void I2C_MultiRegsRead(unsigned char Reg, unsigned char *data)
{
data[0] = I2C_RegRead(0x01);
data[1] = I2C_RegRead(0x02);
data[2] = I2C_RegRead(0x03);
data[3] = I2C_RegRead(0x04);
data[4] = I2C_RegRead(0x05);
data[5] = I2C_RegRead(0x06);
}
/*******************************************************************************
** MMA845x Configeration
******************************************************************************/
#define WHO_AM_I 0x0D
#define DATA_CFG 0x0E
#define HP_FILTER 0x0F
#define CTRL_REG1 0x2A
#define CTRL_REG2 0x2B
#define CTRL_REG3 0x2C
#define CTRL_REG4 0x2D
#define CTRL_REG5 0x2E
void MMA845x_Reset(void)
{
unsigned char RST = 1 << 6;
I2C_RegWrite(CTRL_REG2, RST);
HAL_Delay(500);
//__delay_cycles(10000);
}
void MMA845x_Standby(void)
{
unsigned char ACTIVE = 0x01;
I2C_RegWrite(CTRL_REG1, (I2C_RegRead(CTRL_REG1) & ~ACTIVE));
}
void MMA845x_OutputDataRate(void)
{
unsigned char DR = 6 << 3; // DR[2:0] = 110, 6.25Hz
I2C_RegWrite(CTRL_REG1, (I2C_RegRead(CTRL_REG1) | DR));
}
void MMA845x_OverSamplingMode(void)
{
unsigned char MODS = 0x01; // MODS[1:0] = 01, Low Power
I2C_RegWrite(CTRL_REG2, (I2C_RegRead(CTRL_REG2) | MODS));
}
void MMA845x_FilterCutoff(void)
{
unsigned char HPF_BYP = 1 << 5;
unsigned char LPF_EN = 1 << 4;
I2C_RegWrite(HP_FILTER, (I2C_RegRead(HP_FILTER) | HPF_BYP));
I2C_RegWrite(HP_FILTER, (I2C_RegRead(HP_FILTER) | LPF_EN));
}
void MMA845x_InterruptPolarity(void)
{
unsigned char PP_OD = 0x01; // Open-Drain on interrupt pad
I2C_RegWrite(CTRL_REG2, (I2C_RegRead(CTRL_REG2) | PP_OD));
}
void MMA845x_InterruptEnable(void)
{
unsigned char INT_EN_DRDY = 0x01; // Data Ready interrupt enable
I2C_RegWrite(CTRL_REG4, (I2C_RegRead(CTRL_REG4) | INT_EN_DRDY));
}
void MMA845x_InterruptConfig(void)
{
unsigned char INT_CONFIG_DRDY = 0x01; // Interrupt is routed to INT1 pin
I2C_RegWrite(CTRL_REG5, (I2C_RegRead(CTRL_REG5) | INT_CONFIG_DRDY));
}
void MMA845x_Active(void)
{
unsigned char ACTIVE = 0x01;
I2C_RegWrite(CTRL_REG1, (I2C_RegRead(CTRL_REG1) | ACTIVE));
}
void MMA845x_Init(void)
{
// MX_I2C1_Init();
MMA845x_Reset();
// MMA845x_Standby();
//MMA845x_OutputDataRate();
// MMA845x_OverSamplingMode();
MMA845x_FilterCutoff();
MMA845x_InterruptPolarity();
MMA845x_InterruptConfig();
MMA845x_InterruptEnable();
MMA845x_Active();
// Single_Write(MMA845x_IIC_ADDRESS,CTRL_REG1,ASLP_RATE_20MS+DATA_RATE_5MS);
// Single_Write(MMA845x_IIC_ADDRESS,XYZ_DATA_CFG_REG, FULL_SCALE_2G);
// Single_Write(MMA845x_IIC_ADDRESS,CTRL_REG1, ACTIVE_MASK);
// I2C_RegWrite(CTRL_REG1, 0x10);
// I2C_RegWrite(0x0E, 0x00);
// MMA845x_Active();
// //I2C_RegWrite(CTRL_REG1, 0x10);
}
void MMA845x_ReadXYZ(void)
{
I2C_MultiRegsRead(0x01, ReadOutData);
Gx = (ReadOutData[0] << 8) | ReadOutData[1];
Gy = (ReadOutData[2] << 8) | ReadOutData[3];
Gz = (ReadOutData[4] << 8) | ReadOutData[5];
} |