单片机GPIO模拟I2C

#include "main.h"
#include "up_delay.h"
#include "up_i2c.h"

 /**
  * @file   I2C_Configuration
  * @brief  EEPROM管脚配置
  * @param  无
  * @retval 无
  */
void up_i2c_init(void)
{
	GPIO_InitType GPIO_InitStructure;
	RCC_APB_Peripheral_Clock_Enable(I2C_SCL_GPIO_RCC);
	GPIO_Structure_Initialize(&GPIO_InitStructure);
		
	/* Select the GPIO pin to control */
	GPIO_InitStructure.Pin          = I2C_SCL_PIN;
	GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_OUT_OD;
	GPIO_InitStructure.GPIO_Current = GPIO_HIGH_DREIVE;
	GPIO_Peripheral_Initialize(I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
	
	GPIO_InitStructure.Pin          = I2C_SDA_PIN;
	GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_OUT_OD;
	GPIO_InitStructure.GPIO_Current = GPIO_HIGH_DREIVE;
	GPIO_Peripheral_Initialize(I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
	
	SDA_H;
	SCL_H;
}

 /**
  * @file   I2C_delay
  * @brief  延迟时间
  * @param  无
  * @retval 无
  */

#define US_CONFIG  2

static void I2C_delay(void)
{	
  SysTick_Delay_Us(US_CONFIG);
}

/**
  * @file   I2C_Start
  * @brief  无
  * @param  无
  * @retval 无
  */
FunctionalState I2C_Start(void)
{
	SDA_H;
	SCL_H;
	I2C_delay();
	if(!SDA_read)return DISABLE;	/* SDA线为低电平则总线忙,退出 */
	SDA_L;
	I2C_delay();
	if(SDA_read) return DISABLE;	/* SDA线为高电平则总线出错,退出 */
	SDA_L;
	I2C_delay();
	return ENABLE;
}

 /**
  * @file   I2C_Stop
  * @brief  无
  * @param  无
  * @retval 无
  */
void I2C_Stop(void)
{
	SCL_L;
	I2C_delay();
	SDA_L;
	I2C_delay();
	SCL_H;
	I2C_delay();
	SDA_H;
	I2C_delay();
}

 /**
  * @file   I2C_Ack
  * @brief  无
  * @param  无
  * @retval 无
  */
void I2C_Ack(void)
{	
	SCL_L;
	I2C_delay();
	SDA_L;
	I2C_delay();
	SCL_H;
	I2C_delay();
	SCL_L;
	I2C_delay();
}

 /**
  * @file   I2C_NoAck
  * @brief  无
  * @param  无
  * @retval 无
  */
void I2C_NoAck(void)
{	
	SCL_L;
	I2C_delay();
	SDA_H;
	I2C_delay();
	SCL_H;
	I2C_delay();
	SCL_L;
	I2C_delay();
}

 /**
  * @file   I2C_WaitAck
  * @brief  无
  * @param  无
  * @retval 返回为:=1有ACK,=0无ACK
  */
FunctionalState I2C_WaitAck(void) 	
{
	SCL_L;
	I2C_delay();
	SDA_H;			
	I2C_delay();
	SCL_H;
	I2C_delay();
	if(SDA_read)
	{
      SCL_L;
      return DISABLE;
	}
	SCL_L;
	return ENABLE;
}

 /**
  * @file    I2C_SendByte
  * @brief   数据从高位到低位
  * @param    SendByte- SendByte: 发送的数据
  * @retval  无
  */
void I2C_SendByte(uint8_t SendByte) 
{
    uint8_t i=8;
    while(i--)
    {
			SCL_L;
			I2C_delay();
      if(SendByte&0x80){
        SDA_H;  
			}else{ 
        SDA_L;   
			}
			SendByte<<=1;
			I2C_delay();
			SCL_H;
			I2C_delay();
    }
    SCL_L;
}

 /**
  * @file    I2C_ReceiveByte
  * @brief   数据从高位到低位
  * @param    None
  * @retval  I2C总线返回的数据
  */
uint8_t I2C_ReceiveByte(void)  
{ 
    uint8_t i=8;
    uint8_t ReceiveByte=0;

    SDA_H;				
    while(i--)
    {
      ReceiveByte<<=1;      
      SCL_L;
      I2C_delay();
			SCL_H;
      I2C_delay();	
      if(SDA_read)
      {
        ReceiveByte|=0x01;
      }
    }
    SCL_L;
    return ReceiveByte;
}

//uint8_t AHT20_Read_Status(void)//读取AHT20的状态寄存器
//{

//	uint8_t Byte_first;	
//	I2C_Start();
//	I2C_SendByte(0x71);
//	I2C_Ack();
//	Byte_first = I2C_ReceiveByte();
//	I2C_NoAck();
//	I2C_Stop();
//	return Byte_first;
//}

/
#define I2C_SCL_GPIO_PORT   	      GPIOA  
#define I2C_SCL_GPIO_RCC      			RCC_APB_PERIPH_IOPA 

#define I2C_SCL_PIN      					 	GPIO_PIN_4  
#define I2C_SDA_PIN      					 	GPIO_PIN_5   

/* Private define ------------------------------------------------------------*/
#define SCL_H         {I2C_SCL_GPIO_PORT->PBC  = I2C_SCL_PIN;}	 
#define SCL_L         {I2C_SCL_GPIO_PORT->PBSC = I2C_SCL_PIN;}   
   
#define SDA_H         {I2C_SCL_GPIO_PORT->PBC  = I2C_SDA_PIN;}	
#define SDA_L         {I2C_SCL_GPIO_PORT->PBSC = I2C_SDA_PIN;}	

#define SCL_read      GPIO_Input_Pin_Data_Get(I2C_SCL_GPIO_PORT, I2C_SCL_PIN)  
#define SDA_read      GPIO_Input_Pin_Data_Get(I2C_SCL_GPIO_PORT, I2C_SDA_PIN)	


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌风_lwp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值