Autor Tema: Problema con SHT71. Falla driver en programa sencillo  (Leído 6190 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Nun

  • PIC10
  • *
  • Mensajes: 2
Problema con SHT71. Falla driver en programa sencillo
« en: 16 de Septiembre de 2009, 13:59:21 »
Hola a todos!

En primer lugar muchas gracias por la ayuda prestada aunque no la haya pedido por post. He navegado por el foro y entre los muchos y curiosos proyectos que he visto con el sht11, he cogido algunos drivers de este sensor y del sht75. Quiero programar en CSS, y he intentado modificar el codigo a las funciones que conozco pero peta igual, así que doy por sentado que el driver está bien y no es un problema de lenguaje (corregidme si me equivoco). El driver que os he tomado prestado es, en concreto, este:

Código: CSS
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                                                                           //
  3. // Driver file for SHT71 Temperature & Humidity Sensor                       //
  4. //                                                                           //
  5. // ***** To initialise SHT71 sensor upon power up *****                      //
  6. //                                                                           //
  7. // Function : sht_init()                                                     //
  8. // Return   : none                                                           //
  9. //                                                                           //
  10. //                                                                           //
  11. // ***** To measure and caluculate SHT71 temp & real RH *****                //
  12. //                                                                           //
  13. // Function : sht_rd (temp, truehumid)                                       //
  14. // Return   : temperature & true humidity in float values                    //
  15. //                                                                           //
  16. ///////////////////////////////////////////////////////////////////////////////
  17.  
  18. #define sht_data_pin   PIN_A1
  19. #define sht_clk_pin    PIN_A2
  20.  
  21.  
  22. //***** Function to alert SHT71 *****
  23.  
  24. void comstart (void)
  25. {
  26.  output_high(sht_data_pin);  //data high
  27.  output_low(sht_clk_pin);//clk low
  28.  delay_us(1);
  29.  output_high(sht_clk_pin);//clk high
  30.  delay_us(1);
  31.  output_low(sht_clk_pin);//data low
  32.  delay_us(1);
  33.  output_low(sht_clk_pin);//clk low
  34.  delay_us(2);
  35.  output_high(sht_clk_pin);//clk high
  36.  delay_us(1);
  37.  output_high(sht_clk_pin);//data high
  38.  delay_us(1);
  39.  output_low(sht_clk_pin);//clk low
  40. }
  41.  
  42.  
  43. //***** Function to write data to SHT71 *****
  44.  
  45. int1 comwrite (int8 iobyte)
  46. {
  47.  int8 i, mask = 0x80;
  48.  int1 ack;
  49.  
  50.  //Shift out command
  51.  delay_us(4);
  52.  for(i=0; i<8; i++)
  53.   {
  54.    output_bit(sht_clk_pin, 0);                          //clk low
  55.    if((iobyte & mask) > 0) output_float(sht_data_pin);  //data high if MSB high
  56.    else output_bit(sht_data_pin, 0);                    //data low if MSB low
  57.    delay_us(1);
  58.    output_bit(sht_clk_pin, 1);                          //clk high
  59.    delay_us(1);
  60.    mask = mask >> 1;                                    //shift to next bit
  61.   }
  62.  
  63.  //Shift in ack
  64.  output_bit(sht_clk_pin, 0);  //clk low
  65.  delay_us(1);
  66.  ack = input(sht_data_pin);   //get ack bit
  67.  output_bit(sht_clk_pin, 1);  //clk high
  68.  delay_us(1);
  69.  output_bit(sht_clk_pin, 0);  //clk low
  70.  return(ack);
  71. }
  72.  
  73.  
  74. //***** Function to read data from SHT71 *****
  75.  
  76. int16 comread (void)
  77. {
  78.  int8 i;
  79.  int16 iobyte = 0;
  80.  const int16 mask0 = 0x0000;
  81.  const int16 mask1 = 0x0001;
  82.  
  83.  //shift in MSB data
  84.  for(i=0; i<8; i++)
  85.   {
  86.    iobyte = iobyte << 1;
  87.    output_bit(sht_clk_pin, 1);                //clk high
  88.    delay_us(1);
  89.    if (input(sht_data_pin)) iobyte |= mask1;  //shift in data bit
  90.    else iobyte |= mask0;
  91.    output_bit(sht_clk_pin, 0);                //clk low
  92.    delay_us(1);
  93.   }
  94.  
  95.  //send ack 0 bit
  96.  output_bit(sht_data_pin, 0); //data low
  97.  delay_us(1);
  98.  output_bit(sht_clk_pin, 1);  //clk high
  99.  delay_us(2);
  100.  output_bit(sht_clk_pin, 0);  //clk low
  101.  delay_us(1);
  102.  output_float(sht_data_pin);  //data high
  103.  
  104.  //shift in LSB data
  105.  for(i=0; i<8; i++)
  106.   {
  107.    iobyte = iobyte << 1;
  108.    output_bit(sht_clk_pin, 1);                //clk high
  109.    delay_us(1);
  110.    if (input(sht_data_pin)) iobyte |= mask1;  //shift in data bit
  111.    else iobyte |= mask0;
  112.    output_bit(sht_clk_pin, 0);                //clk low
  113.    delay_us(1);
  114.   }
  115.  
  116.  //send ack 1 bit
  117.  output_float(sht_data_pin);  //data high
  118.  delay_us(1);
  119.  output_bit(sht_clk_pin, 1);  //clk high
  120.  delay_us(2);
  121.  output_bit(sht_clk_pin, 0);  //clk low
  122.  
  123.  return(iobyte);
  124. }
  125.  
  126.  
  127. //***** Function to wait for SHT71 reading *****
  128.  
  129. void comwait (void)
  130. {
  131.  int16 sht_delay;
  132.  
  133.  output_float(sht_data_pin);                     //data high
  134.  output_bit(sht_clk_pin, 0);                     //clk low
  135.  delay_us(1);
  136.  for(sht_delay=0; sht_delay<30000; sht_delay++)  // wait for max 300ms
  137.   {
  138.    if (!input(sht_data_pin)) break;              //if sht_data_pin low, SHT71 ready
  139.    delay_us(10);
  140.   }
  141. }
  142.  
  143.  
  144. //***** Function to reset SHT71 communication *****
  145.  
  146. void comreset (void)
  147. {
  148.  int8 i;
  149.  
  150.  output_float(sht_data_pin);    //data high
  151.  output_bit(sht_clk_pin, 0);    //clk low
  152.  delay_us(2);
  153.  for(i=0; i<9; i++)
  154.   {
  155.    output_bit(sht_clk_pin, 1);  //toggle clk 9 times
  156.    delay_us(2);
  157.    output_bit(sht_clk_pin, 0);
  158.    delay_us(2);
  159.  }
  160.  comstart();
  161. }
  162.  
  163.  
  164. //***** Function to soft reset SHT71 *****
  165.  
  166. void sht_soft_reset (void)
  167. {
  168.  comreset();           //SHT71 communication reset
  169.  comwrite(0x1e);       //send SHT71 reset command
  170.  delay_ms(15);         //pause 15 ms
  171. }
  172.  
  173.  
  174. //***** Function to measure SHT71 temperature *****
  175.  
  176. int16 measuretemp (void)
  177. {
  178.  int1 ack;
  179.  int16 iobyte;
  180.  
  181.  comstart();             //alert SHT71
  182.  ack = comwrite(0x03);   //send measure temp command and read ack status
  183.  if(ack == 1) return;
  184.  comwait();              //wait for SHT71 measurement to complete
  185.  iobyte = comread();     //read SHT71 temp data
  186.  return(iobyte);
  187. }
  188.  
  189.  
  190. //***** Function to measure SHT71 RH *****
  191.  
  192. int16 measurehumid (void)
  193. {
  194.  int1 ack;
  195.  int16 iobyte;
  196.  
  197.  comstart();            //alert SHT71
  198.  ack = comwrite(0x05);  //send measure RH command and read ack status
  199.  if(ack == 1) return;
  200.  comwait();             //wait for SHT71 measurement to complete
  201.  iobyte = comread();    //read SHT71 temp data
  202.  return(iobyte);
  203. }
  204.  
  205.  
  206. //***** Function to calculate SHT71 temp & RH *****
  207.  
  208. void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue)
  209. {
  210.  float truehumid1, rh;
  211.  
  212.  //calculate temperature reading
  213.  tc = ((float) temp * 0.01) - 40.0;
  214.  
  215.  //calculate Real RH reading
  216.  rh = (float) humid;
  217.  
  218.  rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0;
  219.  
  220.  //calculate True RH reading
  221.  rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin;
  222. }
  223.  
  224.  
  225. //***** Function to measure & calculate SHT71 temp & RH *****
  226.  
  227. void sht_rd (float & temp, float & truehumid)
  228. {
  229.  int16 restemp, reshumid;
  230.  float realhumid;
  231.  restemp = 0; truehumid = 0;
  232.  
  233.  restemp = measuretemp();    //measure temp
  234.  reshumid = measurehumid();  //measure RH
  235. calculate_data (restemp, reshumid, temp, realhumid, truehumid);  //calculate temp & RH
  236. }
  237.  
  238.  
  239. //***** Function to initialise SHT71 on power-up *****
  240.  
  241. void sht_init (void)
  242. {
  243.  comreset();    //reset SHT71
  244.  delay_ms(20);  //delay for power-up
  245. }
