em có tham khảo code trên mạng , đọc toàn trả về 0xFF, bác nào từng làm rồi cho em tham khảo. Không thì các bác xem giúp em code
typedef signed char int8_t;
typedef signed int int16_t;
typedef signed long int int32_t;
/* Kieu So Nguyen Khong Dau */
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned long int uint32_t;
/* Kieu So Thuc */
typedef float float32_t;
#define SHT_NOACK 0
#define SHT_ACK 1
#define SHT_STATUS_REG_W 0x06 //0000 0110
#define SHT_STATUS_REG_R 0x07 //0000 0111
#define SHT_MEASURE_TEMP 0x03 //0000 0011
#define SHT_MEASURE_HUMI 0x05 //0000 0101
#define SHT_RESET 0x1E //0001 1110
//Cac hang so voi do phan giai 14 va 12 bit
#define H_C1 -4.0
#define H_C2 0.0405
#define H_C3 -0.0000028
#define H_D1 -40.1
#define H_D2 0.01
#define H_T1 0.01
#define H_T2 0.00008
//Cac hang so voi do phan giai 12 va 8 bit
#define L_C1 -4.0
#define L_C2 0.648
#define L_C3 -0.00072
#define L_D1 -40.2
#define L_D2 0.04
#define L_T1 0.01
#define L_T2 0.00128
//Define Pin PIC Use for SHT10
#define SHT_CLK RC3
#define SHT_DDR_CLK TRISC3
#define SHT_DATA_IN RC4
#define SHT_DATA_OUT RC4
#define SHT_DDR_DATA TRISC4
#define DDROUT 0
#define DDRIN 1
uint8_t SHT_Resolution;
void SHT_Start();
void SHT_ResetConection();
uint8_t SHT_WriteByte(uint8_t Data);
uint8_t SHT_ReadByte(uint8_t ack);
void SHT_Init(uint8_t resolution);
void SHT_ResetChip();
//uint16_t SHT_ReadSenSor(uint8_t Command);
int16 SHT_ReadSenSor(int8 Command);
void SHT_ReadTemHumi(float *tem, float *humi);
/*******************************************************************************
Noi Dung : Bat dau mot ket noi moi.
Tham Bien : Khong.
Tra Ve : Khong.
********************************************************************************/
void SHT_Start()
{
SHT_DDR_CLK = DDROUT; // SHT la chan ra
SHT_DDR_DATA = DDROUT; // Data la chan ra
SHT_DATA_OUT = 1;
SHT_CLK = 0;
SHT_CLK = 1;
SHT_DATA_OUT = 0;
SHT_CLK = 0;
SHT_CLK = 1;
SHT_DATA_OUT = 1;
SHT_CLK = 0;
}
/*******************************************************************************
Noi Dung : Reset ket noi moi.
Tham Bien : Khong.
Tra Ve : Khong.
********************************************************************************/
void SHT_ResetConection()
{
uint8_t i;
SHT_DDR_DATA = DDROUT; //Data la chan ra
SHT_DATA_OUT=1;
for (i=0; i<9; i++)
{
SHT_CLK=1;
SHT_CLK=0;
}
SHT_Start();
}
//! /*******************************************************************************
//!Noi Dung : Gui 1 Byte du lieu len SHT.
//!Tham Bien : Data: Byte du lieu can viet.
//!Tra Ve : 1: Neu viet Byte xay ra loi.
//! 0: Neu viet Byte thanh cong.
//!********************************************************************************/
uint8_t SHT_WriteByte(uint8_t Data)
{
uint8_t i, error1 = 0;
SHT_DDR_DATA = DDROUT; // Data la chan ra
delay_us(2);
for(i = 0x80; i > 0; i /= 2)
{
SHT_CLK = 0;
if(i & Data) SHT_DATA_OUT = 1;
else SHT_DATA_OUT = 0;
delay_us(1);
SHT_CLK = 1;
delay_us(1);
}
SHT_CLK = 0;
SHT_DDR_DATA = DDRIN; // Data la chan vao
SHT_DATA_IN=DDRIN;
delay_us(1);
SHT_CLK = 1;
delay_us(1);
error1 = SHT_DATA_IN;
SHT_CLK = 0;
delay_ms(250);
return(error1);
}
//! /*******************************************************************************
//!Noi Dung : Doc 1 Byte du lieu tu SHT.
//!Tham Bien : ack: Gia tri ACK 0,1.
//!Tra Ve : Khong.
//!********************************************************************************/
uint8_t SHT_ReadByte(uint8_t ack)
{
uint8_t i, val = 0;
SHT_DDR_DATA = DDRIN; // Data la chan vao
SHT_DATA_IN=DDRIN;
for(i = 0x80; i > 0; i /= 2)
{
SHT_CLK = 1;
delay_us(1);
if(SHT_DATA_IN) val = val | i;
delay_us(1);
SHT_CLK = 0;
}
SHT_DDR_DATA = DDROUT; // Data la chan ra
delay_us(1);
SHT_DATA_OUT = ! ack;
SHT_CLK = 1;
delay_us(1);
SHT_CLK = 0;
return(val);
}
//! /*******************************************************************************
//!Noi Dung : Khoi tao SHT.
//!Tham Bien : resolution: gia tri do phan giai.
//! - SHT_14_12_BIT
//! - SHT_12_8_BIT
//!Tra Ve : Khong.
//!********************************************************************************/
void SHT_Init(uint8_t resolution)
{
SHT_ResetConection();
SHT_WriteByte(SHT_STATUS_REG_W);
delay_ms(300);
SHT_WriteByte(resolution);
SHT_Resolution=resolution;
}
//! /*******************************************************************************
//!Noi Dung : Reset SHT.
//!Tham Bien : Khong.
//!Tra Ve : Khong.
//!********************************************************************************/
void SHT_ResetChip()
{
SHT_ResetConection();
SHT_WriteByte(SHT_RESET);
delay_ms(100);
}
//! /*******************************************************************************
//!Noi Dung : Doc 16 bit du lieu tu SHT.
//!Tham Bien : Command: Ma lenh yeu cau.
//!Tra Ve : 16 bit du lieu.
//!********************************************************************************/
int16 SHT_ReadSenSor(int8 Command)
{
int8 msb, lsb, crc;
int16 kq;
//!i2c_start();
//!i2c_write(Command);
//!msb = i2c_read();
//!lsb = i2c_read();
//!crc = i2c_read(0);
//!i2c_stop();
//!delay_ms(5);
//!kq = (int16) ((msb << 8) | lsb);
//!return(kq);
SHT_ResetConection();
SHT_WriteByte(Command);
SHT_DDR_DATA = DDRIN;
//while(SHT_DATA_IN);
msb = SHT_ReadByte(SHT_ACK);
lsb = SHT_ReadByte(SHT_ACK);
crc = SHT_ReadByte(SHT_NOACK);
return(((uint16_t) msb << 8) | (uint16_t) lsb);
}
void SHT_ReadTemHumi(float *tem, float *humi)
{
int16 SOT;
int16 SORH;
SOT = SHT_ReadSenSor(SHT_MEASURE_TEMP);
SORH= SHT_ReadSenSor(SHT_MEASURE_HUMI);
if(SHT_Resolution==SHT_14_12_BIT)
{
*tem=(H_D1+H_D2*SOT);
*humi=((H_D1+H_D2*SOT-25)*(H_T1+H_T2*SORH)+H_C1+H_C2*SORH+H_C3*SORH*SORH);
}
else
{
*tem=(L_D1+L_D2*SOT);
*humi=((L_D1+L_D2*SOT-25)*(L_T1+L_T2*SORH)+L_C1+L_C2*SORH+L_C3*SORH*SORH);
}
}
Về phần cứng 2 dây SDA và SCL em có sử dụng thêm module DS1307 nữa (trong module treo trở rồi nên Sensor em k treo trở nữa ). Các bác thẩm định giúp em.
Em cảm ơn ạ