PIC12F675 + DHT11

  • 1 Replies
  • 1891 Views
*

Offline JENG

  • *****
  • 808
  • รู้ทุกเรื่องเว้นเรื่องจริง
    • View Profile
PIC12F675 + DHT11
« on: May 14, 2015, 02:06:19 pm »





Source code
Code: [Select]
/*
PROGRAM : DHT11
AUTHOR : MR.SOMLAK MANGNIMIT
DATE : 13/05/2015
PIC12F675
 */


#include <htc.h>
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF);
//FOSC_INTRCIO
#define _XTAL_FREQ 4000000


#define delay_us(us)    __delay_us(us)
#define delay_ms(ms)    __delay_ms(ms)


#define SET_BIT(p, b)   (p|=1<<b)
#define CLR_BIT(p, b)   (p&=~(1<<b))
#define GET_BIT(p, b)   ((p&(1<<b))>>b)
#define TOG_BIT(p, b)   (p^=(1<<b))


volatile unsigned char DHT_H1, DHT_H2;
volatile unsigned char DHT_T1, DHT_T2;
volatile unsigned char DHT_SM;


struct bit_field
{
    unsigned char bit0:1;
    unsigned char bit1:1;
    unsigned char bit2:1;
    unsigned char bit3:1;
    unsigned char bit4:1;
    unsigned char bit5:1;
    unsigned char bit6:1;
    unsigned char bit7:1;
};


volatile union
{
    struct bit_field bit_of_data;
    unsigned char byte_of_data;
}status;


#define TOUT        status.bit_of_data.bit0
#define DHT_PIN 0
#define DHT_PIN_CHECK() GET_BIT(GPIO, DHT_PIN)


volatile unsigned char lcdbitmask;


#define LCD_CK_PIN 1
#define LCD_DT_PIN 2
#define LCD_EN_PIN 4


#define LCD_CK_LO()  CLR_BIT(GPIO, LCD_CK_PIN)
#define LCD_CK_HI()  SET_BIT(GPIO, LCD_CK_PIN)


#define LCD_DT_LO()  CLR_BIT(GPIO, LCD_DT_PIN)
#define LCD_DT_HI()  SET_BIT(GPIO, LCD_DT_PIN)


#define LCD_EN_LO()  CLR_BIT(GPIO, LCD_EN_PIN)
#define LCD_EN_HI()  SET_BIT(GPIO, LCD_EN_PIN)


//----------------------------------------------------------------------
void shiftOut(unsigned char byte_of_data)
//----------------------------------------------------------------------
{
    unsigned char i = 8;


    while(i--)
    {
        LCD_DT_LO();
        if(byte_of_data & 0x80) LCD_DT_HI();
        byte_of_data <<= 1;
        LCD_CK_LO();
        LCD_CK_HI();
    }
}


//----------------------------------------------------------------------
void lcdSendByte(unsigned char byte_of_data)
//----------------------------------------------------------------------
{
    unsigned char mask = 0;


    if(byte_of_data & 0x01) mask |= 0x01;
    if(byte_of_data & 0x02) mask |= 0x02;
    if(byte_of_data & 0x04) mask |= 0x04;
    if(byte_of_data & 0x08) mask |= 0x08;


    mask |= lcdbitmask;


    shiftOut(mask);
    LCD_EN_HI();
    LCD_EN_LO();
}


//----------------------------------------------------------------------
void lcdWriteByte(unsigned char byte_of_data)
//----------------------------------------------------------------------
{
    lcdSendByte(byte_of_data >> 4);
    lcdSendByte(byte_of_data & 0x0f);
}


//----------------------------------------------------------------------
void lcdWriteCommand(unsigned char byte_of_data)
//----------------------------------------------------------------------
{
    lcdWriteByte(byte_of_data);
    delay_ms(5);
}


//----------------------------------------------------------------------
void lcdWriteData(unsigned char byte_of_data)
//----------------------------------------------------------------------
{
    lcdbitmask |= 0x10;             //RS
    lcdWriteByte(byte_of_data);
    lcdbitmask ^= 0x10;             //RS
}