No se porque el foro toma que a partir del comentario para iniciar el sensor es comentario todo, pero en mi compilador no lo és.

El codigo principal es el siguiente:

Código: CSS
  1. #include <18f458.h>
  2. #include <sht71.C>
  3. #device ICD=TRUE
  4. #fuses HS,NOLVP,NOWDT,PUT
  5. #use delay(clock=20000000)
  6. #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
  7.  
  8. void main (){
  9. float temp, truehumid;
  10.  
  11. sth_init();
  12. sht_rd (temp, truehumid);
  13.  
  14. printf ("La temp es %fl y la humedad es %fl",TEMP,TRUEHUMID);
  15.  
  16. }

En concreto peta el primer delay en la función comstart con el error undefined function. Me huele a error mio, pero no lo encuentro.

Muchas gracias por adelantado si me respondeis al post y también por la que os he tomado prestada ^^
Un beso
« Última modificación: 16 de Septiembre de 2009, 14:03:56 por Nun »

Desconectado septiembre_negro

  • PIC18
  • ****
  • Mensajes: 310
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #1 en: 16 de Septiembre de 2009, 17:09:25 »
Hola
A reserva que alguien con más experiencia te comente  te cuento  lo que me paso
Recién comienzo en esto del ccs y estuve asiendo pruebas con un rtc ds1307 al igual que tu no podía  leer el  rtc considerando que el error era mío  modifique mi código varias veces  asta que se me ocurrió probar con una librería diferente.
De igual manera sucedió con un max7219 solo que esta ves con lo que me había sucedido probé enseguida con otra librería y funciono a la primera en conclusión hay librerías que no funcionan.

