本帖最后由 Ricky 于 2013-5-25 12:27 编辑
- #include "dht11.h"
- #include "LCD12864RSPI.h"
- #define DHT11_PIN1 1
- #define DHT11_PIN2 2
- #define DHT11_PIN3 3
- #define DHT11_PIN4 4
- #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
- unsigned char wendu1[]=" T1:";
- unsigned char wendu2[]=" T2:";
- unsigned char wendu3[]=" T3:";
- unsigned char wendu4[]=" T4:";
- unsigned char shidu1[]=" H1:";
- unsigned char shidu2[]=" H2:";
- unsigned char shidu3[]=" H3:";
- unsigned char shidu4[]=" H4:";
- unsigned char du[]={0xA1, 0xE6};
- unsigned char h[]=" %";
- byte dht11_dat[5];
- int key=-1,oldkey=-1, NUM_KEYS = 5,adc_key_in,now;
- int adc_key_val[5] ={50, 200, 400, 600, 800 };
- char dha1[4],dha2[4],dhb1[4],dhb2[4],dhc1[4],dhc2[4],dhd1[4],dhd2[4];
- double ma1=0,ma2=0,mb1=0,mb2=0,mc1=0,mc2=0,md1=0,md2=0;
- void setup()
- {
- Serial.begin(9600);
- LCDA.Initialise(); //INIT SCREEN
- delay(100);
- initDHT( DHT11_PIN1 );
- initDHT( DHT11_PIN2 );
- initDHT( DHT11_PIN3 );
- initDHT( DHT11_PIN4 );
- }
-
- void initDHT( int nPinID )
- {
- DDRC |= _BV(nPinID);
- PORTC |= _BV(nPinID);
- }
-
- byte reading_dht11_dat( int nPinID )
- {
- byte i = 0;
- byte result=0;
- for(i=0; i< 8; i++){
- while(!(PINC & _BV(nPinID))); // wait for 50us
- delayMicroseconds(30);
-
- if(PINC & _BV(nPinID))
- result |=(1<<(7-i));
-
- while((PINC & _BV(nPinID))); // wait '1' finish
- }
- return result;
- }
-
- bool Read_Data_From_PIN(int nPinID, byte dht11_dat[] )
- {
- byte dht11_in;
- byte i;
- // start condition
- // 1. pull-down i/o pin from 18ms
- PORTC &= ~_BV(nPinID);
- delay(18);
- PORTC |= _BV(nPinID);
- delayMicroseconds(40);
-
- DDRC &= ~_BV(nPinID);
- delayMicroseconds(40);
-
- dht11_in = PINC & _BV(nPinID);
-
- if(dht11_in){
- Serial.println("dht11 start condition 1 not met");
- return false;
- }
- delayMicroseconds(80);
-
- dht11_in = PINC & _BV(nPinID);
-
- if(!dht11_in){
- Serial.println("dht11 start condition 2 not met");
- return false;
- }
- delayMicroseconds(80);
- // now ready for data reception
- for (i=0; i<5; i++)
- dht11_dat = reading_dht11_dat( nPinID );
-
- DDRC |= _BV(nPinID);
- PORTC |= _BV(nPinID);
-
- byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
- // check check_sum
- if(dht11_dat[4]!= dht11_check_sum)
- {
- Serial.println("dht11 checksum error");
- return false;
- }
- return true;
- }
-
-
- void print_Data(int nPinID, byte dht11_dat[] )
- {switch(nPinID)
- {
- case DHT11_PIN1:
- ma1=dht11_dat[0]+dht11_dat[1];
- ma2=dht11_dat[2]+dht11_dat[3];
- dtostrf(ma1,3,1,dha1);
- dtostrf(ma2,3,1,dha2);
- LCDA.DisplayString(0,1,shidu1,AR_SIZE(shidu1));
- LCDA.DisplayString(0,3,(unsigned char *)dha1,AR_SIZE(dha1));
- LCDA.DisplayString(0,5,h,AR_SIZE(h));
- LCDA.DisplayString(1,1,wendu1,AR_SIZE(wendu1));
- LCDA.DisplayString(1,3,(unsigned char *)dha2,AR_SIZE(dha2));
- LCDA.DisplayString(1,5,du,AR_SIZE(du));
- break;
- case DHT11_PIN2:
- mb1=dht11_dat[0]+dht11_dat[1];
- mb2=dht11_dat[2]+dht11_dat[3];
- dtostrf(mb1,3,1,dhb1);
- dtostrf(mb2,3,1,dhb2);
- LCDA.DisplayString(2,1,shidu2,AR_SIZE(shidu2));
- LCDA.DisplayString(2,3,(unsigned char *)dhb1,AR_SIZE(dhb1));
- LCDA.DisplayString(2,5,h,AR_SIZE(h));
- LCDA.DisplayString(3,1,wendu2,AR_SIZE(wendu2));
- LCDA.DisplayString(3,3,(unsigned char *)dhb2,AR_SIZE(dhb2));
- LCDA.DisplayString(3,5,du,AR_SIZE(du));
- break;
- case DHT11_PIN3:
- mc1=dht11_dat[0]+dht11_dat[1];
- mc2=dht11_dat[2]+dht11_dat[3];
- dtostrf(mc1,3,1,dhc1);
- dtostrf(mc2,3,1,dhc2);
- LCDA.DisplayString(0,1,shidu3,AR_SIZE(shidu3));
- LCDA.DisplayString(0,3,(unsigned char *)dhc1,AR_SIZE(dhc1));
- LCDA.DisplayString(0,5,h,AR_SIZE(h));
- LCDA.DisplayString(1,1,wendu3,AR_SIZE(wendu3));
- LCDA.DisplayString(1,3,(unsigned char *)dhc2,AR_SIZE(dhc2));
- LCDA.DisplayString(1,5,du,AR_SIZE(du));
- break;
- case DHT11_PIN4:
- md1=dht11_dat[0]+dht11_dat[1];
- md2=dht11_dat[2]+dht11_dat[3];
- dtostrf(md1,3,1,dhd1);
- dtostrf(md2,3,1,dhd2);
- LCDA.DisplayString(2,1,shidu4,AR_SIZE(shidu4));
- LCDA.DisplayString(2,3,(unsigned char *)dhd1,AR_SIZE(dhd1));
- LCDA.DisplayString(2,5,h,AR_SIZE(h));
- LCDA.DisplayString(3,1,wendu4,AR_SIZE(wendu4));
- LCDA.DisplayString(3,3,(unsigned char *)dhd2,AR_SIZE(dhd2));
- LCDA.DisplayString(3,5,du,AR_SIZE(du));
- break;
- }
- delay(1000);
- }
-
- void xianshi1()
- {
- if( Read_Data_From_PIN( DHT11_PIN1, dht11_dat ) )
- {
- print_Data( DHT11_PIN1, dht11_dat );
- }
- if( Read_Data_From_PIN( DHT11_PIN2, dht11_dat ) )
- {
- print_Data( DHT11_PIN2, dht11_dat );
- }
- }
-
- void xianshi2()
- {
- if( Read_Data_From_PIN( DHT11_PIN3, dht11_dat ) )
- {
- print_Data( DHT11_PIN3, dht11_dat );
- }
- if( Read_Data_From_PIN( DHT11_PIN4, dht11_dat ) )
- {
- print_Data( DHT11_PIN4, dht11_dat );
- }
- }
-
- int get_key(unsigned int input)
- {
- int k;
- for (k = 0; k < NUM_KEYS; k++)
- {
- if (input < adc_key_val[k])
- {
- return k;
- }
- }
- if (k >= NUM_KEYS)k = -1; // No valid key pressed
- return k;
- }
-
- void loop()
- {
- adc_key_in = analogRead(5);
- key = get_key(adc_key_in);
- if(key!=oldkey)
- {
- delay(50);
- adc_key_in = analogRead(5);
- key = get_key(adc_key_in);
- if(key!=oldkey)
- {
- oldkey=key;
- if(key>=0)
- {
- switch(key)
- {
- case 0:
- {
- LCDA.CLEAR();
- xianshi1();
- break;
- }
- case 1:
- {
- LCDA.CLEAR();
- xianshi2();
- break;
- }
- }
- }
- }
- }
- }
复制代码
|