//----------------------------------------------------------------------
const unsigned char rowOffSet[] = {0x00, 0x40, 0x14, 0x54};
void lcdSetCursor(unsigned char col, unsigned char row)
//----------------------------------------------------------------------
{
    if(row > 4) row = 4-1;
    lcdWriteCommand(0x80|(col + rowOffSet[row]));
}


//----------------------------------------------------------------------
void lcdPrint(char * pointer)
//----------------------------------------------------------------------
{
    while(* pointer)
    {
        lcdWriteData(* pointer++);
    }
}


//----------------------------------------------------------------------
void lcdInitial(void)
//----------------------------------------------------------------------
{
    unsigned char i = 3;


    lcdbitmask = 0;
    shiftOut(0);
    LCD_EN_LO();


    delay_ms(50);


    while(i--)
    {
        lcdSendByte(0x33);
        delay_ms(5);
    }


    lcdSendByte(0x22);
    lcdWriteCommand(0x28);
    lcdWriteCommand(0x08);
    lcdWriteCommand(0x0C);
    lcdWriteCommand(0x01);
}


//----------------------------------------------------------------------
void interrupt isr(void)
//----------------------------------------------------------------------
{
    if(TMR1IF)
    {
        TOUT = 1;
        TMR1ON = 0;
        TMR1IF = 0;
    }
}


//----------------------------------------------------------------------
void DHT_PIN_LOW(void)
//----------------------------------------------------------------------
{
    CLR_BIT(TRISIO, DHT_PIN);
    CLR_BIT(GPIO, DHT_PIN);
}


//----------------------------------------------------------------------
void DHT_PIN_FLOAT(void)
//----------------------------------------------------------------------
{
    SET_BIT(TRISIO, DHT_PIN);
}


//----------------------------------------------------------------------
void dhtStartSignal(void)
//----------------------------------------------------------------------
{
    DHT_PIN_LOW();
    delay_ms(18);
    DHT_PIN_FLOAT();
    delay_us(40);
}


//----------------------------------------------------------------------
unsigned char dhtResponse(void)
//----------------------------------------------------------------------
{
    TOUT = 0;
    TMR1 = 0;
    TMR1ON = 1;
    while(!DHT_PIN_CHECK() && !TOUT);
    if(TOUT) return 0;


    TMR1 = 0;
    while(DHT_PIN_CHECK() && !TOUT);
    if(TOUT) return 0;
    else
    {
        TMR1ON = 0;
        return 1;
    }
}


//----------------------------------------------------------------------
unsigned char dhtReadByte(void)
//----------------------------------------------------------------------
{
    unsigned char byte_of_data;
    unsigned char counter;


    byte_of_data = 0;


    for(counter = 0; counter < 8; counter++)
    {
        TOUT = 0;
        TMR1 = 0;
        TMR1ON = 1;
        while(!DHT_PIN_CHECK() && !TOUT);
        if(TOUT) return 0;


        TOUT = 0;
        TMR1 = 0;
        TMR1ON = 1;
        while(DHT_PIN_CHECK()  && !TOUT);
        if(TOUT) return 0;
        else
        {
            TMR1ON = 0;
            if(TMR1 > 40) byte_of_data |= 1<<(7-counter);
        }
    }


    return byte_of_data;
}


//----------------------------------------------------------------------
void setup(void)
//----------------------------------------------------------------------
{
    GPIO   = 0x00;
    TRISIO = 0x00;
    CMCON  = 0x07;      //Disable comparator
    ANSEL  = 0x00;


    GIE = 1;            //Global interrupt enable
    PEIE = 1;           //Peripheral interrupt enable


    TMR1IE = 1;         //Timer 1 interrupt enable
    T1CON = 0;          //Prescaler 1:1 and timer 1 off
    TMR1IF = 0;         //Clear timer 1 interrupt flag
    TMR1 = 0;


    lcdInitial();
}


