sumitbhut
Newbie level 3
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);
}