当前位置:文档之家› STM32 IIC通用程序

STM32 IIC通用程序

#ifndef __I2C_Configuration_H
#define __I2C_Configuration_H
#include
#include"GPIO_Configuration.h"
#include"RCC_Configuration.h"
#include"Delay_Configuration.h"

typedef struct
{GPIO_TypeDef* GPIOx;
u16 GPIO_Pin;
}I2C_SCL;
I2C_SCL SCL;

typedef struct
{GPIO_TypeDef* GPIOx;
u16 GPIO_Pin;
}I2C_SDA;
I2C_SDA SDA;

/*IIC总线启动*/
void I2C_Start ()//I2C开始
{GPIO_Output_I2C_Configuration(SCL.GPIOx,SCL.GPIO_Pin); //SCL置输出
GPIO_Output_I2C_Configuration(SDA.GPIOx,SDA.GPIO_Pin); //SDA置输出
GPIO_SetBits(SDA.GPIOx, SDA.GPIO_Pin);//SDA=1;
delay_us(10);
GPIO_SetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=1;
delay_us(10);
GPIO_ResetBits(SDA.GPIOx, SDA.GPIO_Pin);//SDA=0;
delay_us(10);
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
delay_us(10);
}

/*IIC总线结束*/
void I2C_Stop () //I2C结束
{
GPIO_Output_I2C_Configuration(SDA.GPIOx,SDA.GPIO_Pin); //SDA置输出
GPIO_ResetBits(SDA.GPIOx, SDA.GPIO_Pin);//SDA=0;
delay_us(10);
GPIO_SetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=1;
delay_us(10);
GPIO_SetBits(SDA.GPIOx, SDA.GPIO_Pin);//SDA=1;
delay_us(10);
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
delay_us(10);
}

/*主机检查从机的响应信号*/
bool I2C_Slave_ACK(void) // 检查从机应答信号,返回0有ACK,返回1无ACK
{ bool ACK;
u8 s1;
GPIO_Input_I2C_Configuration(SDA.GPIOx,SDA.GPIO_Pin); //SDA置输入
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
GPIO_SetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=1;
s1=GPIO_ReadInputDataBit(SDA.GPIOx,SDA.GPIO_Pin);
if (s1)
{ACK = TRUE;}
else
{ACK = FALSE;}
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
return(ACK);
}

/*主机发送IIC总线上一个字节数据*/
void I2C_SendByte(u8 data) //发送一个字节
{u8 bitcount=8; //发送8位
GPIO_Output_I2C_Configuration(SDA.GPIOx,SDA.GPIO_Pin); //SDA置输出
do
{ if((data&0x80)==0x80)
{GPIO_SetBits(SDA.GPIOx, SDA.GPIO_Pin);}//SDA=1,写 1
else
{GPIO_ResetBits(SDA.GPIOx, SDA.GPIO_Pin);}//SDA=0,写 0
data=data<<1;
delay_us(10);
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
delay_us(10);
GPIO_SetBits(SCL.GPIOx, SCL.GPIO_Pin); //SCL=1;
delay_us(10); //在时钟大于4u秒期间写数据
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
bitcount--;
} while(bitcount);
}

/*主机接收IIC总线上一个字节数据*/
u8 I2C_ReciveByte(void) //接受一个字节
{
u8 s1=0,temp1=0;
u8 bitcount1=8;
GPIO_Input_I2C_Configuration(SDA.GPIOx,SDA.GPIO_Pin); //SDA置输入
GPIO

_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
do
{delay_us(10); //在时钟大于4u秒期间读数据
GPIO_SetBits(SCL.GPIOx, SCL.GPIO_Pin); //SCL=1;

s1=GPIO_ReadInputDataBit(SDA.GPIOx, SDA.GPIO_Pin);
if(s1) //读 1
{temp1=temp1|0x01;}
else //读 0
{temp1=temp1&0xfe;}
delay_us(10);
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
if(bitcount1-1)
{temp1=temp1<<1;}
bitcount1--;
}while(bitcount1);
return(temp1);
}

/*主机向IC总线发送连续读信号*/
void I2C_ack(void) // 发送连续读信号
{
GPIO_Output_I2C_Configuration(SDA.GPIOx,SDA.GPIO_Pin); //SDA置输出;
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
delay_us(5);
GPIO_ResetBits(SDA.GPIOx, SDA.GPIO_Pin);//SDA=0;
delay_us(5);
GPIO_SetBits(SCL.GPIOx, SCL.GPIO_Pin); //SCL=1;
delay_us(5);
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
}

/*主机向IC总线发送不连续读信号*/
void I2C_nack(void) // 发送不连续读信号
{
GPIO_Output_I2C_Configuration(SDA.GPIOx,SDA.GPIO_Pin); //SDA置输出 ;
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
delay_us(5);
GPIO_SetBits(SDA.GPIOx, SDA.GPIO_Pin);//SDA=1;
delay_us(5);
GPIO_SetBits(SCL.GPIOx, SCL.GPIO_Pin); //SCL=1;
delay_us(5);
GPIO_ResetBits(SCL.GPIOx, SCL.GPIO_Pin);//SCL=0;
}
#endif

相关主题
文本预览
相关文档 最新文档