Haaaaa y si eres niño te cambio el beso por un saludo de mano   :mrgreen: :D :D :D :D


Desconectado Nun

  • PIC10
  • *
  • Mensajes: 2
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #2 en: 17 de Septiembre de 2009, 13:57:41 »
xDDD

Pos asi a la espera intentare hacerme yo mi libreria para el sensor (alea iacta est q se dice xDDD)

Lo unico que puede pasar es que no funcione, pero de perdidos al rio xD

Y tranki q no hace falta cambiar beso por saludo de mano >.<

Desconectado MLO__

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 4581
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #3 en: 17 de Septiembre de 2009, 15:12:14 »
Hola.

Pues, esta es una librería funcional (la he probado y me funciona) para el SHT11:

Código: C (Mac)
  1. #define DATOS  PIN_A4
  2. #define CLOCK  PIN_A5
  3.  
  4. #define SHT11_noACK 0
  5. #define SHT11_ACK   1
  6.  
  7. // SHT11 commands
  8. #define SHT11_STATUS_REG_W 0x06
  9. #define SHT11_STATUS_REG_R 0x07
  10. #define SHT11_MEASURE_TEMP 0x03  
  11. #define SHT11_MEASURE_HUMI 0x05  
  12. #define SHT11_RESET        0x1E  
  13.  
  14. enum {TEMP,HUMI};
  15.  
  16.  
  17. byte sht11_escribir_byte(byte value)
  18. {
  19.         byte i,error=0;
  20.  
  21.         for (i=128;i>0;i/=2)                
  22.    {
  23.                 if (i & value)  output_high(DATOS);  
  24.                 else output_low(DATOS);
  25.                 output_high(CLOCK);              
  26.                 delay_us(5);                    
  27.                 output_low(CLOCK);
  28.    }
  29.         output_high(DATOS);                  
  30.         output_high(CLOCK);                  
  31.         error=input(DATOS);                  
  32.         output_low(CLOCK);
  33.         return error;                        
  34. }
  35.  
  36.  
  37. byte sht11_leer_byte(byte ack)
  38. {
  39.         byte i,val=0;
  40.  
  41.         output_high(DATOS);          
  42.         for (i=128;i>0;i/=2)          
  43.    {
  44.                 output_high(CLOCK);              
  45.                 if (input(DATOS))      
  46.                   val=(val | i);  
  47.       output_low(CLOCK);
  48.         }
  49.    if (ack) output_low(DATOS);      
  50.         else output_float(DATOS);
  51.         output_high(CLOCK);            
  52.         delay_us(5);                    
  53.         output_low(CLOCK);
  54.         output_high(DATOS);            
  55.         return val;
  56. }
  57.  
  58.  
  59. void sht11_init(void)
  60. {
  61.         output_high(DATOS);
  62.         output_low(CLOCK);            
  63.         delay_us(2);
  64.         output_high(CLOCK);
  65.         delay_us(2);
  66.         output_low(DATOS);
  67.         delay_us(2);
  68.         output_low(CLOCK);
  69.         delay_us(5);
  70.         output_high(CLOCK);
  71.         delay_us(2);
  72.         output_float(DATOS);
  73.         delay_us(2);
  74.         output_low(CLOCK);
  75. }
  76.  
  77.  
  78. void sht11_hard_reset(void)
  79. {
  80.         byte i;
  81.  
  82.         output_high(DATOS);
  83.         output_low(CLOCK);          
  84.         for(i=0;i<9;i++)            
  85.    {
  86.                 output_high(CLOCK);
  87.                 delay_us(2);
  88.                 output_low(CLOCK);
  89.                 delay_us(2);
  90.         }
  91.         sht11_init();            
  92. }
  93.  
  94. byte sht11_soft_reset(void)
  95. {
  96.         byte error=0;
  97.  
  98.         sht11_hard_reset();                                  
  99.         error+=sht11_escribir_byte(SHT11_RESET);          
  100.         return error;                              
  101. }
  102.  
  103.  
  104. byte sht11_leer_registro_estado(byte *p_valor, byte *p_checksum)
  105. {
  106.         byte error=0;
  107.  
  108.         sht11_init();                          
  109.         error = sht11_escribir_byte(SHT11_STATUS_REG_R);  
  110.         *p_valor = sht11_leer_byte(SHT11_ACK);          
  111.         *p_checksum = sht11_leer_byte(SHT11_NOACK);      
  112.         return error;                              
  113. }
  114.  
  115.  
  116. byte sht11_escribir_registro_estado(byte *p_valor)
  117. {
  118.         byte error=0;
  119.  
  120.         sht11_init();                          
  121.         error += sht11_escribir_byte(SHT11_STATUS_REG_W);
  122.         error += sht11_escribir_byte(*p_valor);          
  123.         return error;                            
  124. }
  125.  
  126.  
  127. byte sht11_medicion(byte *p_valor, byte *p_checksum, byte modo)
  128. {
  129.         byte error=0;
  130.         int16 i;
  131.  
  132.         sht11_init();
  133.         switch(modo)                            
  134.    {
  135.                 case TEMP       : error+=sht11_escribir_byte(SHT11_MEASURE_TEMP); break;
  136.                 case HUMI       : error+=sht11_escribir_byte(SHT11_MEASURE_HUMI); break;
  137.                 default     : break;
  138.         }
  139.         for (i=0;i<65535;i++) if(input(DATOS)==0) break;  
  140.         if(input(DATOS)) error+=1;              
  141.         *(p_valor+1)  =sht11_leer_byte(SHT11_ACK);    
  142.         *(p_valor)    =sht11_leer_byte(SHT11_ACK);    
  143.         *(p_checksum) =sht11_leer_byte(SHT11_NOACK);  
  144.  
  145.         return error;
  146. }
  147.  
  148.  
  149. void sht11_calculos(float *p_humedad, float *p_temperatura)
  150. {
  151.  
  152.         const float C1=-4.0;                                   
  153.         const float C2=+0.0405;                        
  154.         const float C3=-0.0000028;                     
  155.         const float T1=+0.01;                          
  156.         const float T2=+0.00008;                       
  157.  
  158.         float rh;
  159.         float t;
  160.         float rh_lin;                                                  
  161.         float rh_true;                                                 
  162.         float t_C;                                                             
  163.         rh = *p_humedad;                                               
  164.         t = *p_temperatura;                                    
  165.  
  166.         t_C = t*0.01 - 40;                                     
  167.         rh_lin=C3*rh*rh + C2*rh + C1;          
  168.         rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;    
  169.         if(rh_true>100)rh_true=100;            
  170.         if(rh_true<0.1)rh_true=0.1;            
  171.  
  172.         *p_temperatura=t_C;                                    
  173.         *p_humedad=rh_true;                                    
  174. }

