STM32配置IIC接口通信方式参考源码.docx

上传人:b****4 文档编号:4472486 上传时间:2022-12-01 格式:DOCX 页数:7 大小:16.03KB
下载 相关 举报
STM32配置IIC接口通信方式参考源码.docx_第1页
第1页 / 共7页
STM32配置IIC接口通信方式参考源码.docx_第2页
第2页 / 共7页
STM32配置IIC接口通信方式参考源码.docx_第3页
第3页 / 共7页
STM32配置IIC接口通信方式参考源码.docx_第4页
第4页 / 共7页
STM32配置IIC接口通信方式参考源码.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

STM32配置IIC接口通信方式参考源码.docx

《STM32配置IIC接口通信方式参考源码.docx》由会员分享,可在线阅读,更多相关《STM32配置IIC接口通信方式参考源码.docx(7页珍藏版)》请在冰豆网上搜索。

STM32配置IIC接口通信方式参考源码.docx

STM32配置IIC接口通信方式参考源码

STM32配置IIC接口通信方式参考源码

全部分类移动开发与应用WEB前端架构与运维程序设计数据库操作系统热点技术综合STM32配置IIC接口通信方式参考源码90阅读 0评论2016-11-30 zhuimengcanyang

分类:

嵌入式最近在读取SHT3x系列sensor的温度和湿度,用到的是IIC接口。

顺便写了一下STM32的IIC接口。

这次配置的是STM32内部的IIC接口。

注意:

读的时候,怎么发送Ack,和NAck信号,参考stm的设计文档。

点击(此处)折叠或打开#include'Dev_SHT3X.h'

#include'globalDef.h'

#includestdio.h>

#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*/

while(!

I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

/*ClearEV6bysettingagainthePEbit*/

I2C_Cmd(I2C1,ENABLE);

/*Sendthedeviceaddresstowriteto*/

I2C_SendData(I2C1,CMD_READH_SHX);

/*TestonEV8andclearit*/

while(!

I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/*Sendthedeviceaddresstowriteto*/

I2C_SendData(I2C1,CMD_READL_SHX);

/*TestonEV8andclearit*/

while(!

I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));

//------------------------------------------------

/*SendSTRATconditionasecondtime*/

I2C_GenerateSTART(I2C1,ENABLE);

/*TestonEV5andclearit*/

while(!

I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));

/*Sendshxaddressforread*/

I2C_Send7bitAddress(I2C1,((SHT3X_ADDRESS1)|0x01),I2C_Direction_Receiver);

/*TestonEV6andclearit*/

while(!

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

#include'bsp_iic.h'

#include'bsp_SysConfig.h'

externvoidSHT3X_InitDevice(void);

externvoidsht3x_testTask(void);

#endif/*_DEV_SHT3X_H*/上一篇:

STM32模拟IIC通信方式参考源码

下一篇:

linuxgrep命令ITPUB博客|ITPUB论坛|chinaunix论坛

北京皓辰网域网络信息技术有限公司.版权所有

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1