Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

DHT11 temp and humidity sensor with PIC16f1829 in MPLAB code problem

Status
Not open for further replies.

sumitbhut

Newbie level 3
Joined
Jul 1, 2016
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
64
Hello, i m read humidity and temperature from a DHT11 and display it on a UART terminal with a PIC microcontroller(PIC16f1829).The start signal is supposed to make RA0 low for 25ms, then high for 40us, then it gets configured as an input and waits for the response signal from the DHT11.But response is only “No response from the sensor,” plz. fined error this code

Code:
#include<pic.h>
#include<stdlib.h>
#include<stdio.h>


#define _XTAL_FREQ 32000000
#pragma config FOSC = INTOSC
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#pragma config PLLEN = ON      // PLL Enable (4x PLL disabled)


char message1[] = "Temp = 00.0 C";
char message2[] = "RH   = 00.0 %";
unsigned short TOUT = 0, CheckSum, i;
unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;


void InitUART(void)
{
	TRISC4 = 0;   					// TX Pin
	TRISC5 = 1;   					// RX Pin
	
	SPBRG = ((_XTAL_FREQ/16)/9600) - 1;
	BRGH  = 1;                   	// Fast baudrate
	//BRG16 = 0;
    SYNC  = 0;						// Asynchronous
	SPEN  = 1;						// Enable serial port pins
	CREN  = 1;						// Enable reception
	SREN  = 0;						// No effect
	TXIE  = 0;						// Disable tx interrupts
	
	TX9   = 0;						// 8-bit transmission
	RX9   = 0;						// 8-bit reception
	TXEN  = 0;						// Reset transmitter
	TXEN  = 1;						// Enable the transmitter
}


void SendByteSerially(unsigned char Byte)  // Writes a character to the serial port
{
	 // wait for previous transmission to finish
    while(!TXIF);
	TXREG = Byte;
    
    //Lcd_Write_Char('Y');
}

unsigned char ReceiveByteSerially(void)   // Reads a character from the serial port
{
	if(OERR) // If over run error, then reset the receiver
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF);  // Wait for transmission to receive
	
	return RCREG;
}

void SendStringSerially(const unsigned char* st)
{
	while(*st)
		SendByteSerially(*st++);
}

void StartSignal(){
    TRISA&=~0x01;
 
PORTAbits.RA0 = 0;     // Data port is output

  __delay_ms(25);
  PORTAbits.RA0 = 1;

  __delay_us(40);
  TRISA|=0x01;  // Data port is input
}

unsigned short CheckResponse(){
  TOUT = 0;
  TMR2 = 0;
  TMR2ON = 1;      // start timer
  while(!PORTAbits.RA0 && !TOUT);
  if (TOUT) return 0;
  else {
   TMR2 = 0;
   while(PORTAbits.RA0 && !TOUT);
   if (TOUT) return 0;
   else {
   TMR2ON = 0;
   return 1;
   }
  }
}

unsigned short ReadByte(){
  unsigned short num = 0, t;
  TRISA|=0x01;
  for (i=0; i<8; i++){
   while(!PORTAbits.RA0);
   TMR2 = 0;
   TMR2ON = 1;
   while(PORTAbits.RA0);
   TMR2ON = 0;
   if(TMR2 > 40) num |= 1<<(7-i);  // If time > 40us, Data is 1
  }
  return num;
}

void interrupt ISR(){
  if(TMR2IF){
   TOUT = 1;
   TMR2ON = 0; // stop timer
   TMR2IF  = 0; // Clear TMR0 interrupt flag
  }
}

void main() {
    
  unsigned short check;
  int buf;
   APFCON0=0x84;//Alternate function enable for TX RX
   ANSELA&=~0x01;//Digital select RA2
   OPTION_REG|=0x80;
   OSCCON=0x70;//Internal Oscillator frequency selec
   InitUART(); 
   __delay_ms(100);
   SendStringSerially("start");
   
  TMR2IE = 1;  // Enable Timer2 interrupt
  T2CON = 0;        // Prescaler 1:1, and Timer2 is off initially
  TMR2IF =0;   // Clear TMR INT Flag bit
  TMR2 = 0;
  

  GIE = 1;    //Enable global interrupt
  PEIE = 1;   //Enable peripheral interrupt
  
  do {
    __delay_ms(1000);
    StartSignal();
    check = CheckResponse();
    if (!check) {
     SendStringSerially("No response  ");
     SendStringSerially("from the sensor");  
     SendStringSerially(",\r\n");
    }
    else
    {
    
     RH_Byte1 = ReadByte();
     RH_Byte2 = ReadByte();
     T_Byte1 = ReadByte();
     T_Byte2 = ReadByte();
     CheckSum = ReadByte();
     
     // Check for error in Data reception
     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
      
   
      
     buf=atoi(message1);
     SendStringSerially(buf);  
     SendStringSerially("\r\n");
     
     buf=atoi(message2);
     SendStringSerially(buf);  
     SendStringSerially("\r\n");
     } 
     else{
     SendStringSerially("Trying Again ...");  
     SendStringSerially("\r\n");
     SendStringSerially("Checksum Error!");  
     SendStringSerially("\r\n");
      }
    }

  }while(1);
}
 