//----------------------------------------------------------------------
void main(void)
//----------------------------------------------------------------------
{
    setup();


    for(;;)
    {
        delay_ms(1000);


        dhtStartSignal();


        if(dhtResponse())
        {
            DHT_H1 = dhtReadByte();
            DHT_H2 = dhtReadByte();
            DHT_T1 = dhtReadByte();
            DHT_T2 = dhtReadByte();
            DHT_SM = dhtReadByte();


            if(DHT_SM == (DHT_H1 + DHT_H2 + DHT_T1 + DHT_T2))
            {
                lcdSetCursor(0, 0);
                lcdPrint("###  DHT-11  ###");
               
                lcdSetCursor(0, 1);
                lcdPrint("T: ");
                lcdWriteData(DHT_T1/10 + '0');
                lcdWriteData(DHT_T1%10 + '0');
                lcdWriteData(223);
                lcdPrint("C  ");


                lcdPrint("H: ");
                lcdWriteData(DHT_H1/10 + '0');
                lcdWriteData(DHT_H1%10 + '0');
                lcdPrint(" %");
            }
            else
            {
                lcdSetCursor(0, 0);
                lcdPrint("Checksum Error! ");
                lcdSetCursor(0, 1);
                lcdPrint("Trying Again ...");
            }
        }
        else
        {


            lcdSetCursor(0, 0);
            lcdPrint("No response     ");
            lcdSetCursor(0, 1);
            lcdPrint("from the sensor ");
        }
    }
}
Hex
Code: [Select]
:1000000014C02EC02DC02CC02BC02AC029C028C0AF
:1000100027C0D6C025C024C023C022C021C020C014
:100020001FC01EC01DC00040145411241FBECFE5C8
:10003000D4E0DEBFCDBF10E0A0E6B0E0E8EEF4E033
:1000400002C005900D92A63CB107D9F720E0A6ECBE
:10005000B0E001C01D92AE3CB207E1F735D142C21B
:10006000CFCFA09AA898089598E0949888230CF48C
:10007000949AC09A25E02A95F1F70000C098880F5D
:10008000915099F7979A85E08A95F1F70000979833
:100090000895CF9380FD02C090E001C090E181FD02
:1000A000906282FD906483FD9068C091C600C92B68
:1000B0008C2FDADF88E08C27D7DF8C2FD5DFCF912C
:1000C0000895CF93C82F82958F70E3DF8C2F8F70A8
:1000D000E0DFCF910895F5DF8FE19EE40197F1F71E
:1000E00000C0000008959091C60092609093C600F1
:1000F000E8DF9091C60082E089278093C6000895CA
:10010000653008F063E0E62FF0E0EA5DFF4FE49130
:100110008E0F8068E0DF0895CF93DF93EC01888134
:10012000882329F02196DFDF89918111FCCFDF91AF
:10013000CF9108959091C60091609093C60081116F
:1001400006C09091C60081E089278093C600809107
:10015000C6008ADF08951092C6008C9A8F9AB89ACA
:1001600080E082DF2FEF80E792E0215080409040D6
:10017000E1F700C0000083E38CDF8FE19EE401978C
:10018000F1F700C0000083E384DF8FE19EE4019774
:10019000F1F700C0000083E37CDF8FE19EE401976C
:1001A000F1F700C0000082E274DF88E294DF88E0AB
:1001B00092DF8CE090DF81E08EDF81E0BBDF08958D
:1001C0001F920F920FB60F9211248F938091CD0042
:1001D00081608093CD0013BE8F910F900FBE0F9062
:1001E0001F9018953EDF2FEF80EE90E02150804069
:1001F0009040E1F700C00000A09825ED2A95F1F7A6
:10020000000008958091CD008E7F8093CD0012BEB6
:1002100082E083BF989904C08091CD0080FFFACF1F
:100220009091CD00892F817090FD0EC012BE989BD9
:1002300004C09091CD0090FFFACF9091CD0090FD39
:1002400004C013BE81E0089580E0089527E030E007
:1002500060E072E041E050E08091CD008E7F8093BD
:10026000CD0012BE73BF989904C08091CD0080FF6D
:10027000FACF9091CD00892F817090FD1BC012BEE6
:10028000989B04C09091CD0090FFFACF9091CD0043
:1002900090FD13C013BE82B7813540F0CA01022E13
:1002A00002C0880F991F0A94E2F7682B2150310988
:1002B00098F602C080E00895862F08954CDF89B734
:1002C000816089BF78940895F9DF0F2EF8E78F2EAB
:1002D000F0E09F2EF02D0F2EFDEC3F2EF02D0F2E77
:1002E000F9E7AF2EF0E0BF2EF02D0F2EF7E6CF2E60
:1002F000F0E0DF2EF02D0F2EF8E6EF2EF0E0FF2ECF
:10030000F02D0AE710E00F2EFFED2F2EF02DCDE798
:10031000D0E00F2EFEE76F2EF0E07F2EF02D0F2E97
:10032000FFE74F2EF0E05F2EF02D2FEF89E698E1EA
:10033000215080409040E1F700C00000EFEFF9E667
:1003400028E1E150F0402040E1F700C000004ADF22
:1003500059DF81110CC060E0D3DE82E890E0DCDE82
:1003600061E080E0CDDE83E990E0D6DEDECF6EDFB7
:100370008093C8006BDF8093CB0068DF8093CC0054
:1003800065DF8093C70062DF8093CA002091CA00B6
:100390006091C8008091CB005091CC004091C70083
:1003A00030E090E0860F911D850F911D840F911D07
:1003B00099272817390709F07CC08091CC00839DCC
:1003C000812D1124869586958695805DF401808324
:1003D0008091CC00839D912D1124969596959695AC
:1003E000990F292F220F220F920F891B805DF50193
:1003F00080838091C800839D812D112486958695E8
:100400008695805DF60180838091C800839D912D43
:100410001124969596959695990F292F220F220FC4
:10042000920F891B805DF7018083F8012082809103
:10043000CC0090E09C01220F331F220F331F220FAC
:10044000331F820F931F9C0120563F4FADECBCEC35
:100450003DD096958795969587958093CC00809111
:10046000CC00839D812D1124869586958695805D8F
:1004700088838091CC00839D912D1124969596952B
:100480009695990F292F220F220F920F891B805DBD
:10049000F3018083F201208260E080E031DE81E7B9
:1004A00090E03ADE61E080E02BDE80E690E034DE32
:1004B0003CCF60E080E024DE84EA90E02DDE61E065
:1004C00080E01EDE85EB90E027DE2FCFA29FB001FB
:1004D000B39FC001A39F01D0B29F700D811D112455
:0804E000911D0895F894FFCF6F
:1004E80048756D693A202030302025524820202058
:1004F8000054656D703A2020303020432030302081
:1005080046004E6F20726573706F6E7365202020F1
:1005180020200066726F6D207468652073656E73A5
:100528006F722000436865636B73756D2045727246
:100538006F72212000547279696E67204167616982
:060548006E202E2E2E0095
:00000001FF
สามารถติดตาม electoday ได้ที่

Facebook
www.facebook.com/groups/coopmicro

Google+
https://plus.google.com/communities/103482067769375459277

☺☺☺ความรู้ และความฉลาด ไม่ใช่สิ่งเดียวกัน จะมีประโยชน์อะไร ถ้าฉลาดแต่อยู่ในกะลา☺☺☺

*

Offline jst

  • ***
  • 145
  • ย่อโลกให้กัน เพราะชีวิตแสนสั้น เรียนกันไม่หมด
    • View Profile
    • Free Cloud Data Log
Re: PIC12F675 + DHT11
« Reply #1 on: May 14, 2015, 05:47:52 pm »
ผมนี้รีบก๊อบเลย   ;D ;D ;D

Thanks