电子说
STM32 IO口模拟I2C(附源代码)
I2C英文名为inter-Integrated Circuit,是用于多设备通讯的两线式串行总线,分为主机Master和从机Slave,通常有一个主机和多个从机,从机之间可通过地址进行区分,不同种类的设备地址不同。I2C分别是时钟线SCL和数据线SDA,其中SCL和SDA由主机控制,可设置成开漏输出模式。
STM32 IO口模拟I2C实例代码1:
#define IIC_SCL_1 GPIO_SetBits(GPIOB, GPIO_Pin_6) /* SCL = 1 */
#define IIC_SCL_0 GPIO_ResetBits(GPIOB, GPIO_Pin_6) /* SCL = 0 */
#define IIC_SDA_1 GPIO_SetBits(GPIOB, GPIO_Pin_7) /* SDA = 1 */
#define IIC_SDA_0 GPIO_ResetBits(GPIOB, GPIO_Pin_7) /* SDA = 0 */
#define IIC_READ_SDA() GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) /* 读SDA口线状态 */
//初始化IIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
IIC_Stop();
}
//产生IIC起始信号
//SCL为高电平时SDA由高变低
void IIC_Start(void)
{
IIC_SDA_1;
IIC_SCL_1;
delay_us(4);
IIC_SDA_0;
delay_us(4);
IIC_SCL_0;
}
//产生IIC停止信号
//SCL为高电平时SDA由低变高
//IIC空闲时SCL和SDA均输出高电平,这样不会干扰其他设备的收发
void IIC_Stop(void)
{
IIC_SDA_0;
IIC_SCL_1;
delay_us(4);
IIC_SDA_1;
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
uint8_t IIC_WaitAck(void)
{
uint8_t errCount = 0;
uint8_t ack = 0;
IIC_SDA_1;
delay_us(4);
IIC_SCL_1;
delay_us(4);
while(IIC_READ_SDA())
{
errCount++;
if(errCount 》 250){
ack = 1;
break;
}
}
IIC_SCL_0;
return ack;
}
//产生应答ACK
//SCL为高电平时SDA为低电平表示应答
void IIC_Ack(void)
{
IIC_SDA_0;
delay_us(4);
IIC_SCL_1;
delay_us(4);
IIC_SCL_0;
delay_us(4);
IIC_SDA_1; //释放SDA
}
//产生非应答NACK
//SCL为高电平时SDA为高电平表示非应答
void IIC_NAck(void)
{
IIC_SDA_1;
delay_us(4);
IIC_SCL_1;
delay_us(4);
IIC_SCL_0;
delay_us(4);
}
//IIC发送一个字节
void IIC_WriteByte(uint8_t txd)
{
uint8_t i;
IIC_SCL_0;
for(i = 0; i 《 8; i++)
{
(txd & 0x80) ? IIC_SDA_1 : IIC_SDA_0;
txd 《《= 1;
delay_us(4);
IIC_SCL_1;
delay_us(4);
IIC_SCL_0;
delay_us(4);
}
IIC_SDA_1;
}
//读1个字节,ack=1时,发送ACK,ack=0,发送NACK
uint8_t IIC_ReadByte(uint8_t ack)
{
uint8_t i, rcv = 0;
for(i = 0; i 《 8; i++)
{
rcv 《《= 1;
IIC_SCL_1;
delay_us(4);
if(IIC_READ_SDA()){
rcv++;
}
IIC_SCL_0;
delay_us(4);
}
ack ? IIC_Ack() : IIC_NAck();
return rcv;
}
STM32 IO口模拟I2C实例代码2:
#define I2C_Speed 100000#define I2C_EE I2C1#define uStatus 0x80#define dTime 5#define I2C_EE_GPIO GPIOB#define I2C_EE_SCL GPIO_Pin_5#define I2C_EE_SDA GPIO_Pin_4#define I2C_EE_CLK RCC_APB1Periph_I2C1#define SCL_H GPIOB-》BSRR = GPIO_Pin_5#define SCL_L GPIOB-》BRR = GPIO_Pin_5#define SDA_H GPIOB-》BSRR = GPIO_Pin_4#define SDA_L GPIOB-》BRR = GPIO_Pin_4#define SCL_read GPIOB-》IDR & GPIO_Pin_5#define SDA_read GPIOB-》IDR & GPIO_Pin_4static unsigned int cntForInitial = 0;static unsigned char fSigStatus = 0;//static bool LedStatus = true;void I2C_Init(){GPIO_InitTypeDef GPIO_InitStructure;//* Configure I2C_EE pins: SCL and SDAGPIO_InitStructure.GPIO_Pin = I2C_EE_SCL | I2C_EE_SDA;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);}static void i2c_start(){SDA_H;SCL_H;DelayUs(dTime);SDA_L;DelayUs(dTime);SCL_L;DelayUs(dTime);// LedStatus = !LedStatus;//f_LCT1(LedStatus);}/*******************************************************************Fuction:Stop i2c*******************************************************************/static void i2c_stop(){SDA_L;SCL_H;DelayUs(dTime);SDA_H;DelayUs(dTime);SCL_H;DelayUs(dTime);}/*******************************************************************Fuction:i2c Master wait for ackOnly ack*******************************************************************/static unsigned char i2c_rd_ack(){unsigned char flag = 0;SDA_H;SCL_H;DelayUs(dTime/2);flag = SDA_read;DelayUs(dTime/2);SCL_L;DelayUs(dTime/2);if(flag == 1)return 0;return 1;}/*******************************************************************Fuction:i2c Byte transmissionOnly Send,no ack,no stop*******************************************************************/static unsigned char i2c_sb(unsigned char Byte){unsigned char cnt;SCL_L;for(cnt=0;cnt《8;cnt++){if(Byte&0x80)SDA_H;elseSDA_L;DelayUs(dTime);SCL_H;DelayUs(dTime);SCL_L;Byte 《《= 1;DelayUs(dTime);}return i2c_rd_ack();}/*******************************************************************Fuction:i2c Byte receiveReturn Byte*******************************************************************/static unsigned char i2c_rb(){unsigned char cnt;unsigned char Byte=0;SDA_H;for(cnt=0;cnt《8;++cnt){Byte 《《= 1;DelayUs(dTime);SCL_H;DelayUs(dTime);if(SDA_read)Byte |= 0x01;SCL_L;DelayUs(dTime);}return Byte;}/*******************************************************************Fuction:i2c ACK Master send*******************************************************************/static void i2c_wr_ack(unsigned char ACK){if(ACK)SDA_H;elseSDA_L;SCL_H;DelayUs(dTime);SCL_L;DelayUs(dTime);}/*******************************************************************Fuction:i2c Byte receiveReturn Byte*******************************************************************/uint8_t ReadReg(unsigned int addr){uint8_t temp;i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((uint8_t)(addr 》》 8))){i2c_stop();return 0;}if(!i2c_sb((uint8_t)(addr & 0xFF))){i2c_stop();return 0;}i2c_start();if(!i2c_sb(BRG_DEV_ADDR |0x01)){i2c_stop();return 0;}temp = i2c_rb();i2c_wr_ack(1);i2c_stop();return temp;}unsigned char WriteReg8(unsigned int addr,unsigned char wData){i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr 》》 8))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr & 0xFF))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData))){i2c_stop();return 0;}i2c_stop();//DelayUs(1);return 1; }unsigned char WriteReg16(unsigned int addr,unsigned char wData1,unsigned char wData2){i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr 》》 8))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr & 0xFF))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData2))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData1))){i2c_stop();return 0;}i2c_stop();DelayUs(1);return 1; }unsigned char WriteReg32(unsigned int addr,unsigned char wData1,unsigned char wData2,unsigned char wData3,unsigned char wData4){i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr 》》 8))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr & 0xFF))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData4))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData3))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData2))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData1))){i2c_stop();return 0;}i2c_stop();DelayUs(1);return 1;}
以上是STM32 IO口模拟I2C的源代码,希望对用户有所帮助。
本文整合自CSDN、博客园
责编AJX
全部0条评论
快来发表一下你的评论吧 !