我正在用的SHT30程序,模拟时序:
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//=============================================================================
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_4|GPIO_Pin_1|GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*
SCL_1;
SDA1_1;
SCL_0;
SDA1_0;
SCL_1;
SDA1_1;
IIC_SCL_1;
IIC_SCL_0;
IIC_SCL_1;
IIC_SCL_0;
IIC_SDA1_1;
IIC_SDA1_0;
IIC_SDA1_1;
IIC_SDA1_0;
IIC_SDA1_1;
IIC_SDA1_0;
IIC_SDA1_1;
IIC_SDA1_0;
IIC_SDA2_1;
IIC_SDA2_0;
IIC_SDA2_1;
IIC_SDA2_0;
IIC_SDA3_1;
IIC_SDA3_0;
IIC_SDA3_1;
IIC_SDA3_0;
IIC_SDA4_1;
IIC_SDA4_0;
IIC_SDA4_1;
IIC_SDA4_0;
*/
}
/***********************************************
调用方式:void I2Cstart(void)
************************************************/
void I2CStart1(void)
{
SDA1_1;
SCL_1;
Delay_Nus(N1);
SDA1_0;
Delay_Nus(N1);
SCL_0;
}
void I2CStart2(void)
{
SDA2_1;
SCL_1;
Delay_Nus(N1);
SDA2_0;
Delay_Nus(N1);
SCL_0;
}
void I2CStart3(void)
{
SDA3_1;
SCL_1;
Delay_Nus(N1);
SDA3_0;
Delay_Nus(N1);
SCL_0;
}
void I2CStart4(void)
{
SDA4_1;
SCL_1;
Delay_Nus(N1);
SDA4_0;
Delay_Nus(N1);
SCL_0;
}
/***********************************************
停止:void I2CStop(void)
************************************************/
void I2CStop1(void)
{
SCL_0;
SDA1_0;
Delay_Nus(N1);
SCL_1; Delay_Nus(N2);
SDA1_1;//stop
Delay_Nus(N1);
}
void I2CStop2(void)
{
SCL_0;
SDA2_0;
Delay_Nus(N1);
SCL_1; Delay_Nus(N2);
SDA2_1;//stop
Delay_Nus(N1);
}
void I2CStop3(void)
{
SCL_0;
SDA3_0;
Delay_Nus(N1);
SCL_1; Delay_Nus(N2);
SDA3_1;//stop
Delay_Nus(N1);
}
void I2CStop4(void)
{
SCL_0;
SDA4_0;
Delay_Nus(N1);
SCL_1; Delay_Nus(N2);
SDA4_1;//stop
Delay_Nus(N1);
}
/***********************************************
应答:void SendAck(void)
主器件为接收方,从器件为发送方,应答信号
************************************************/
void SendAck1(void)
{
SCL_0;
SDA1_0;//低电平表示应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
void SendAck2(void)
{
SCL_0;
SDA2_0;//低电平表示应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
void SendAck3(void)
{
SCL_0;
SDA3_0;//低电平表示应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
void SendAck4(void)
{
SCL_0;
SDA4_0;//低电平表示应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
/***********************************************
非应答:void SendNotAck(void)
主器件为接收方,从器件为发送方
************************************************/
void SendNotAck1(void)
{
SCL_0;
SDA1_1;//高电平表示非应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
void SendNotAck2(void)
{
SCL_0;
SDA2_1;//高电平表示非应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
void SendNotAck3(void)
{
SCL_0;
SDA3_1;//高电平表示非应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
void SendNotAck4(void)
{
SCL_0;
SDA4_1;//高电平表示非应答
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
}
/***********************************************
等待应答?unsigned char WaitAck(void)
等待从器件接收方的应答
************************************************/
unsigned char WaitAck1(void)
{
unsigned char errtime=255;//因故障接收方无ACK,超时值为255
SDA1_1;//SDA,SCL均高,释放数据线准备接收应答
SCL_1;
Delay_Nus(N1);
//切换为输入
// {GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=(u32)8<<20;}//利用上拉此行可省略
while(GPIOA->IDR&PIN_SDA1)//判断从设备输出SDA是否为1,
{
errtime--;
if(!errtime){I2CStop1();SystemError=0x11;return 0;}
}
SCL_0;Delay_Nus(N1);
return 1;
}
unsigned char WaitAck2(void)
{
unsigned char errtime=255;//因故障接收方无ACK,超时值为255
SDA2_1;//SDA,SCL均高,释放数据线准备接收应答
SCL_1;
Delay_Nus(N1);
//切换为输入
// {GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=(u32)8<<20;}//利用上拉此行可省略
while(GPIOA->IDR&PIN_SDA2)//判断从设备输出SDA是否为1,
{
errtime--;
if(!errtime){I2CStop2();SystemError=0x11;return 0;}
}
SCL_0;Delay_Nus(N1);
return 1;
}
unsigned char WaitAck3(void)
{
unsigned char errtime=255;//因故障接收方无ACK,超时值为255
SDA3_1;//SDA,SCL均高,释放数据线准备接收应答
SCL_1;
Delay_Nus(N1);
//切换为输入
// {GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=(u32)8<<20;}//利用上拉此行可省略
while(GPIOA->IDR&PIN_SDA3)//判断从设备输出SDA是否为1,
{
errtime--;
if(!errtime){I2CStop3();SystemError=0x11;return 0;}
}
SCL_0;Delay_Nus(N1);
return 1;
}
unsigned char WaitAck4(void)
{
unsigned char errtime=255;//因故障接收方无ACK,超时值为255
SDA4_1;//SDA,SCL均高,释放数据线准备接收应答
SCL_1;
Delay_Nus(N1);
//切换为输入
// {GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=(u32)8<<20;}//利用上拉此行可省略
while(GPIOA->IDR&PIN_SDA4)//判断从设备输出SDA是否为1,
{
errtime--;
if(!errtime){I2CStop4();SystemError=0x11;return 0;}
}
SCL_0;Delay_Nus(N1);
return 1;
}
/***********************************************
发送字节:void I2CSendByte(unsigned char ch)
发送一个字节
************************************************/
void I2CSendByte1(unsigned char ch)
{
unsigned char i=8;
while(i--)
{
if((ch&0x80)==0x80){SDA1_1;}//字节的最高位为1,SDA=1,否则SDA=0
else{SDA1_0;}
ch<<=1;
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
Delay_Nus(N1);
}
}
void I2CSendByte2(unsigned char ch)
{
unsigned char i=8;
while(i--)
{
if((ch&0x80)==0x80){SDA2_1;}//字节的最高位为1,SDA=1,否则SDA=0
else{SDA2_0;}
ch<<=1;
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
Delay_Nus(N1);
}
}
void I2CSendByte3(unsigned char ch)
{
unsigned char i=8;
while(i--)
{
if((ch&0x80)==0x80){SDA3_1;}//字节的最高位为1,SDA=1,否则SDA=0
else{SDA3_0;}
ch<<=1;
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
Delay_Nus(N1);
}
}
void I2CSendByte4(unsigned char ch)
{
unsigned char i=8;
while(i--)
{
if((ch&0x80)==0x80){SDA4_1;}//字节的最高位为1,SDA=1,否则SDA=0
else{SDA4_0;}
ch<<=1;
Delay_Nus(N1);
SCL_1;
Delay_Nus(N1);
SCL_0;
Delay_Nus(N1);
}
}
/***********************************************
接收字节:Byte I2CReceieveByte(void)
接收一个字节
************************************************/
unsigned char I2CReceieveByte1(void)
{
unsigned char i=8;
unsigned char ddata=0;
SDA1_1;
while(i--)
{
ddata<<=1;
SCL_0;
Delay_Nus(N1);//等待从器件送出数据
SCL_1;
Delay_Nus(N1);
//切换为输入
if(READ_SDA1)ddata++;//非0,
Delay_Nus(N1);
}
return ddata;
}
unsigned char I2CReceieveByte2(void)
{
unsigned char i=8;
unsigned char ddata=0;
SDA2_1;
while(i--)
{
ddata<<=1;
SCL_0;
Delay_Nus(N1);//等待从器件送出数据
SCL_1;
Delay_Nus(N1);
//切换为输入
if(READ_SDA2)ddata++;//非0,
Delay_Nus(N1);
}
return ddata;
}
unsigned char I2CReceieveByte3(void)
{
unsigned char i=8;
unsigned char ddata=0;
SDA3_1;
while(i--)
{
ddata<<=1;
SCL_0;
Delay_Nus(N1);//等待从器件送出数据
SCL_1;
Delay_Nus(N1);
//切换为输入
if(READ_SDA3)ddata++;//非0,
Delay_Nus(N1);
}
return ddata;
}
unsigned char I2CReceieveByte4(void)
{
unsigned char i=8;
unsigned char ddata=0;
SDA4_1;
while(i--)
{
ddata<<=1;
SCL_0;
Delay_Nus(N1);//等待从器件送出数据
SCL_1;
Delay_Nus(N1);
//切换为输入
if(READ_SDA4)ddata++;//非0,
Delay_Nus(N1);
}
return ddata;
}
//
void SHT_INI(void)
{
I2CStart1();
I2CSendByte1(0x88);//器件的写地址
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x20);//
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x2F);//
Delay_Nus(N2);
WaitAck1();
}
void SHT_SoftReset(void)
{
I2CStart1();
I2CSendByte1(0x88);//器件的写地址
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x30);//
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0xA2);//
Delay_Nus(N2);
WaitAck1();
I2CStop1();}
void SHT_Heat(void)
{
I2CStart1();
I2CSendByte1(0x88);//器件的写地址
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x30);//
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x6D);//
Delay_Nus(N2);
WaitAck1();
I2CStop1();
}
void SHT_DisHeat(void)
{
I2CStart1();
I2CSendByte1(0x88);//器件的写地址
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x30);//
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x66);//
Delay_Nus(N2);
WaitAck1();
I2CStop1();
}
void ReadSHT1(unsigned char ad,unsigned char count,unsigned char *buff)
{
unsigned char i;
I2CStart1();
I2CSendByte1(0x88);//器件的写地址
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x2C);//
Delay_Nus(N2);
WaitAck1();
I2CSendByte1(0x06);//
Delay_Nus(N2);
WaitAck1();
I2CStart1();
I2CSendByte1(0x89);//器件的读地址
Delay_Nus(N2);
WaitAck1();
SCL_1;//释放SCL
//切换为输入
//{GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=(u32)8<<20;}//利用上拉此行可省略
while((GPIOA->IDR&PIN_SCL)==0)//等待转换完毕
{
;
}
I2CStart1();
I2CSendByte1(0x89);//器件的读地址
Delay_Nus(N2);
WaitAck1();
for(i=0;i<count;i++)//连续读取count个数值
{
buff[i]=I2CReceieveByte1();
if(i!=count-1)SendAck1();//除非最后一个字节,其它都要从MASTER发应答
}
SendNotAck1();
I2CStop1();
} |