Attachments

  • dht_11.jpg
    dht_11.jpg
    184.9 KB · Views: 35
  • DHT11.X.rar
    144.6 KB · Views: 21

jean12

Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
I advise you to go step by step,don`t upload all codes together,first establish communication and then start reading and writing.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,571
Helped
5,036
Reputation
10,096
Reaction score
4,876
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
131,972
This isn't the underlying problem but I'm suspicious of:
Code:
void SendStringSerially(const unsigned char* st)
{
	while(*st)
		SendByteSerially(*st++);
}
I'm not sure 'st' should be a const.

Brian.
 

sumitbhut

Newbie level 3
Joined
Jul 1, 2016
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
64
Hi jean,
i m using this code for only bit writing then bit reading and show the read bit display in terminal.....but only two bits read....



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<pic.h>
#include<stdlib.h>
#include<stdio.h>
#include <time.h>
 
 
 
#define _XTAL_FREQ 32000000
//#define DHT PORTAbits.RA2
#pragma config FOSC = INTOSC
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#pragma config PLLEN = ON      // PLL Enable (4x PLL disabled)
void InitUART(void)
{
    TRISC4 = 0;                     // TX Pin
    TRISC5 = 1;                     // RX Pin
    
    SPBRG = ((_XTAL_FREQ/16)/9600) - 1;
    BRGH  = 1;                      // Fast baudrate
    //BRG16 = 0;
    SYNC  = 0;                      // Asynchronous
    SPEN  = 1;                      // Enable serial port pins
    CREN  = 1;                      // Enable reception
    SREN  = 0;                      // No effect
    TXIE  = 0;                      // Disable tx interrupts
    
    TX9   = 0;                      // 8-bit transmission
    RX9   = 0;                      // 8-bit reception
    TXEN  = 0;                      // Reset transmitter
    TXEN  = 1;                      // Enable the transmitter
}
 
 
void SendByteSerially(unsigned char Byte)  // Writes a character to the serial port
{
     // wait for previous transmission to finish
    while(!TXIF);
    TXREG = Byte;
 }
 
unsigned char ReceiveByteSerially(void)   // Reads a character from the serial port
{
    if(OERR) // If over run error, then reset the receiver
    {
        CREN = 0;
        CREN = 1;
    }
    
    while(!RCIF);  // Wait for transmission to receive
    
    return RCREG;
}
 
void SendStringSerially(const unsigned char* st)
{
    while(*st)
        SendByteSerially(*st++);
}
 
 
void main()
{ unsigned char buf[16],pre[15]={0},pressure=0;
  unsigned char pre_buf[42]={0},pre_buf1[42]={0},s=0,i,start=0,pre_dec=0,threshold,j,data[5]={0};
  float humidity;
    float temperature;  
    OPTION_REG|=0x80;
    OSCCON=0x70;//Internal Oscillator frequency select
    ANSELA&=~0x04;//Digital select RA2
    APFCON0=0x84;//Alternate function enable for TX RX
    // WPUA=0x04;
    InitUART(); 
    __delay_ms(500);
    SendStringSerially("start");
 
    while(1)
   { 
    TMR1H=0;
    TMR1L=0;
    
    /*************StartSignal*********/
    TRISA&=~0x04;        //Configure RA2 as output
    PORTAbits.RA2 = 0;
    __delay_ms(25);
    PORTAbits.RA2 = 1;
    __delay_us(40);
    TRISA|=0x04;       //Configure RA2 as input
    /**********ReadData***********/
    /*******Starting 2 bit for CheckResponse and last 40 bit DATA  *********/
    for(i=0;i<41;i++)
    {
    TMR1H=0;
    TMR1L=0;
    T1CON=1;
    while(!PORTAbits.RA2);            //Waiting for Echo low
    T1CON= 0;               //Timer Stops
    pre_buf[i] = (TMR1L|(TMR1H<<8));
    sprintf(buf,"%d",(TMR1L|(TMR1H<<8)));
    SendStringSerially(buf);
    SendStringSerially("\r\n");
    
    TMR1H=0;
    TMR1L=0;
    T1CON=1;               //Timer Stops
    while(PORTAbits.RA2 ); //Waiting for Echo goes high
    T1CON= 0;               //Timer Stops
    pre_buf1[i] = (TMR1L|(TMR1H<<8));
    sprintf(buf,"%d",(TMR1L|(TMR1H<<8)));
    SendStringSerially(buf);
    SendStringSerially("\r\n");
    }
    
    threshold= ((pre_buf1[0]+pre_buf[0])/2);
    for(i=0;i<41;i++)
    {
    j=i/8;
    if(pre_buf1[i]>=threshold)
    data[j] |= 1;
    }
    if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) 
    {
      humidity = data[0];
      temperature = data[2];
    }
    else    {
       SendStringSerially("DHT_ERROR_CHECKSUM");
    }
    }
   }

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top