Saludos
El papel lo aguanta todo

Desconectado juanpavz

  • PIC12
  • **
  • Mensajes: 77
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #4 en: 17 de Septiembre de 2009, 17:57:45 »
Hola MLO podrías subir un ejemplo básico, yo tampoco puedo hacer andar el sensor
Hazlo o no lo hagas... Pero no lo intentes

Desconectado MLO__

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 4581
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #5 en: 17 de Septiembre de 2009, 20:34:52 »
Ok.

Código: C#
  1. #include <18F452.h>
  2. #fuses  XT, NOPROTECT, BROWNOUT, BORV45, NOPUT, NOCPD, NOLVP, NOWDT
  3. #use delay(clock=4000000)
  4.  
  5. #include "lcd420.h"
  6. #include "sht11.h"
  7.  
  8. void lectura_sht11(void);
  9. void mostrar_temp_hum(void);
  10.  
  11. unsigned int8 temp_actual, humedad_actual;
  12.  
  13. typedef union
  14. {
  15.    int16 i;
  16.    float f;
  17. } valor;
  18. valor humedad, temperatura;
  19. byte errorsht11,checksum;
  20.  
  21.  
  22. void main()
  23. {
  24.    lcd_init();
  25.    sht11_hard_reset();
  26.  
  27.    while(true)
  28.    {
  29.       lectura_sht11();
  30.       mostrar_temp_hum();
  31.       delay_ms(300);
  32.    }
  33. }
  34.  
  35.  
  36. void lectura_sht11(void)
  37. {
  38.    errorsht11=0;
  39.         errorsht11+=sht11_medicion((byte*) &humedad.i, &checksum, HUMI);     //measure humidity
  40.         errorsht11+=sht11_medicion((byte*) &temperatura.i, &checksum, TEMP); //measure temperature
  41.         if(errorsht11!=0)                                                    //in case of an error: connection reset
  42.    {
  43.       printf(lcd_putc,"\n\rerror:%U", errorsht11);
  44.                 sht11_hard_reset();
  45.         }
  46.         else
  47.    {
  48.       humedad.f=(float)humedad.i;                              //converts integer to float
  49.            temperatura.f=(float)temperatura.i;                      //converts integer to float
  50.            sht11_calculos(&humedad.f, &temperatura.f);              //calculate humidity, temperature
  51.       temp_actual = temperatura.f;
  52.       humedad_actual = humedad.f;
  53.         }
  54. }
  55.  
  56.  
  57. void mostrar_temp_hum(void)
  58. {
  59.    lcd_gotoxy(1,1);
  60.    printf(lcd_putc,"T:%1.1f C ",temperatura.f);
  61.    lcd_gotoxy(1,2);
  62.    printf(lcd_putc,"H:%1.1f %%HR ",humedad.f);
  63. }

