ImageVerifierCode 换一换
格式:DOCX , 页数:7 ,大小:16.03KB ,
资源ID:4472486      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4472486.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(STM32配置IIC接口通信方式参考源码.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

1、STM32配置IIC接口通信方式参考源码STM32配置IIC接口通信方式参考源码 全部分类 移动开发与应用 WEB前端 架构与运维 程序设计 数据库 操作系统 热点技术 综合 STM32配置IIC接口通信方式参考源码 90阅读0评论2016-11-30zhuimengcanyang 分类:嵌入式 最近在读取SHT3x系列sensor的温度和湿度,用到的是IIC接口。顺便写了一下STM32的IIC接口。这次配置的是STM32内部的IIC接口。注意:读的时候,怎么发送Ack, 和 NAck信号,参考stm的设计文档。 点击(此处)折叠或打开 #include Dev_SHT3X.h #includ

2、e globalDef.h #include stdio.h #define I2C1_OWN_ADDRESS7 0x0A #define I2C_Speed 40000 #define SHT3X_ADDRESS 0x44 /* read out command */ #define CMD_READH_SHX 0x2c #define CMD_READL_SHX 0x06 /* * SHX device: IIC1 * PB6 - SCL * PB7 - SDA */ static void IIC_gpioConfig(void) GPIO_InitTypeDef GPIO_InitSt

3、ructure; /* 使能与 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

4、= GPIO_Mode_AF_OD; / 开漏输出 GPIO_Init(GPIOB, &GPIO_InitStructure); static void IIC_modeConfig(void) I2C_InitTypeDef I2C_InitStructure; I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; /* I2C 配置 */ I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; /* 高电平数据稳定,低电平数据变化 SCL 时钟线的占空比 */ I2C_InitStructure.I2C_OwnA

5、ddress1 = I2C1_OWN_ADDRESS7; /* setting master address. */ I2C_InitStructure.I2C_Ack = I2C_Ack_Enable ; /* enable ack */ I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; /* 7-bit addressing mode */ I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; /* communication speed = 400k *

6、/ I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); void SHT3X_InitDevice(void) IIC_gpioConfig(); IIC_modeConfig(); void sht3x_readTempHumi(uint8_t* pBuffer, uint8_t NumByteToRead) while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY); /* detect IIC busy */ I2C_GenerateSTART(I2C1, ENABLE); /* Send S

7、TART condition */ /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT); /* Send slave device address for write */ I2C_Send7bitAddress(I2C1, (SHT3X_ADDRESS 1), I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_T

8、RANSMITTER_MODE_SELECTED); /* Clear EV6 by setting again the PE bit */ I2C_Cmd(I2C1, ENABLE); /* Send the device address to write to */ I2C_SendData(I2C1, CMD_READH_SHX); /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED); /* Send the device address to writ

9、e to */ I2C_SendData(I2C1, CMD_READL_SHX); /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED); /- /* Send STRAT condition a second time */ I2C_GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELE

10、CT); /* Send shx address for read */ I2C_Send7bitAddress(I2C1, (SHT3X_ADDRESS 1) | 0x01), I2C_Direction_Receiver); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED); /* While there is data to be read */ while(NumByteToRead) if(NumByteToRead = 1) /* D

11、isable Acknowledgement */ I2C_AcknowledgeConfig(I2C1, DISABLE); /* Send STOP Condition */ I2C_GenerateSTOP(I2C1, ENABLE); /* Test on EV7 and clear it */ if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) /* Read a byte from the EEPROM */ *pBuffer = I2C_ReceiveData(I2C1); /* Point to the next lo

12、cation where the byte read will be saved */ pBuffer+; /* Decrement the read bytes counter */ NumByteToRead-; /* Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig(I2C1, ENABLE); /- static float SHT3X_CalcTemperature(uint16_t rawValue) / calculate temperature C / T = -4

13、5 + 175 * rawValue / (216-1) return 175.0f * (float)rawValue / 65535.0f - 45.0f; /- static float SHT3X_CalcHumidity(uint16_t rawValue) / calculate relative humidity %RH / RH = rawValue / (216-1) * 100 return 100.0f * (float)rawValue / 65535.0f; void sht3x_testTask(void) uint8_t recBuffer6; uint16_t

14、rawdata; float fTemp, fHumi; sht3x_readTempHumi(&recBuffer0, 6); rawdata = recBuffer0; rawdata = (rawdata 8) + recBuffer1; fTemp = SHT3X_CalcTemperature(rawdata); rawdata = recBuffer3; rawdata = (rawdata 8) + recBuffer4; fHumi = SHT3X_CalcHumidity(rawdata); enterCriticalSection(); printf(T: %0.2f, H

15、: %0.1frn, fTemp, fHumi); exitCriticalSection(); 点击(此处)折叠或打开 #ifndef _DEV_SHT3X_H #define _DEV_SHT3X_H #include bsp_iic.h #include bsp_SysConfig.h extern void SHT3X_InitDevice(void); extern void sht3x_testTask(void); #endif /* _DEV_SHT3X_H */ 上一篇:STM32模拟IIC通信方式参考源码 下一篇:linux grep命令ITPUB博客 | ITPUB论坛 | chinaunix论坛 北京皓辰网域网络信息技术有限公司. 版权所有

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

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