STM32配置IIC接口通信方式参考源码Word格式文档下载.docx
《STM32配置IIC接口通信方式参考源码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《STM32配置IIC接口通信方式参考源码Word格式文档下载.docx(7页珍藏版)》请在冰豆网上搜索。
#defineI2C1_OWN_ADDRESS70x0A
#defineI2C_Speed40000
#defineSHT3X_ADDRESS0x44
/*readoutcommand*/
#defineCMD_READH_SHX0x2c
#defineCMD_READL_SHX0x06
/**
*SHXdevice:
IIC1
*PB6-SCL
*PB7-SDA
*/
staticvoidIIC_gpioConfig(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
/*使能与I2C1有关的时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
/*PB6-I2C1_SCL、PB7-I2C1_SDA*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_OD;
//开漏输出
GPIO_Init(GPIOB,&
GPIO_InitStructure);
}
staticvoidIIC_modeConfig(void)
I2C_InitTypeDefI2C_InitStructure;
I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
/*I2C配置*/
I2C_InitStructure.I2C_DutyCycle=I2C_DutyCycle_2;
/*高电平数据稳定,低电平数据变化SCL时钟线的占空比*/
I2C_InitStructure.I2C_OwnAddress1=I2C1_OWN_ADDRESS7;
/*settingmasteraddress.*/
I2C_InitStructure.I2C_Ack=I2C_Ack_Enable;
/*enableack*/
I2C_InitStructure.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
/*7-bitaddressingmode*/
I2C_InitStructure.I2C_ClockSpeed=I2C_Speed;
/*communicationspeed=400k*/
I2C_Init(I2C1,&
I2C_InitStructure);
I2C_Cmd(I2C1,ENABLE);
voidSHT3X_InitDevice(void)
IIC_gpioConfig();
IIC_modeConfig();
voidsht3x_readTempHumi(uint8_t*pBuffer,uint8_tNumByteToRead)
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
/*detectIICbusy*/
I2C_GenerateSTART(I2C1,ENABLE);
/*SendSTARTcondition*/
/*TestonEV5andclearit*/
while(!
I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
/*Sendslavedeviceaddressforwrite*/
I2C_Send7bitAddress(I2C1,(SHT3X_ADDRESS1),I2C_Direction_Transmitter);
/*TestonEV6andclearit*/
I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/*ClearEV6bysettingagainthePEbit*/
/*Sendthedeviceaddresstowriteto*/
I2C_SendData(I2C1,CMD_READH_SHX);
/*TestonEV8andclearit*/
I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,CMD_READL_SHX);
//------------------------------------------------
/*SendSTRATconditionasecondtime*/
/*Sendshxaddressforread*/
I2C_Send7bitAddress(I2C1,((SHT3X_ADDRESS1)|0x01),I2C_Direction_Receiver);
I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/*Whilethereisdatatoberead*/
while(NumByteToRead)
if(NumByteToRead==1)
/*DisableAcknowledgement*/
I2C_AcknowledgeConfig(I2C1,DISABLE);
/*SendSTOPCondition*/
I2C_GenerateSTOP(I2C1,ENABLE);
/*TestonEV7andclearit*/
if(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/*ReadabytefromtheEEPROM*/
*pBuffer=I2C_ReceiveData(I2C1);
/*Pointtothenextlocationwherethebytereadwillbesaved*/
pBuffer++;
/*Decrementthereadbytescounter*/
NumByteToRead--;
}
/*EnableAcknowledgementtobereadyforanotherreception*/
I2C_AcknowledgeConfig(I2C1,ENABLE);
//-----------------------------------------------------------------------------
staticfloatSHT3X_CalcTemperature(uint16_trawValue)
//calculatetemperature[°
C]
//T=-45+175*rawValue/(2^16-1)
return175.0f*(float)rawValue/65535.0f-45.0f;
staticfloatSHT3X_CalcHumidity(uint16_trawValue)
//calculaterelativehumidity[%RH]
//RH=rawValue/(2^16-1)*100
return100.0f*(float)rawValue/65535.0f;
voidsht3x_testTask(void)
uint8_trecBuffer[6];
uint16_trawdata;
floatfTemp,fHumi;
sht3x_readTempHumi(&
recBuffer[0],6);
rawdata=recBuffer[0];
rawdata=(rawdata8)+recBuffer[1];
fTemp=SHT3X_CalcTemperature(rawdata);
rawdata=recBuffer[3];
rawdata=(rawdata8)+recBuffer[4];
fHumi=SHT3X_CalcHumidity(rawdata);
enterCriticalSection();
printf('
T:
%0.2f,H:
%0.1f\r\n'
fTemp,fHumi);
exitCriticalSection();
}点击(此处)折叠或打开#ifndef_DEV_SHT3X_H
#define_DEV_SHT3X_H
bsp_iic.h'
bsp_SysConfig.h'
externvoidSHT3X_InitDevice(void);
externvoidsht3x_testTask(void);
#endif/*_DEV_SHT3X_H*/上一篇:
STM32模拟IIC通信方式参考源码
下一篇:
linuxgrep命令ITPUB博客|ITPUB论坛|chinaunix论坛
北京皓辰网域网络信息技术有限公司.版权所有