c - How to get device ID of DSTH01 I2C Communication? -
i trying humidity , temperature using dsth01. not able proper value .i getting humidity -24 , temperature -50. don't know missing. please guide me resolve problem. if device id solve problem. using pic18f2520 10 mhz crystal.
i have done following connection:
pin1-gnd pin2-pull 3.3 v 10k- microcontroller pin_c3 pin3 - pull 3.3 v 10k-microcontroller pin_c4 pin4 - microcontroller pin_a2 pin5 - 3.3 v
here source code:
#define dsth01_sck pin_c3 #define dsth01_sda pin_c4 #define dsth01_csb pin_a2 #define sda_h output_high(dsth01_sda) #define sda_l output_low(dsth01_sda) #define sck_h output_high(dsth01_sck) #define sck_l output_low(dsth01_sck) #define cs_h output_high(dsth01_csb) #define cs_l output_low(dsth01_csb) #define slaveaddress 0x40 #define registeraddress0 0x00 #define registeraddress1 0x01 #define registeraddress2 0x02 #define registeraddress3 0x03 #define registeraddress11 0x11 void small_delay(void); void i2c_start(void); void i2c_stop(void); void i2c_write(unsigned int8 data); int8 i2c_read(void); void i2c_ack(unsigned int8 a); int8 i2c_send(unsigned int8 slaveadd,unsigned int8 registeradd,unsigned int8 *s); int8 i2c_receive(unsigned int8 slaveadd,unsigned int8 registeradd,unsigned int8 *s); void temperature_read(unsigned int8 *s); void relativehumidity_read(unsigned int8 *s); int1 check_sda(void); unsigned int8 ack; unsigned int8 id; unsigned int8 relativehumidity =0; unsigned int8 temperature =0; unsigned int8 start1=0x01; unsigned int8 start2=0x11; void main(void) { set_tris_c(0x80); delay_ms(100);delay_ms(100); output_low(pin_c3); output_high(pin_c4); output_low(pin_a2); enable_interrupts(int_rda); enable_interrupts(global); delay_ms(100);delay_ms(100); delay_ms(100);delay_ms(100); delay_ms(100);delay_ms(100); cs_l; i2c_receive(slaveaddress,registeraddress11,&id); while (1) { relativehumidity_read(&relativehumidity); temperature_read(&temperature); delay_ms(100);delay_ms(100); delay_ms(100);delay_ms(100); delay_ms(100);delay_ms(100); printf("\r\nrelativehumidity:%d",relativehumidity); printf("\ttemperature:%d",temperature); } } void i2c_start() { sda_h;small_delay(); sck_h;small_delay(); sda_l;small_delay(); sck_l;small_delay(); } void i2c_stop() { sda_l;small_delay(); sck_h;small_delay(); sda_h;small_delay(); } void i2c_write(unsigned int8 data) { unsigned int8 i; for(i=0;i<8;i++) { if(data&0x80) {sda_h;small_delay();} else sda_l;small_delay(); sck_h;small_delay(); sck_l;small_delay(); data<<=1; } sda_h;small_delay(); sck_h;small_delay(); //if(sda==1) if(check_sda()==1) ack=0; else ack=1; sck_l;small_delay(); } unsigned int8 i2c_read() { unsigned int8 data=0; unsigned int8 i; sda_h; for(i=0;i<8;i++) { sck_l;small_delay(); sck_h;small_delay(); data<<=1; //if(sda==1) if(check_sda()==1) data=data+1; } sck_l;small_delay(); return data; } void i2c_ack(unsigned int8 a) { if(a==0) {sda_l;small_delay();} else sda_h;small_delay(); sck_h;small_delay(); sck_l;small_delay(); } unsigned int8 i2c_send(unsigned int8 slaveadd,unsigned int8 registeradd,unsigned int8 *s) { slaveadd=slaveadd<<1; i2c_start(); i2c_write(slaveadd); if(ack==0) return 0; i2c_write(registeradd); if(ack==0) return 0; i2c_write(*s); if(ack==0) return 0; i2c_stop(); return 1; } unsigned int8 i2c_receive(unsigned int8 slaveadd,unsigned int8 registeradd,unsigned int8 *s) { slaveadd = slaveadd<<1; i2c_start(); i2c_write(slaveadd); if(ack==0) return 0; i2c_write(registeradd); if(ack==0) return 0; i2c_start(); i2c_write(slaveadd+1); if(ack==0) return 0; *s=i2c_read(); i2c_ack(1); i2c_stop(); return 1; } void relativehumidity_read(unsigned int8 *s) { unsigned int8 status=1; unsigned int8 relativehumidityh; unsigned int8 relativehumidityl; unsigned int16 relhum; i2c_send(slaveaddress,registeraddress3,&start1); while(status==1) { i2c_receive(slaveaddress,registeraddress0,&status); } i2c_receive(slaveaddress,registeraddress1,&relativehumidityh); i2c_receive(slaveaddress,registeraddress2,&relativehumidityl); relhum=relativehumidityh; relhum=relhum<<8; relhum+=relativehumidityl; relhum=relhum>>4; *s=relhum/16-24; } void temperature_read(unsigned int8 *s) { unsigned int8 status=1; unsigned int8 temperatureh; unsigned int8 temperaturel; unsigned int16 temp; i2c_send(slaveaddress,registeraddress3,&start2); while(status==1) { i2c_receive(slaveaddress,registeraddress0,&status); } i2c_receive(slaveaddress,registeraddress1,&temperatureh); i2c_receive(slaveaddress,registeraddress2,&temperaturel); temp=temperatureh; temp<<=8; temp+=temperaturel; temp>>=2; *s=temp/32-50; } int1 check_sda(void) { int1 a; set_tris_c(0x90);//10010000,rx,0,0,sda,0,0,0,0 small_delay(); if(input(dsth01_sda)){a =1; } else{a = 0;} set_tris_c(0x80);//10000000 small_delay(); return a; } void small_delay(void) { char a; for(a = 0;a < 10;a++); }
update deleted answer because thought faulty , useless, perhaps there here op after ...
how can getting negative values unsigned
variables?
looking @ dsth01 data sheet, % relative humidity calculated (rh/16)-24
, suggests code has unnecessary line
relhum=relhum>>4; // remove line *s=relhum/16-24;
next, data sheet says temperature calulated (temp/32)-50
, suggests code has unnecessary line
temp>>=2; // remove line *s=temp/32-50;
assuming have read i2c data correctly.
the data sheet has error in table 6 shows humidity 11 bits. has error in table 8 instead of showing temperature repeats same table humidity, table 6.
i think doing divisions because data sheet says results 12-bit , 14-bit respectively, inference data left-aligned, why divisions (actually shifts) required - once! suggest conversions data sheet says: use datah
& datal
, these zero-padded on right. code might better
relhum = (relhum >> 4) - 24; // div 16 *s = (unsigned int8)relhum; // cast 8-bit
and
temp = (temp >> 5) - 50; // div 32 *s = (unsigned int8)temp; // cast 8-bit
Comments
Post a Comment