今天调了一天,一开始地址写0x1c,i2c出错。之后改成0x38.就没问题了。但是读出的数据有问题,所有的都是ff。一直找不到问题。望各位帮看看。 /* MMA8451.c * * //****************************************************************************** #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]; } |
STM32F107+LWIP---如何检查tcp通讯断开?并重新连接
有没有用过数字式mems麦克风的,想问下SPI通讯的问题
stm32f103 CAN通讯多帧发送问题?
STM32F107作为USB主机模拟串口通讯不成功(采用PL2303芯片)
STM32F103C8 LIN通讯例程
求助,丐版J-Link通讯不上
ethernet可以与ethercat通讯吗?
LIS3DH SPI 通讯问题
STM32F373 CAN通讯问题CAN_FLAG_LEC
请教高手看看,STM32F1的多机通讯,地址匹配唤醒问题
评分
查看全部评分
老兄,我读id了。好像也不正常。但是写函数正常啊。真是不懂哪出的问题。我要是把mma8451的地址改成非0x38,写的这个指令都过不去,地址用0x38就能通过i2c写指令,但读出的数据就一直不对。真是弄不清啊 。老兄,帮看看代码又没问题吧。
读出的设备id也不对也是ff。证明没有通讯么?但很奇怪,我只有把地址id设置038,程序才能过i2cwrite函数,这第一步是不是证明已经mma8451有应答了呢 ?
HAL_I2C_Master_Transmit 是发送函数,HAL_I2C_Master_Receive是接收函数
I2C_RegWrite 是写寄存器,只要调用HAL_I2C_Master_Transmit就可以了
I2C_RegRead 是读寄存器,一般先调用HAL_I2C_Master_Transmit 写要读的寄存器的地址,然后调用HAL_I2C_Master_Receive函数来读
如果id的寄存器地址是0,调用 ret= I2C_RegRead(0) 就可以了,ret即是id的值,这个值确保正确,才能证明i2c通信正常 请参考
评分
查看全部评分
HAL_I2C_Mem_Read(&hi2c1, I2C1_READ_ADDRESS, 0x02, 1, &data[1], 1, 35);
HAL_I2C_Mem_Read(&hi2c1, I2C1_READ_ADDRESS, 0x03, 1, &data[2], 1, 35);
HAL_I2C_Mem_Read(&hi2c1, I2C1_READ_ADDRESS, 0x04, 1, &data[3], 1, 35);
HAL_I2C_Mem_Read(&hi2c1, I2C1_READ_ADDRESS, 0x05, 1, &data[4], 1, 35);
HAL_I2C_Mem_Read(&hi2c1, I2C1_READ_ADDRESS, 0x06, 1, &data[5], 1, 35);
//HAL_I2C_Mem_Read(&hi2c1, I2C1_READ_ADDRESS, 0x01, 0x06, data, 6, 3500);
这个函数。用那个hal-i2ctransmit 和个hal-i2crecive函数怎么也不行.还有就是我用HAL_I2C_Mem_Read这个函数一起读6个寄存器也读不出(见最后一行)。真是搞不明白。
我用其它的函数读出来了。见楼下。我还得仔细琢磨琢磨之前的函数为什么不成。谢谢老兄。
我觉得还是用本身的i2c更好,出问题基本原因都是都mcu本身的理解不够到位造成的。