第一范文网 - 专业文章范例文档资料分享平台

高度提炼的模拟I2C程序

来源:用户分享 时间:2025/5/15 2:44:08 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

高度提炼的模拟I2C程序

以下是模拟I2C高度提炼的程序,只要提供GPIO端口、SCL和SDA引脚号、从设备addr,就可以开始使用,基于STM32,对于其他CPU,稍作修改即可。

优点:如果系统中用到了多个模拟I2C通信实例,,就不用每一个模拟I2C实例都编写一套几乎一摸一样的代码,,以下代码对于所有实例都通用。

就像我的系统,很多地方用到了模拟I2C,至少3-4个,有EEPROM,motion运动传感器、ADC等等,,都在不同的引脚上面,但是只用这一套代码。 对于单个I2C,下面也是实用的。

当然:以下代码是根据I2C协议标准编写的,,所有的延时函数都是根据标准所定,,如果不符合您的系统,,可以将delay_us延时加大。

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

头文件

----------------------------------------------------------------------------------------- /*

*********************************************************************************************************

* \\'-.__.-'/ | Company : o--Shen Zhen xxxxx Technology Co,.Ltd--o * / (o)(o) \\ | Website : o--http://www.xxxxx .com.cn--o * \\ \\/ / | Copyright : * /'------'\\ | Product :

* /, .... , \\ | File : bsp_SimulateI2C.h

* /// .::::. \\\\\\ | Descript : use GPIOs to Simulate I2C communication * ///\\ :::::: /\\\\\\ | Version : V0.10

* '' ).''''.( `` | Author : nicholasldf

*=(((====)))= | EditTime : 2015-09-06-10:00

********************************************************************************************************* */

#ifndef __BSP_SIMULATE_I2C__ #define __BSP_SIMULATE_I2C__

#include \

//模拟I2C结构体声明

//Simulate I2C Port struct define struct SimuI2cPortType {

GPIO_TypeDef *SCLPort;//GPIO PORT

uint32_t SCLPin; //GPIO PIN GPIO_TypeDef *SDAPort;//GPIO PORT uint32_t SDAPin; //GPIO PIN

uint8_t address; //slave address };

//模拟I2C的SCL、SDA管脚控制宏定义

//SCL output hardware operate

#define SIMUI2C_SCL_SET HAL_GPIO_WritePin(pSimuI2cPort->SCLPort, pSimuI2cPort->SCLPin, GPIO_PIN_SET)

#define SIMUI2C_SCL_CLR HAL_GPIO_WritePin(pSimuI2cPort->SCLPort, pSimuI2cPort->SCLPin, GPIO_PIN_RESET) //SDA output hardware operate

#define SIMUI2C_SDA_SET HAL_GPIO_WritePin(pSimuI2cPort->SDAPort, pSimuI2cPort->SDAPin, GPIO_PIN_SET)

#define SIMUI2C_SDA_CLR HAL_GPIO_WritePin(pSimuI2cPort->SDAPort, pSimuI2cPort->SDAPin, GPIO_PIN_RESET) //SDA input hardware operate

#define SIMUI2C_SDA_IN HAL_GPIO_ReadPin(pSimuI2cPort->SDAPort, pSimuI2cPort->SDAPin)

//simulate i2c APIs

void bsp_SimuI2C_start(struct SimuI2cPortType *pSimuI2cPort); void bsp_SimuI2C_stop(struct SimuI2cPortType *pSimuI2cPort); void bsp_SimuI2C_SandAck(struct SimuI2cPortType *pSimuI2cPort); void bsp_SimuI2C_SandNack(struct SimuI2cPortType *pSimuI2cPort); uint32_t bsp_SimuI2C_ReadAck(struct SimuI2cPortType *pSimuI2cPort); uint8_t bsp_SimuI2C_read_byte(struct SimuI2cPortType *pSimuI2cPort); void

bsp_SimuI2C_write_byte(struct SimuI2cPortType *pSimuI2cPort, uint8_t data);

#endif /* End of module include */

//-------------------------------------------------------------------------------源文件-----------------------------------------------------------------------------------------

/*

*********************************************************************************************************

* \\'-.__.-'/ | Company : o--Shen Zhen xxxxx Technology Co,.Ltd--o * / (o)(o) \\ | Website : o--http://www.xxxxx .com.cn--o * \\ \\/ / | Copyright :

* /'------'\\ | Product :

* /, .... , \\ | File : bsp_SimulateI2C.c

* /// .::::. \\\\\\ | Descript : use GPIOs to Simulate I2C communication * ///\\ :::::: /\\\\\\ | Version : V0.10

* '' ).''''.( `` | Author : nicholasldf

*=(((====)))= | EditTime : 2015-09-06-10:00

********************************************************************************************************* */

#include \#include \

/* 配置SDA管脚为输入方式

********************************************************************************************************* * function : config_sda_in

* Description : config SDA GPIO for input

* Argument(s) : point to struct SimuI2cPortType * Return(s) : none

********************************************************************************************************* */

static void config_sda_in(struct SimuI2cPortType *pSimuI2cPort) {

GPIO_InitTypeDef GPIO_InitStruct;

/* Configure SDA GPIO pin */

GPIO_InitStruct.Pin = pSimuI2cPort->SDAPin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

HAL_GPIO_Init(pSimuI2cPort->SDAPort, &GPIO_InitStruct); }

/* 配置SDA管脚为输出方式

********************************************************************************************************* * function : config_sda_out

* Description : config SDA GPIO for output

* Argument(s) : point to struct SimuI2cPortType * Return(s) : none

*********************************************************************************************************

*/

static void config_sda_out(struct SimuI2cPortType *pSimuI2cPort) {

GPIO_InitTypeDef GPIO_InitStruct;

/* Configure SDA GPIO pin */

GPIO_InitStruct.Pin = pSimuI2cPort->SDAPin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

HAL_GPIO_Init(pSimuI2cPort->SDAPort, &GPIO_InitStruct); }

/* 生成一个I2C的start开始条件,或者restart重新开始条件

********************************************************************************************************* * function : bsp_SimuI2C_start

* Description : generate a i2c start or restart * Argument(s) : point to struct SimuI2cPortType * Return(s) : none

********************************************************************************************************* */

void bsp_SimuI2C_start(struct SimuI2cPortType *pSimuI2cPort) {

//config sda pin output

config_sda_out(pSimuI2cPort);

//here may be a stop SIMUI2C_SCL_SET;

delay_us(1);//SCL setup time for STOP condition, 0.6uS SIMUI2C_SDA_SET;

delay_us(2);//the bus must be free before a new transmission can start, 1.2uS

//start

SIMUI2C_SDA_CLR;

delay_us(1);//SCL hold time for START condition, 0.6uS SIMUI2C_SCL_CLR;

delay_us(1);//SCL low period * 0.5 = 0.65uS }

/* 生成一个I2C的stop停止条件

*********************************************************************************************************

搜索更多关于: 高度提炼的模拟I2C程序 的文档
高度提炼的模拟I2C程序.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8z7mb25n6b8jj339naeq_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top