1、接线图
接线说明:VCC GND SDA(PB5)-传感器模块第4PIN SCL(PB4)- 传感器模块第5PIN
2、IIC通讯代码:
#define READ_ADDR 0x33
#define DEVICE_ADDR 0x32
u8 arry_write[10]={0x37,0x01,0x40,0x88,0x01,0x2a,0x01,0x00};
void I2C_GPIO_Config(void)
{
/* 配置I2C管脚的复用功能 */
GPIO_DigitalRemapConfig(AFIOB, GPIO_Pin_5, AFIO_AF_0,ENABLE); //PB5 I2C SDA
GPIO_DigitalRemapConfig(AFIOB, GPIO_Pin_4, AFIO_AF_0,ENABLE); //PB4 I2C SCL
}
void I2C_Driver(void)
{
I2C_InitTypeDef I2C_InitStruct;
I2C_InitStruct.I2C_Acknowledge = I2C_Acknowledge_Disable;
I2C_InitStruct.I2C_Broadcast = I2C_Broadcast_Disable;
I2C_InitStruct.I2C_OwnAddress = 0x00;
I2C_InitStruct.I2C_Prescaler = 640-1;
I2C_Init(I2C,&I2C_InitStruct);
}
/**
* @brief IIC写函数
* @param pBuffer:需要写入的数据
* @param WriteAddr:从机地址
* @param NumByteToWrite:需要写入的数据长度
* @retval 无
*/
void I2C_EE_Write(u8 pBuffer, unsigned int WriteAddr, u16 data_size)
{
// int i;
/******************等待从机ready***************/
I2C_GenerateEvent(I2C,I2C_Event_Start,DISABLE);
I2C->CCR |= I2C_CCR_SI | I2C_CCR_ACK;
I2C_Cmd(I2C,DISABLE);
I2C_Cmd(I2C,ENABLE);
I2C_GenerateEvent(I2C,I2C_Event_Start,ENABLE);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_StartOk)!= SET);
I2C_SendAddr(I2C, DEVICE_ADDR);//器件地址,写
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MASGetAckW)!=SET);
I2C_SendData(I2C,WriteAddr);//发送要写的字地址
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDSGetAck)!=SET);
// for(i=0;i<data_size;i++)
// {
// I2C_SendData(I2C, *(pBuffer++));
// while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDSGetAck) != SET);
// }
I2C_SendData(I2C, pBuffer);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDSGetAck) != SET);
/******************发送停止位***************/
I2C_GenerateEvent(I2C,I2C_Event_Stop,ENABLE);
}
//uint8_t I2C_EE_Read(u8* pBuffer,u16 ReadAddr, u16 DeviceAddr, u16 data_size)
uint8_t I2C_EE_Read(u16 ReadAddr)
{
// int i;
uint8_t date = 0 ;
/******************等待从机ready***************/
I2C_GenerateEvent(I2C,I2C_Event_Start,ENABLE);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_StartOk)!= SET);
I2C_SendAddr(I2C, DEVICE_ADDR);//器件地址,写
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MASGetAckW)!=SET);
I2C_SendData(I2C,ReadAddr);//发送要读的页地址
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDSGetAck)!=SET);
//printf("-----%s %d----\n", __func__, __LINE__) ;
I2C_GenerateEvent(I2C,I2C_Event_Stop,ENABLE);
/******************接收数据***************/
I2C_GenerateEvent(I2C,I2C_Event_Start,ENABLE);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_StartOk)!= SET);
I2C_SendAddr(I2C, READ_ADDR);//器件地址,读
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MASGetAckR)!=SET);
I2C->CCR=I2C_CCR_ACK|I2C_CCR_SI;//主机发送NACK
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDGSendNack) != SET);
date = I2C_ReceiveData(I2C);
/******************发送停止位***************/
I2C_GenerateEvent(I2C,I2C_Event_Stop,ENABLE);
return date ;
}
init(void)
{
I2C_EE_Write(arry_write[0], 0x20, 1);Software_Delay(100000) ;
I2C_EE_Write(arry_write[1], 0x21, 1);Software_Delay(100000) ;
I2C_EE_Write(arry_write[2], 0x22, 1);Software_Delay(100000) ;
I2C_EE_Write(arry_write[3], 0x23, 1);Software_Delay(100000) ;
I2C_EE_Write(arry_write[4], 0x1f, 3);Software_Delay(100000) ;
I2C_EE_Write(arry_write[5], 0x30, 1);Software_Delay(100000) ;
I2C_EE_Write(arry_write[6], 0x32, 1);Software_Delay(100000) ;
I2C_EE_Write(arry_write[7], 0x32, 1);Software_Delay(100000) ;
}
3、模块图片分享: