سلام دوستان
من میخوام دما رو از رو سنسور DHT22 بخونم .
البته در ARM .
من کد زیر را با توجه به datasheet در keil نوشتم اما GPIO_ReadValue(0) پیوسته 0xff را نمایش می ده و به این ترتیب از حلقه های while خارج نمی شیم و دما محاسبه نمی شه . و این که آیا نیاز هست من از تایمر استفاده کنم ؟
دوستان میشه کد را نگاهی بندازید و اگر نظری دارید اعلام کنید .
من میخوام دما رو از رو سنسور DHT22 بخونم .
البته در ARM .
من کد زیر را با توجه به datasheet در keil نوشتم اما GPIO_ReadValue(0) پیوسته 0xff را نمایش می ده و به این ترتیب از حلقه های while خارج نمی شیم و دما محاسبه نمی شه . و این که آیا نیاز هست من از تایمر استفاده کنم ؟
دوستان میشه کد را نگاهی بندازید و اگر نظری دارید اعلام کنید .
کد:
#include "LPC17xx.h" #include "stdio.h" #include "string.h" //#define StartPin 23 #define Num 23 #define DHT22_DATA_BIT_COUNT 41 float lastHumidity; float lastTemperature; void StartSignal(void){ int i, j, retryCount ; int currentTemperature=0; int currentHumidity=0; unsigned int bitTimes[DHT22_DATA_BIT_COUNT]; //return currentTime; for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) { bitTimes[i] = 0; } //----------------------------------------------- Step 1: Send the activate pulse ---------------------------------------- // MCU send out start signal to DHT22 (1- pin is out put , 2- MCU pulls low for 18ms , 3- MCU pulls up for 20~40us). // and DHT22 send response signal to MCU(1-pin is input , 2- DTH22 pulls low 80us , 3- DHT22 pulls up 80us ). // If always signal high-voltage-level, it means DHT22 is not working properly, please check the electrical connection status. GPIO_SetDir(0, 1<< Num,1); //Set Pin 0.23 As output GPIO_ClearValue(0,0x800000); //pull down 0.23 delay_ms(18); // 18 ms wait (spec: at least 1ms) GPIO_SetValue(0,1<< Num); //pull up 0.23 //=========Find the start of the ACK Pulse retryCount = 0; do { if (retryCount > 40) { // (Spec is 20-40 us high) err=1; return err; } retryCount++; Delay_us(1); } while (GPIO_ReadValue(0)>> Num == 0xff); // Exit on DHT22 pull low within 40us //=======check that Am2302 is pulls low for 80us retryCount = 0; do { if (retryCount > 40) { // (Spec is 80 us, 40 * 2 == 80us) err=2; return err; } retryCount++; Delay_us(2); temp= GPIO_ReadValue(0)>> Num; } while ((GPIO_ReadValue(0)>> Num)== 0x00); // Exit on DHT22 pull high within 80us //===========check that Am2302 is pulls high for 80us retryCount = 0; do { if (retryCount > 40) { //(Spec is 80 us, 40 * 2 == 80us) err=3; return err; } retryCount++; Delay_us(2); temp= GPIO_ReadValue(0)>> Num; } while ((GPIO_ReadValue(0)>> Num)== 0xff); // Exit on DHT22 pull low within 80us //----------------------------------------------- Step 2: RHT22 send data to MCU ---------------------------------------- // Start bit -> low volage within 50us (actually could be anything from 35-75us) // 0 -> high volage within 26-28us (actually could be 10-40us) // 1 -> high volage within 70us (actually could be 60-85us) //============================ Read the 40 bit data stream for (i = 0; i < 5; i++) { for (j = 0; j < 8; j++) { // Instead of relying on the data sheet, just wait while the DHT22 pin is low // We now wait between 1us to 75us retryCount = 0; do { if (retryCount > 75) { err=4; return err; } retryCount++; Delay_us(1); } while (GPIO_ReadValue(0)>> Num == 0x00); temp = GPIO_ReadValue(0)>> Num; if (GPIO_ReadValue(0)>> Num == 0xff) { bitTimes[i*8+j] = 1; // If pin is still high, bit value is a 1 } else { bitTimes[i*8+j] = 0; // The bit value is a 0 } } }//end for // Second 16 bits is Temperature =============================== for(i = 0; i < 16; i++) { if(bitTimes[i + 17] > 11) { currentTemperature |= (1 << (15 - i)); } } if ((currentTemperature & 0x8000)==0x8000) { lastTemperature = ((float)(currentTemperature & 0x7FFF) / 10.0) * -1.0; } else { lastTemperature = (float)(currentTemperature) / 10.0; } } ///////////////////////////////////////////////////////////main//////////////////////////////////////////////// int main ( void ) { while ( 1 ) { StartSignal(); } }