member_tdh
Member level 5
sht75 pic
Hi!
I'm using a PIC16F88 to interface with an SHT75, but code not work.
Could anybody help me!?!
void comstart(void)
{
trisa1=0; //DATA-line in output
ra0=0;
ra1=1;
#asm nop #endasm
ra0=1;
#asm nop #endasm
ra1=0;
#asm nop #endasm
ra0=0;
#asm nop #endasm
ra0=1;
#asm nop #endasm
ra1=1;
#asm nop #endasm
ra0=0;
}
void comreset(void)
{
unsigned char i;
trisa1=0; //DATA-line in output
ra1=1;
ra0=0;
for(i=0;i<9;i++) //9 clock cycles
{
ra0=1;
#asm nop #endasm
ra0=0;
}
comstart(); //transmission start
HumiError = FALSE;
}
void sht_soft_reset (void)
{
comreset(); //SHT75 communication reset
comwrite(RESET); //send SHT71 reset command
}
void sht_init (void)
{
sht_soft_reset(); //reset SHT71
// comreset(); //SHT71 communication reset
delay_ms(15); //delay for power-up
}
void comwait(void)
{
unsigned int16 sht_delay;
ra1=1;
ra0=0;
for(sht_delay=0;sht_delay<30000;sht_delay++)
{
if(!ra1)
{
//printf("\n\r-> SHT71 comwait error!\n\r");
break;
}
delay_us(10);
}
}
void comwrite(unsigned char ddata)
{
unsigned char i;
trisa1=0; //DATA-line in output
for(i=0;i<8;i++)
{
ra0=0;
if(ddata&0x80) ra1=1;
else ra1=0;
ra0=1;
ddata<<=1;
#asm nop #endasm
}
ra1=1; //release DATA-line (sua them)
trisa1=1; //DATA-line in input
#asm nop #endasm
ra1=0; //(old code)
ra0=0; //(old code)
ra0=1; //clock 9th for ACK
#asm nop #endasm
if(input(PIN_A1))
{
HumiError=TRUE; //check ack (DATA will be pulled down by SHT11)
while(HumiError)
{
rb3=1;
printf("\n\r-> SHT71 writed error...ACK!\n\r");
}
}
ra0=0;
}
unsigned int comread(unsigned char back)
{
unsigned char i;
unsigned int bbyte=0;
trisa1=1; //DATA-line in input
for(i=0;i<8;i++)
{
bbyte<<=1;
ra0=1;
bbyte|=input(PIN_A1);
ra0=0;
#asm nop #endasm
}
// ra1=!back; //"ack==1" pull down DATA-Line
trisa1=0; //DATA-line in output
ra1=!back; //"ack==1" pull down DATA-Line
ra0=1;
#asm nop #endasm
ra0=0;
#asm nop #endasm
trisa1=1; //DATA-line in input
ra1=1;
return bbyte;
}
unsigned int s_measure(unsigned char mode)
{
unsigned char msb=0,lsb=0,crc;
sht_soft_reset();
comstart();
switch(mode)
{
case TEMP:
comwrite(MEASURE_TEMP);
break;
case HUMI:
comwrite(MEASURE_HUMI);
break;
default:
break;
}
trisa1=1; //DATA-line in input
comwait(); //wait for the measurement to complete
msb = comread(ACK); //read the first byte (MSB)
lsb = comread(ACK); //read the second byte (LSB)
crc = comread(noACK); //read checksum
return (msb*256 + lsb);
}
void HumidityGet(float *p_humidity, float *p_temperature)
{
unsigned int humidity=0,temperature=0;
humidity = s_measure(HUMI); //measure humidity
DoAm=humidity;
temperature = s_measure(TEMP); //measure temperature
NhietDo=temperature;
if(HumiError == TRUE) comreset(); //in case of an error: Reset
else
{
*p_humidity=(float)humidity; //converts integer to float
*p_temperature=(float)temperature; //converts integer to float
calc_sth11(p_humidity, p_temperature); //calculate humidity, temperature
}
}
//----------------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity, float *p_temperature)
{
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [°C]
t_C = D1 + D2*(*p_temperature);
if(t_C > 100) t_C = 100;
if(t_C < 1) t_C=1;
rh_lin = C3*(*p_humidity)*(*p_humidity) + C2*(*p_humidity) + C1;
rh_true = (t_C-25)*(T1+T2*(*p_humidity)) + rh_lin;
if(rh_true > 100) rh_true=100;
if(rh_true < 0.1) rh_true=0.1;
*p_temperature=t_C;
*p_humidity=rh_true;
}
void s_write_statusreg(unsigned char value)
{
comstart();
comwrite(STATUS_REG_W); //send command to sensor
comwrite(value); //send value of status register
}
unsigned char s_read_statusreg(unsigned char value, unsigned char checksum)
{
comstart();
comwrite(STATUS_REG_R);
value=comread(ACK); //read status register (8-bit)
checksum=comread(noACK); //read checksum (8-bit)
return value; //error=1 in case of no response form the sensor
}
Hi!
I'm using a PIC16F88 to interface with an SHT75, but code not work.
Could anybody help me!?!
void comstart(void)
{
trisa1=0; //DATA-line in output
ra0=0;
ra1=1;
#asm nop #endasm
ra0=1;
#asm nop #endasm
ra1=0;
#asm nop #endasm
ra0=0;
#asm nop #endasm
ra0=1;
#asm nop #endasm
ra1=1;
#asm nop #endasm
ra0=0;
}
void comreset(void)
{
unsigned char i;
trisa1=0; //DATA-line in output
ra1=1;
ra0=0;
for(i=0;i<9;i++) //9 clock cycles
{
ra0=1;
#asm nop #endasm
ra0=0;
}
comstart(); //transmission start
HumiError = FALSE;
}
void sht_soft_reset (void)
{
comreset(); //SHT75 communication reset
comwrite(RESET); //send SHT71 reset command
}
void sht_init (void)
{
sht_soft_reset(); //reset SHT71
// comreset(); //SHT71 communication reset
delay_ms(15); //delay for power-up
}
void comwait(void)
{
unsigned int16 sht_delay;
ra1=1;
ra0=0;
for(sht_delay=0;sht_delay<30000;sht_delay++)
{
if(!ra1)
{
//printf("\n\r-> SHT71 comwait error!\n\r");
break;
}
delay_us(10);
}
}
void comwrite(unsigned char ddata)
{
unsigned char i;
trisa1=0; //DATA-line in output
for(i=0;i<8;i++)
{
ra0=0;
if(ddata&0x80) ra1=1;
else ra1=0;
ra0=1;
ddata<<=1;
#asm nop #endasm
}
ra1=1; //release DATA-line (sua them)
trisa1=1; //DATA-line in input
#asm nop #endasm
ra1=0; //(old code)
ra0=0; //(old code)
ra0=1; //clock 9th for ACK
#asm nop #endasm
if(input(PIN_A1))
{
HumiError=TRUE; //check ack (DATA will be pulled down by SHT11)
while(HumiError)
{
rb3=1;
printf("\n\r-> SHT71 writed error...ACK!\n\r");
}
}
ra0=0;
}
unsigned int comread(unsigned char back)
{
unsigned char i;
unsigned int bbyte=0;
trisa1=1; //DATA-line in input
for(i=0;i<8;i++)
{
bbyte<<=1;
ra0=1;
bbyte|=input(PIN_A1);
ra0=0;
#asm nop #endasm
}
// ra1=!back; //"ack==1" pull down DATA-Line
trisa1=0; //DATA-line in output
ra1=!back; //"ack==1" pull down DATA-Line
ra0=1;
#asm nop #endasm
ra0=0;
#asm nop #endasm
trisa1=1; //DATA-line in input
ra1=1;
return bbyte;
}
unsigned int s_measure(unsigned char mode)
{
unsigned char msb=0,lsb=0,crc;
sht_soft_reset();
comstart();
switch(mode)
{
case TEMP:
comwrite(MEASURE_TEMP);
break;
case HUMI:
comwrite(MEASURE_HUMI);
break;
default:
break;
}
trisa1=1; //DATA-line in input
comwait(); //wait for the measurement to complete
msb = comread(ACK); //read the first byte (MSB)
lsb = comread(ACK); //read the second byte (LSB)
crc = comread(noACK); //read checksum
return (msb*256 + lsb);
}
void HumidityGet(float *p_humidity, float *p_temperature)
{
unsigned int humidity=0,temperature=0;
humidity = s_measure(HUMI); //measure humidity
DoAm=humidity;
temperature = s_measure(TEMP); //measure temperature
NhietDo=temperature;
if(HumiError == TRUE) comreset(); //in case of an error: Reset
else
{
*p_humidity=(float)humidity; //converts integer to float
*p_temperature=(float)temperature; //converts integer to float
calc_sth11(p_humidity, p_temperature); //calculate humidity, temperature
}
}
//----------------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity, float *p_temperature)
{
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [°C]
t_C = D1 + D2*(*p_temperature);
if(t_C > 100) t_C = 100;
if(t_C < 1) t_C=1;
rh_lin = C3*(*p_humidity)*(*p_humidity) + C2*(*p_humidity) + C1;
rh_true = (t_C-25)*(T1+T2*(*p_humidity)) + rh_lin;
if(rh_true > 100) rh_true=100;
if(rh_true < 0.1) rh_true=0.1;
*p_temperature=t_C;
*p_humidity=rh_true;
}
void s_write_statusreg(unsigned char value)
{
comstart();
comwrite(STATUS_REG_W); //send command to sensor
comwrite(value); //send value of status register
}
unsigned char s_read_statusreg(unsigned char value, unsigned char checksum)
{
comstart();
comwrite(STATUS_REG_R);
value=comread(ACK); //read status register (8-bit)
checksum=comread(noACK); //read checksum (8-bit)
return value; //error=1 in case of no response form the sensor
}