Autor Tema: Problema con SHT 75 y pic 18f4410  (Leído 2423 veces)

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

Desconectado iNoXSteeL

  • PIC12
  • **
  • Mensajes: 75
    • La web de InoX
Problema con SHT 75 y pic 18f4410
« en: 01 de Octubre de 2009, 16:19:49 »
Hola a todos,

Estoy utilizando el sensor sht 75 con el 18f4410, pero me da números erróneos, mientras que usándolo tal como está con un 16f877, funciona perfectamente.

El problema ha sido al cambiar de pic, ya que el 16f877 se me quedó escaso de memoria para el programa y he tenido que irme al 18f4410.

Puede ser problema de la librería ?? ... porque he tenido problema también con la lcd420 y lo he solucionado cambiando la dirección del puerto b, de 0x06 a 3969 que es la del puerto b del 18..

Gracias de antemano,

Un saludo.

Desconectado migsantiago

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8257
    • Sitio de MigSantiago
Re: Problema con SHT 75 y pic 18f4410
« Respuesta #1 en: 01 de Octubre de 2009, 16:36:18 »
Hola, talvez sea la librería o la velocidad del link serial, ¿cuál librería estás usando?

Desconectado iNoXSteeL

  • PIC12
  • **
  • Mensajes: 75
    • La web de InoX
Re: Problema con SHT 75 y pic 18f4410
« Respuesta #2 en: 01 de Octubre de 2009, 16:43:38 »
Hola Migsantiago,

La librería es esta:

Código: C
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                                                                           //
  3. // Driver file for SHT75 Temperature & Humidity Sensor                       //
  4. //                                                                           //
  5. // ***** To initialise SHT75 sensor upon power up *****                      //
  6. //                                                                           //
  7. // Function : sht_init()                                                     //
  8. // Return   : none                                                           //
  9. //                                                                           //
  10. //                                                                           //
  11. // ***** To measure and calculate SHT75 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_E1
  19. #define sht_clk_pin    PIN_E0
  20.  
  21.  
  22. //***** Function to alert SHT75 *****
  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. //***** Function to write data to SHT75 *****
  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 SHT75 *****
  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 SHT75 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, SHT75 ready
  139.    delay_us(10);
  140.   }
  141. }
  142.  
  143.  
  144. //***** Function to reset SHT75 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 SHT75 *****
  165.  
  166. void sht_soft_reset (void)
  167. {
  168.  comreset();           //SHT75 communication reset
  169.  comwrite(0x1e);       //send SHT75 reset command
  170.  delay_ms(15);         //pause 15 ms
  171. }
  172.  
  173.  
  174. //***** Function to measure SHT75 temperature *****
  175.  
  176. int16 measuretemp ()
  177. {
  178.  int1 ack;
  179.  int16 iobyte;
  180.  
  181.  comstart();             //alert SHT75
  182.  ack = comwrite(0x03);   //send measure temp command and read ack status
  183.  if(ack == 1) return;
  184.  comwait();              //wait for SHT75 measurement to complete
  185.  iobyte = comread();     //read SHT75 temp data
  186.  return(iobyte);
  187. }
  188.  
  189.  
  190. //***** Function to measure SHT75 RH *****
  191.  
  192. int16 measurehumid (void)
  193. {
  194.  int1 ack;
  195.  int16 iobyte;
  196.  
  197.  comstart();            //alert SHT75
  198.  ack = comwrite(0x05);  //send measure RH command and read ack status
  199.  if(ack == 1) return;
  200.  comwait();             //wait for SHT75 measurement to complete
  201.  iobyte = comread();    //read SHT75 temp data
  202.  return(iobyte);
  203. }
  204.  
  205.  
  206. //***** Function to calculate SHT75 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 SHT75 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 SHT75 on power-up *****
  240.  
  241. void sht_init (void)
  242. {
  243.  comreset();    //reset SHT75
  244.  delay_ms(20);  //delay for power-up
  245. }

No se si la encontré por aquí o en alguna otra web..

Un saludo

Desconectado MLO__

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 4581
Re: Problema con SHT 75 y pic 18f4410
« Respuesta #3 en: 01 de Octubre de 2009, 16:55:39 »
Hola.

Revisa AQUI, de pronto te sirve.

Saludos
El papel lo aguanta todo

Desconectado iNoXSteeL

  • PIC12
  • **
  • Mensajes: 75
    • La web de InoX
Re: Problema con SHT 75 y pic 18f4410
« Respuesta #4 en: 02 de Octubre de 2009, 17:08:25 »
MLO, gracias por la aclaración, he utilizado el driver que propones para el sht11, pero sigue sin funcionar...

Código: C
  1. #include <18f4410.h>
  2. #device adc=10
  3. #use delay(clock=20000000)
  4. #include <sht11.h>
  5. #fuses HS,XT,NOWDT,NOPUT,NOLVP,BROWNOUT,NOWRT,NOPROTECT,NOMCLR
  6. #include <lcd420.c>
  7. #use fast_io(a)
  8. #use fast_io(d)
  9.  
  10. typedef union
  11. {
  12. int16 i;
  13. float f;
  14. }valor;
  15. valor humedad, temperatura;
  16. byte errorsht11,checksum;
  17.  
  18. void leesht(void);
  19.  
  20. void main()
  21. {
  22.  
  23.   set_tris_a(0b11111111);
  24.   set_tris_d(0b00000000);
  25.   output_b(0x00);
  26.   output_d(0x00);
  27.                                                      
  28.   setup_adc(ADC_CLOCK_INTERNAL);
  29.   setup_port_a(AN0);
  30.   lcd_init ();
  31.   lcd_putc ("\f");
  32.   sht11_hard_reset();
  33.  
  34. while(1)
  35. {
  36. printf (lcd_putc,"tTemp. %0.1f",temperatura.f);
  37. printf(lcd_putc,"Humedad %0.1f%%",humedad.f);
  38.  
  39. }
  40.  
  41. void leesht(void)
  42. {
  43. errorsht11=0;
  44. errorsht11+=sht11_medicion((byte*) &humedad.i, &checksum, HUMI);     //measure humidity
  45. errorsht11+=sht11_medicion((byte*) &temperatura.i, &checksum, TEMP); //measure temperature
  46. if(errorsht11!=0)                                                    //in case of an error: connection reset
  47. {
  48. printf(lcd_putc,"\n\rerror:%U", errorsht11);
  49. }
  50. else
  51. {
  52. humedad.f=(float)humedad.i;                              //converts integer to float
  53. temperatura.f=(float)temperatura.i;                      //converts integer to float
  54. sht11_calculos(&humedad.f, &temperatura.f);              //calculate humidity, temperature
  55. }
  56. }

Estos son los fragmentos de código que uso para el dispositivo, sigue marcando humedad 0.1 y temperatura -40¿ ya no se que hacer...
con el 16f877 funcionaba bien, pero de vez en cuando había que borrar y volver a poner el dispositivo porque simulaba mal..

En la librería sh11 cambié los puertos del clock y data por los que estoy usando, a6 y a7.

Gracias por la atención

Un saludo.

Desconectado iNoXSteeL

  • PIC12
  • **
  • Mensajes: 75
    • La web de InoX
Re: Problema con SHT 75 y pic 18f4410
« Respuesta #5 en: 03 de Octubre de 2009, 06:49:09 »
Bueno, parece ser que cambiando de puerto hace otra cosa, ahora en el puerto E, los valores leídos en pantalla son de 0 y el mensaje de error es "error:4"..

Sigo sin ser capaz de que funcione con éste pic..

Alguien que arroje un poco de luz, por favor.

Gracias de antemano, Un saludo.