Que hay estimados programadores, estuve tratando de acoplar el codigo de ejemplo al CCS, pero no tuve excito, asi es que me decidi por leer el datasheet y ya encaminado lo empece todo, espero les sea de utilidad,, ojo, la humedad es la lineal y no la compensada (se explica en el datasheet), eso les afectara en temperaturas muy diferentes a 25°C, para mi proposito no importaba, imprimo en el serial el temp, humedad y punto de rocio, CRC no lo utilice aunque lo capture en una variable. Cualquier comentario o sugerencia es buena
Codigo:
/////////////////////////////////////////////////////////////////////////////////////////////////
#include <18f2620.h>
#use delay(internal=8M)
#fuses NOMCLR
#use rs232(baud=9600, xmit=PIN_b7)
#include <math.h>
#define nop delay_us(1)
#define STH11_SCK pin_a0
#define STH11_DATA pin_a1
#define STH11_SCK_1 output_high(STH11_SCK)
#define STH11_SCK_0 output_low(STH11_SCK)
#define STH11_DATA_1 output_high(STH11_DATA)
#define STH11_DATA_0 output_low(STH11_DATA)
#define temperatura 0b00000011
#define humedad 0b00000101
void inicio_transferencia_STH11(void)
{
STH11_SCK_0;
delay_us(2);
STH11_SCK_1;
delay_us(2);
STH11_DATA_0;
delay_us(2);
STH11_SCK_0;
delay_us(2);
STH11_SCK_1;
delay_us(2);
STH11_DATA_1;
delay_us(2);
STH11_SCK_0;
}
void SCK_pulso(int esperar)
{
STH11_SCK_1;
delay_us(esperar);
STH11_SCK_0;
delay_us(esperar);
}
float leer_STH11(int modo) // comandos temperatura o humedad
{
long valor_medido;
float valor_final;
int x, registro_h, registro_l, CRC;
inicio_transferencia_STH11();
STH11_DATA_0;
delay_us(1);
registro_h=modo;//modo sera utilizado al final de la funcion
for(x=8;x>=1;x--)
{
if(shift_left(®istro_h,1,0))
STH11_DATA_1;
else
STH11_DATA_0;
STH11_SCK_1;
delay_us(2);
if(x==1)
output_float(STH11_DATA);//detectar ACK y data como entrada
STH11_SCK_0;
delay_us(2);
}
SCK_pulso(2);
while(input(STH11_DATA)); //si falla el sensor se estancaria aqui. <-- OJO
//////empieza a lectura de datos 14 bits
SCK_pulso(2);
for(x=7;x>=1;x--)
{
shift_left(®istro_h,1,input(STH11_DATA));
SCK_pulso(2);
}
STH11_DATA_0;//pulso ACK
SCK_pulso(2);
output_float(STH11_DATA);
delay_us(2);
for(x=8;x>=1;x--)
{
shift_left(®istro_l,1,input(STH11_DATA));
SCK_pulso(2);
}
STH11_DATA_0;//pulso ACK
SCK_pulso(2);
output_float(STH11_DATA);
delay_us(2);
//capturar el CRC
for(x=8;x>=1;x--)
{
shift_left(&CRC,1,input(STH11_DATA));
SCK_pulso(2);
}
SCK_pulso(2);
valor_medido=make16(registro_h,registro_l);
switch(modo)
{
case temperatura:
valor_final= 0.01*(float)valor_medido-40.1;
break;
case humedad: //valor sin compensacion de temperatura
valor_final= -2.0468 + 0.0367*valor_medido - 0.0000015955*valor_medido*valor_medido;
}
return valor_final;
}
/*
float STH11_humdad_compensada( ) //sacar la variable valor medido en la funcion anterior como global para hacer funcionar esta funcion.
{
float temp, hum, humedad_calibrada;
temp = leer_STH11(temperatura);
hum= leer_STH11(humedad);
humedad_calibrada=(temp - 25)*(0.01 + 0.00008* valor_medido )+hum;
return humedad_calibrada;
}*/
float STH11_punto_rocio()
{
float temp, hum, punto_rocio,m,Tn;
temp = leer_STH11(temperatura);
hum= leer_STH11(humedad);
if(temp>=0)
{
m=17.62;
Tn=243.12;
}
else //temperaturas bajo cero
{
m=22.46;
Tn=272.62;
}
punto_rocio= Tn*((LOG (hum/100)+m*temp/(Tn+temp))/(m-LOG(hum/100)-m*temp/(Tn+temp)));
return punto_rocio;
}
main()
{
while(1)
{
printf("\r\nTemp:%f Humedad:%f ",leer_STH11(temperatura),leer_STH11(humedad));
printf("punto rocio:%f ",STH11_punto_rocio( ));
delay_ms(1000);
}
}