Saludos
El papel lo aguanta todo

Desconectado juanpavz

  • PIC12
  • **
  • Mensajes: 77
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #6 en: 17 de Septiembre de 2009, 21:12:31 »
Gracias por el código pero sigo sin poder simularlo :(,
Estoy usando proteus 7.5 subo un pantallazo y el código y simulación
Ojala me puedas ayudar
PSDT: El primer codigo que posteaste era el sht11.h?
Hazlo o no lo hagas... Pero no lo intentes

Desconectado MLO__

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 4581
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #7 en: 17 de Septiembre de 2009, 21:20:35 »
Hola.

Si, el archivo SHT11.h es el que esta posteado anteriormente. Ojo con la libreria de la LCD, cambiala con alguna otra. Cual es el problema propiamente?
El papel lo aguanta todo

Desconectado juanpavz

  • PIC12
  • **
  • Mensajes: 77
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #8 en: 18 de Septiembre de 2009, 01:30:13 »
Pues cambie cambie la libreria, y el problema es que no aparece nada en el LCD
Hazlo o no lo hagas... Pero no lo intentes

Desconectado MLO__

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 4581
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #9 en: 18 de Septiembre de 2009, 02:07:47 »
Adjunto la libreria de la LCD.

Código: C#
  1. // As defined in the following structure the pin connection is as follows:
  2. //     C0  enable
  3. //     C1  rs
  4. //     C2  rw
  5. //     C4  D4
  6. //     C5  D5
  7. //     C6  D6
  8. //     C7  D7
  9. //
  10. //   LCD pins D0-D3 are not used and PIC C3 is not used.
  11.  
  12. struct lcd_pin_map {                 // This structure is overlayed
  13.            BOOLEAN enable;           // on to an I/O port to gain
  14.            BOOLEAN rs;               // access to the LCD pins.
  15.            BOOLEAN rw;               // The bits are allocated from
  16.            BOOLEAN unused;           // low order up.  ENABLE will
  17.            int     data : 4;         // be pin B0.
  18.         } lcd;
  19.  
  20. #byte lcd = 0xF82                        // This puts the entire structure
  21.                                      // on to port C (at address 0xF82)
  22.  
  23. #define lcd_type 2           // 0=5x7, 1=5x10, 2=2 lines
  24.  
  25.  
  26. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  27.                              // These bytes need to be sent to the LCD
  28.                              // to start it up.
  29.  
  30.  
  31.                              // The following are used for setting
  32.                              // the I/O port direction register.
  33.  
  34. struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
  35. struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in
  36.  
  37. BYTE lcdline;
  38.  
  39. BYTE lcd_read_byte() {
  40.       BYTE low,high;
  41.  
  42.       set_tris_c(LCD_READ);
  43.       lcd.rw = 1;
  44.       delay_cycles(1);
  45.       lcd.enable = 1;
  46.       delay_cycles(1);
  47.       high = lcd.data;
  48.       lcd.enable = 0;
  49.       delay_cycles(1);
  50.       lcd.enable = 1;
  51.       delay_us(1);
  52.       low = lcd.data;
  53.       lcd.enable = 0;
  54.       set_tris_c(LCD_WRITE);
  55.       return( (high<<4) | low);
  56. }
  57.  
  58.  
  59. void lcd_send_nibble( BYTE n ) {
  60.       lcd.data = n;
  61.       delay_cycles(1);
  62.       lcd.enable = 1;
  63.       delay_us(2);
  64.       lcd.enable = 0;
  65. }
  66.  
  67.  
  68. void lcd_send_byte( BYTE address, BYTE n ) {
  69.  
  70.       lcd.rs = 0;
  71.       while ( bit_test(lcd_read_byte(),7) ) ;
  72.       lcd.rs = address;
  73.       delay_cycles(1);
  74.       lcd.rw = 0;
  75.       delay_cycles(1);
  76.       lcd.enable = 0;
  77.       lcd_send_nibble(n >> 4);
  78.       lcd_send_nibble(n & 0xf);
  79. }
  80.  
  81.  
  82. void lcd_init() {
  83.     BYTE i;
  84.  
  85.     set_tris_c(LCD_WRITE);
  86.     lcd.rs = 0;
  87.     lcd.rw = 0;
  88.     lcd.enable = 0;
  89.     delay_ms(15);
  90.     for(i=1;i<=3;++i) {
  91.        lcd_send_nibble(3);
  92.        delay_ms(5);
  93.     }
  94.     lcd_send_nibble(2);
  95.     for(i=0;i<=3;++i)
  96.        lcd_send_byte(0, LCD_INIT_STRING[i]);
  97. }
  98.  
  99.  
  100. void lcd_gotoxy( BYTE x, BYTE y) {
  101.    BYTE address;
  102.  
  103.    switch(y) {
  104.      case 1 : address=0x80;break;
  105.      case 2 : address=0xc0;break;
  106.      case 3 : address=0x94;break;
  107.      case 4 : address=0xd4;break;
  108.    }
  109.    address+=x-1;
  110.    lcd_send_byte(0,address);
  111. }
  112.  
  113. void lcd_putc( char c) {
  114.    switch (c) {
  115.      case '\f'   : lcd_send_byte(0,1);
  116.                    lcdline=1;
  117.                    delay_ms(2);
  118.                                            break;
  119.      case '\n'   : lcd_gotoxy(1,++lcdline);        break;
  120.      case '\b'   : lcd_send_byte(0,0x10);  break;
  121.      default     : lcd_send_byte(1,c);     break;
  122.    }
  123. }
  124.  
  125. char lcd_getc( BYTE x, BYTE y) {
  126.    char value;
  127.  
  128.     lcd_gotoxy(x,y);
  129.     lcd.rs=1;
  130.     value = lcd_read_byte();
  131.     lcd.rs=0;
  132.     return(value);
  133. }

Saludos
El papel lo aguanta todo

Desconectado PUMAFIRES

  • PIC10
  • *
  • Mensajes: 1
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #10 en: 02 de Febrero de 2011, 19:22:53 »




YO TENGO UN PEQUÑO PROBLEMA NECESITO LA LIBRERIA DEL SHT71 PERO EN ASEMBLER ES DECIR .INC PARA UN PIC 16F84A O UN 16F877 POR FA SI ALGUEIN ME PUDIERA AYUDAR




Desconectado blackmar

  • PIC10
  • *
  • Mensajes: 1
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #11 en: 21 de Marzo de 2011, 16:24:52 »
acabo de resolver el problema con el sht71, resulta q en la librería q anda dando vueltas tuve q reescribir la funcion de la siguiente forma: esta anda !!!


Código: CSS
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                                                                           //
  3. // Driver file for SHT71 Temperature & Humidity Sensor                       //
  4. //                                                                           //
  5. // ***** To initialise SHT71 sensor upon power up *****                      //
  6. //                                                                           //
  7. // Function : sht_init()                                                     //
  8. // Return   : none                                                           //
  9. //                                                                           //
  10. //                                                                           //
  11. // ***** To measure and caluculate SHT71 temp & real RH *****                //
  12. //                                                                           //
  13. // Function : sht_rd (temp, truehumid)                                       //
  14. // Return   : temperature & true humidity in float values                    //
  15. //                                                                           //
  16. ///////////////////////////////////////////////////////////////////////////////
  17.  
  18. #define sht_data_pin   PIN_C0
  19. #define sht_clk_pin    PIN_C1
  20.  
  21.  
  22. //***** Function to alert SHT71 *****
  23.  
  24. void comstart (void)
  25. {
  26.  output_float(sht_data_pin);  //data high
  27.  output_bit(sht_clk_pin, 0);  //clk low
  28.  delay_us(1);
  29.  output_bit(sht_clk_pin, 1);  //clk high
  30.  delay_us(1);
  31.  output_bit(sht_data_pin, 0); //data low
  32.  delay_us(1);
  33.  output_bit(sht_clk_pin, 0);  //clk low
  34.  delay_us(2);
  35.  output_bit(sht_clk_pin, 1);  //clk high
  36.  delay_us(1);
  37.  output_float(sht_data_pin);  //data high
  38.  delay_us(1);
  39.  output_bit(sht_clk_pin, 0);  //clk low
  40. }
  41.  
  42.  
  43.  
  44. //***** Function to write data to SHT71 *****
  45.  
  46. int1 comwrite (int8 iobyte)
  47. {
  48.  int8 i, mask = 0x80;
  49.  int1 ack;
  50.  
  51.  //Shift out command
  52.  delay_us(4);
  53.  for(i=0; i<8; i++)
  54.   {
  55.    output_bit(sht_clk_pin, 0);                          //clk low
  56.    if((iobyte & mask) > 0) output_float(sht_data_pin);  //data high if MSB high
  57.    else output_bit(sht_data_pin, 0);                    //data low if MSB low
  58.    delay_us(1);
  59.    output_bit(sht_clk_pin, 1);                          //clk high
  60.    delay_us(1);
  61.    mask = mask >> 1;                                    //shift to next bit
  62.   }
  63.  
  64.  //Shift in ack
  65.  output_bit(sht_clk_pin, 0);  //clk low
  66.  delay_us(1);
  67.  ack = input(sht_data_pin);   //get ack bit
  68.  output_bit(sht_clk_pin, 1);  //clk high
  69.  delay_us(1);
  70.  output_bit(sht_clk_pin, 0);  //clk low
  71.  return(ack);
  72. }
  73.  
  74.  
  75. //***** Function to read data from SHT71 *****
  76.  
  77. int16 comread (void)
  78. {
  79.  int8 i;
  80.  int16 iobyte = 0;
  81.  const int16 mask0 = 0x0000;
  82.  const int16 mask1 = 0x0001;
  83.  
  84.  //shift in MSB data
  85.  for(i=0; i<8; i++)
  86.   {
  87.    iobyte = iobyte << 1;
  88.    output_bit(sht_clk_pin, 1);                //clk high
  89.    delay_us(1);
  90.    if (input(sht_data_pin)) iobyte |= mask1;  //shift in data bit
  91.    else iobyte |= mask0;
  92.    output_bit(sht_clk_pin, 0);                //clk low
  93.    delay_us(1);
  94.   }
  95.  
  96.  //send ack 0 bit
  97.  output_bit(sht_data_pin, 0); //data low
  98.  delay_us(1);
  99.  output_bit(sht_clk_pin, 1);  //clk high
  100.  delay_us(2);
  101.  output_bit(sht_clk_pin, 0);  //clk low
  102.  delay_us(1);
  103.  output_float(sht_data_pin);  //data high
  104.  
  105.  //shift in LSB data
  106.  for(i=0; i<8; i++)
  107.   {
  108.    iobyte = iobyte << 1;
  109.    output_bit(sht_clk_pin, 1);                //clk high
  110.    delay_us(1);
  111.    if (input(sht_data_pin)) iobyte |= mask1;  //shift in data bit
  112.    else iobyte |= mask0;
  113.    output_bit(sht_clk_pin, 0);                //clk low
  114.    delay_us(1);
  115.   }
  116.  
  117.  //send ack 1 bit
  118.  output_float(sht_data_pin);  //data high
  119.  delay_us(1);
  120.  output_bit(sht_clk_pin, 1);  //clk high
  121.  delay_us(2);
  122.  output_bit(sht_clk_pin, 0);  //clk low
  123.  
  124.  return(iobyte);
  125. }
  126.  
  127.  
  128. //***** Function to wait for SHT71 reading *****
  129.  
  130. void comwait (void)
  131. {
  132.  int16 sht_delay;
  133.  
  134.  output_float(sht_data_pin);                     //data high
  135.  output_bit(sht_clk_pin, 0);                     //clk low
  136.  delay_us(1);
  137.  for(sht_delay=0; sht_delay<30000; sht_delay++)  // wait for max 300ms
  138.   {
  139.    if (!input(sht_data_pin)) break;              //if sht_data_pin low, SHT71 ready
  140.    delay_us(10);
  141.   }
  142. }
  143.  
  144.  
  145. //***** Function to reset SHT71 communication *****
  146.  
  147. void comreset (void)
  148. {
  149.  int8 i;
  150.  
  151.  output_float(sht_data_pin);    //data high
  152.  output_bit(sht_clk_pin, 0);    //clk low
  153.  delay_us(2);
  154.  for(i=0; i<9; i++)
  155.   {
  156.    output_bit(sht_clk_pin, 1);  //toggle clk 9 times
  157.    delay_us(2);
  158.    output_bit(sht_clk_pin, 0);
  159.    delay_us(2);
  160.  }
  161.  comstart();
  162. }
  163.  
  164.  
  165. //***** Function to soft reset SHT71 *****
  166.  
  167. void sht_soft_reset (void)
  168. {
  169.  comreset();           //SHT71 communication reset
  170.  comwrite(0x1e);       //send SHT71 reset command
  171.  delay_ms(15);         //pause 15 ms
  172. }
  173.  
  174.  
  175. //***** Function to measure SHT71 temperature *****
  176.  
  177. int16 measuretemp (void)
  178. {
  179.  int1 ack;
  180.  int16 iobyte;
  181.  
  182.  comstart();             //alert SHT71
  183.  ack = comwrite(0x03);   //send measure temp command and read ack status
  184.  if(ack == 1) return;
  185.  comwait();              //wait for SHT71 measurement to complete
  186.  iobyte = comread();     //read SHT71 temp data
  187.  return(iobyte);
  188. }
  189.  
  190.  
  191. //***** Function to measure SHT71 RH *****
  192.  
  193. int16 measurehumid (void)
  194. {
  195.  int1 ack;
  196.  int16 iobyte;
  197.  
  198.  comstart();            //alert SHT71
  199.  ack = comwrite(0x05);  //send measure RH command and read ack status
  200.  if(ack == 1) return;
  201.  comwait();             //wait for SHT71 measurement to complete
  202.  iobyte = comread();    //read SHT71 temp data
  203.  return(iobyte);
  204. }
  205.  
  206.  
  207. //***** Function to calculate SHT71 temp & RH *****
  208.  
  209. void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue)
  210. {
  211.  float truehumid1, rh;
  212.  
  213.  //calculate temperature reading
  214.  tc = ((float) temp * 0.01) - 40.0;
  215.  
  216.  //calculate Real RH reading
  217.  rh = (float) humid;
  218.  
  219.  rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0;
  220.  
  221.  //calculate True RH reading
  222.  rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin;
  223. }
  224.  
  225.  
  226. //***** Function to measure & calculate SHT71 temp & RH *****
  227.  
  228. void sht_rd (float & temp, float & truehumid)
  229. {
  230.  int16 restemp, reshumid;
  231.  float realhumid;
  232.  restemp = 0; truehumid = 0;
  233.  
  234.  restemp = measuretemp();    //measure temp
  235.  reshumid = measurehumid();  //measure RH
  236. calculate_data (restemp, reshumid, temp, realhumid, truehumid);  //calculate temp & RH
  237. }
  238.  
  239.  
  240. //***** Function to initialise SHT71 on power-up *****
  241.  
  242. void sht_init (void)
  243. {
  244.  comreset();    //reset SHT71
  245.  delay_ms(20);  //delay for power-up
  246. }

Desconectado fponcy

  • PIC10
  • *
  • Mensajes: 1
Re: Problema con SHT71. Falla driver en programa sencillo
« Respuesta #12 en: 08 de Mayo de 2011, 20:54:01 »
Hola, oye he probado tu codigo, tanto del driver como del programa q hace uso de el pero la humeda q me arroja es de -1 y la temperatura de -40 C algo asi, estoy trabajando con el pic 18f4550...He leido por ahi q muchos otros tienen un problema parecido...No sabes porq pasa esta situacion???