I'm working on my final school project and I'm using the MPL3115A2. The sensor communicate via I2C, i wrote a code but it didn't work. Can you help me by telling me what's wrong with it?
here is the code.
Thank you!
Code:#include "stm32f4xx.h" GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; void I2C_Configuration(void) { // enable APB1 peripheral clock for I2C1 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // enable clock for SCL and SDA pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* setup SCL and SDA pins * You can connect I2C1 to two different * pairs of pins: * 1. SCL on PB6 and SDA on PB7 * 2. SCL on PB8 and SDA on PB9 */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // we are going to use PB6 and PB7 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // set pins to alternate function GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // set GPIO speed GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB // Connect I2C1 pins to AF GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1); // SCL GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); // SDA // configure I2C1 I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on) I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses I2C_Init(I2C1, &I2C_InitStruct); // init I2C1 // enable I2C1 I2C_Cmd(I2C1, ENABLE); } void Write_IIC1(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t REG_data) { while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,SlaveAddress,I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,REG_Address); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_SendData(I2C1,REG_data); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); } uint8_t Read_IIC1(uint8_t SlaveAddress, uint8_t REG_Address) { uint8_t REG_data; while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); I2C_AcknowledgeConfig(I2C1, ENABLE); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,SlaveAddress,I2C_Direction_Transmitter); while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,REG_Address); while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTART(I2C1,ENABLE); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,SlaveAddress,I2C_Direction_Receiver); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); REG_data = I2C_ReceiveData (I2C1); return REG_data; } int main(void) { int i; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; GPIO_Init(GPIOD, &GPIO_InitStruct); I2C_Configuration(); while (1) { Write_IIC1(0x60,0x26,Read_IIC1(0x60, 0x0C)&0x1E);//stand by Write_IIC1(0x60,0x26,Read_IIC1(0x60, 0x0C)|0xC8);//activate altimeter mode ans flags Write_IIC1(0x60,0x26,Read_IIC1(0x60, 0x0C)|0x01);//wake sensor //Write_IIC1(0x60,0x13,0x07); if(Read_IIC1(0x60, 0x0C)) GPIO_SetBits(GPIOD,GPIO_Pin_13); } }
-----