Autor Tema: Ayuda con el Empleo del comando Printf para el envio via rs232 DHT11 CCS CODE  (Leído 400 veces)

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

Desconectado GAMCH333

  • PIC10
  • *
  • Mensajes: 8
Buenos días!!

Una vez mas solicitando su ayuda he logrado interconectar el sensor DHT11 a un PIC16F877, teniendo como resultado el poder observar las variables humedad y temperatura en una pantalla de lcd 16x2, como se muestre en el siguiente programa.


// Interfacing PIC16F877A with DHT11 sensor
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections PUERTO B
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6
//End LCD module connections

#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use rs232 (BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7)      // Tx a 9600 BAUDS VIA RS-232 STANDAR
#include <lcd_B.c>
#use fast_io(A)
// Connection pin between PIC16F877A and DHT11 (RHT01) sensor ENTRADA RA0
#BIT Data_Pin = 0x05.0                       // Pin mapped to PORTA.0
#BIT Data_Pin_Direction = 0x85.0             // Pin direction mapped to TRISA.0

char message1[] = "Temp = 00.0 C";
char message2[] = "RH   = 00.0 %";
short Time_out;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
void start_signal()

{
  Data_Pin_Direction = 0;              // Configure connection pin as output
  Data_Pin = 0;                        // Connection pin output low
  delay_ms(25);
  Data_Pin = 1;                        // Connection pin output high
  delay_us(30);
  Data_Pin_Direction = 1;              // Configure connection pin as input
}
short check_response()

{
  delay_us(40);
  if(!Data_Pin)
  {                     // Read and test if connection pin is low
    delay_us(80);
    if(Data_Pin)
    {                    // Read and test if connection pin is high
      delay_us(50);
      return 1;
      }
    }
}
unsigned int8 Read_Data()
{
  unsigned int8 i, k, _data = 0;     // k is used to count 1 bit reading duration
  if(Time_out)
    break;
  for(i = 0; i < 8; i++)
  {
    k = 0;
    while(!Data_Pin)
    {                          // Wait until pin goes high
      k++;
      if (k > 100)

       {

       Time_out = 1; break;

       }
      delay_us(1);
      }
    delay_us(30);
    if(!Data_Pin)
      bit_clear(_data, (7 - i));               // Clear bit (7 - i)
    else
    {
      bit_set(_data, (7 - i));                 // Set bit (7 - i)
      while(Data_Pin)
      {                         // Wait until pin goes low
      k++;
      if (k > 100) {Time_out = 1; break;}
      delay_us(1);
      }
    }
  }
  return _data;
}
void main()
{
  lcd_init();                                 // Initialize LCD module
  lcd_putc('\f');                             // LCD clear
  while(TRUE)
  {
    delay_ms(1000);
    Time_out = 0;
    Start_signal();
    if(check_response())
    {                    // If there is response from sensor
      RH_byte1 = Read_Data();                 // read RH byte1
      RH_byte2 = Read_Data();                 // read RH byte2
      T_byte1 = Read_Data();                  // read T byte1
      T_byte2 = Read_Data();                  // read T byte2
      Checksum = Read_Data();                 // read checksum
      if(Time_out)
      {                           // If reading takes long time
        lcd_putc('\f');                       // LCD clear
        lcd_gotoxy(5, 1);                     // Go to column 5 row 1
        lcd_putc("Time out!");
      }
      else
      {
       if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))

       {
        message1[7]  = T_Byte1/10  + 48;
        message1[8]  = T_Byte1%10  + 48;
        message1[10] = T_Byte2/10  + 48;
        message2[7]  = RH_Byte1/10 + 48;
        message2[8]  = RH_Byte1%10 + 48;
        message2[10] = RH_Byte2/10 + 48;
        message1[11] = 223;
                                               // Degree symbol
        lcd_putc('\f');                       // LCD clear
        lcd_gotoxy(1, 1);                     // Go to column 1 row 1
        printf(lcd_putc, message1);           // Display message1
        lcd_gotoxy(1, 2);                     // Go to column 1 row 2
        printf(lcd_putc, message2);           // Display message2

                                              // Envio RS232
                         
        printf("%2d.%1d",message1[8],message1[10]);
        printf("%2d.%1d",message2[8],message2[10]);
        printf("\n\r");           // Display message2


        }
       }
      }
     }
   }

pero el resultado en la hyperterminar me son incorrectos dando como resultados estos.

50.9849.48
50.4849.48
50.9849.48
50.4849.48
50.9849.48
50.4849.48

Cuando lo que pretendo es poder obtener el valor de temperatura, y humedad separos por una coma como muestro a continuación:

22.0,30.0
22.0,30.0
22.0.30.0

Y entiendo que es por que utilizo mal el comando fprintf  .

Por favor ..Cualquier ayuda que me puedan brindar sobre la manera correcta de utilizar dicho comando se los agradeceré.

Sin mas por el momento reciban un saludo.





Desconectado Eduardo2

  • PIC24F
  • *****
  • Mensajes: 965
Re:Ayuda con el Empleo del comando Printf para el envio via rs232 DHT11 CCS CODE
« Respuesta #1 en: 06 de Marzo de 2024, 22:52:12 »
        printf("%2d.%1d",message1[8],message1[10]);
        printf("%2d.%1d",message2[8],message2[10]);

Si no escribís la coma, no va a aparecer...

        printf("%2d.%1d,",message1[8],message1[10]);
        printf("%2d.%1d",message2[8],message2[10]);

Desconectado Eduardo2

  • PIC24F
  • *****
  • Mensajes: 965
Re:Ayuda con el Empleo del comando Printf para el envio via rs232 DHT11 CCS CODE
« Respuesta #2 en: 07 de Marzo de 2024, 10:34:22 »
Por otro lado, lo que tenés en message1[]  es texto, y el switch %2d  es para un dato numérico.

Tal como está message[], deberías haber escrito:   printf("%c%c.%c,",message1[7],message1[8],message1[10]);



 

anything