Trying to read data from DHT11

15 Views Asked by At

Hell everyone! I'm trying to read the data from DHT11 and send it via the USB port, but it's seem to fall. It only send the "E" and "R" char from the code i did below. I'm using pic18F4550 with MikroC Pro compiler. Here's is my simulation circuit using proteus 8.15: https://drive.google.com/file/d/1elDUr-4a8b7v5Mhd-F2OhVAC8v02xXy5/view?usp=sharing And here's the code:

#define led_on    1
#define led_off   0
#define in_size   2
#define out_size  6
char count = 0;
bit oldstate;
unsigned char readbuff[in_size] absolute 0x500;
unsigned char writebuff[out_size] absolute 0x540;

unsigned char Check;
unsigned char uniT=0, decT=0, uniHR=0, decHR=0;
unsigned char AuniT=0, AdecT=0, AuniHR=0, AdecHR=0;
unsigned char T_byte1, T_byte2,RH_byte1, RH_byte2;
unsigned Sum;

sbit DataDHT at PORTA.B5;
sbit InDataDHT at TRISA.B5;

void StartSignal()
{
    TRISA.F5 = 0;
    DataDHT = 0;
    delay_ms(18);
    DataDHT = 1;
    delay_us(30);
    TRISA.F5 = 1;
}

void CheckResponse()
{
    Check = 0;
    delay_us(40);
    if(DataDHT == 0)
    {
        delay_us(80);
        if(DataDHT == 1)
        Check = 1;
        delay_us(40);
    }
}

char ReadData()
{
    char i, j;
    for(j = 0; j < 8; j++)
    {
        while(!DataDHT);
        delay_us(30);
        if(DataDHT == 0)
            i&= ~(1<<(7 - j));
        else
        {
            i|= (1 << (7 - j));
            while(DataDHT);
        }
    }
    return i;
}

void interrupt()
{
    if(PIR2.USBIF == 1)
    {
        PIR2.USBIF = 0;
        USB_Interrupt_Proc();
    }
    if(INTCON.INT0IF == 1)
    {
        INTCON.INT0IF = 0;
        oldstate = 1;
    }
}

void main()
{
    ADCON1 |= 0x0F;
    CMCON |= 0x07;
    INTCON2.RBPU = 0;
    PORTE = 0x00;
    LATE = 0x00;
    TRISE.TRISE0 = 0;
    TRISE.TRISE1 = 0;

    HID_Enable(&readbuff, &writebuff);
    INTCON.INT0IF = 0;
    INTCON.INT0IE = 1;
    INTCON2.INTEDG0 = 1;
    PIR2.USBIF = 0;
    PIE2.USBIE = 1;
    INTCON.GIE = 1;
    INTCON.PEIE = 1;
    while(1)
    {
        StartSignal();
        CheckResponse();
        if(Check == 1) //I'm trying to check if the DHT11 is working or not, but seem it doesn't
        {
            RH_byte1 = ReadData();
            RH_byte2 = ReadData();
            T_byte1 = ReadData();
            T_byte2 = ReadData();
            Sum = ReadData();
            if(Sum == ((RH_byte1+RH_byte2+T_byte1+T_byte2) & 0XFF))
            {
                decHR=RH_byte1/10;
                uniHR=RH_byte1%10;
                decT=T_byte1/10;
                uniT=T_byte1%10;

                AdecHR=decHR+0x30;
                AuniHR=uniHR+0x30;
                AdecT=decT+0x30;
                AuniT=uniT+0x30;

                writebuff[0] = 'T';
                writebuff[1] = decT + '0';
                writebuff[2] = uniT + '0';
                writebuff[3] = 'M';
                writebuff[4] = decHR + '0';
                writebuff[5] = uniHR + '0';
                delay_ms(10);
                HID_Write(&writebuff, out_size); //I can't not send the data with this line
            }
        }
        else
        {
            writebuff[0] = 'E';
            writebuff[1] = 'R';
            writebuff[2] = 'R';
            writebuff[3] = 'O';
            writebuff[4] = 'R';
            writebuff[5] = 'S';
            HID_Write(&writebuff, out_size); //And here, it only send the first two char
        }
        if(HID_Read() != 0)
        {
            if(readbuff[0] == '1')
            {
                RE0_bit = led_on;
            }
            else if(readbuff[0] == '0')
            {
                RE0_bit = led_off;
            }

            if(readbuff[1] == '1')
            {
                RE1_bit = led_on;
            }
            else if(readbuff[1] == '0')
            {
                RE1_bit = led_off;
            }
        }
        if(oldstate == 1)
        {
            oldstate = 0;
            HID_Write(&writebuff, out_size);
        }
        delay_ms(100);
    }
}
0

There are 0 best solutions below