|
楼主 |
发表于 2017-4-25 10:13:24
|
显示全部楼层
代码搞几天没找到有什么问题,,用提供给我的样品,没有问题,用给我供货的就是不行,,读取速度慢了,数据始终是最大,
可否上一份你的代码我对比一下
下面这个是代码是霍尼韦尔发给我的测试代码: 和我自己写的代码也是一样的情况,我自己代码用在很多芯片上还都没出现过这情况
这个芯片测试起来一直不顺手,按理说状态位应该为0,但从我这一直是1,不管数据正不正确,都没出现过0
void delay(unsigned char c)
{
unsigned char x,y;
for(x=c; x>0; x--)
for(y=5; y>0; y--)
;
}
void i2c_start()
{
sda=1;
delay(1);
scl=1;
delay(1);
sda=0;
delay(1);
scl=0;
delay(1);
}
void i2c_stop()
{
sda=0;
delay(1);
scl=1;
delay(1);
sda=1;
}
void i2c_ack()
{
sda=0;
delay(1);
scl=1;
delay(1);
scl=0;
sda=1;
}
void i2c_nack()
{
sda=1;
scl=1;
delay(1);
scl=0;
sda=0;
}
void i2c_readack()
{
unsigned char i;
scl=1;
delay(1);
while ((sda==1)&&i<250) i++;
scl=0;
delay(1);
}
void i2c_write(unsigned char addr) //i2c communication to write address of sensor plus one w/r bit.
{
unsigned char i;
unsigned char writedata;
writedata=addr;
for(i=0; i<8; i++)
{
scl=0;
delay(1);
writedata=writedata<<1;
sda=CY;
delay(1);
scl=1;
delay(1);
scl=0;
delay(1);
}
sda=1;
delay(1);
}
unsigned char i2c_read() //i2c communication to read one byte
{
unsigned char i;
unsigned char readbit;
unsigned char readbyte;
scl=0;
delay(1);
sda=1;
delay(1);
for(i=0; i<8; i++)
{
scl=1;
delay(1);
readbit=sda;
readbyte=(readbyte<<1)|readbit;
scl=0;
delay(1);
}
return readbyte;
}
unsigned char fetch_humidity_temperature(unsigned int *p_H_dat, unsigned int *p_T_dat)
{
unsigned char Hum_H, Hum_L, Temp_H, Temp_L, _status;
unsigned int H_dat, T_dat;
i2c_start(); //Measurment requestR
i2c_write(0x4e); //Write sensor address + WRITE "0" bit to SDA line
i2c_readack();
i2c_stop();
delay(100);
i2c_start();
i2c_write(0x4f); //write sensor address + READ "1" bit to SDA line
i2c_readack();
delay(1);
Hum_H=i2c_read(); //read first byte
i2c_ack();
Hum_L=i2c_read(); //read second byte
i2c_ack();
Temp_H =i2c_read(); //read third byte
i2c_ack();
Temp_L =i2c_read(); //read fourth byte
delay(1);
i2c_nack();
delay(1);
i2c_stop();
_status = (Hum_H >> 6) & 0x03;
Hum_H = Hum_H & 0x3f;
H_dat = (((unsigned int)Hum_H) << 8) | Hum_L;
T_dat = (((unsigned int)Temp_H) << 8)| Temp_L;
T_dat = T_dat >> 2;
*p_H_dat = H_dat;
*p_T_dat = T_dat;
return(_status);
}
|
|