/*---------------------------------------------------------------------------*/ static void owwriteb(unsigned byte) { int i = 7; do { if(byte & 0x01) { DS2411_OUTPUT(); OUTP_0(); udelay_tA(); OUTP_1(); /* Releases the bus */ udelay(tB); } else { OUTP_0(); udelay(tC); OUTP_1(); /* Releases the bus */ udelay(tD); } if(i == 0) { DS2411_INPUT(); return; } i--; byte >>= 1; } while(1); }
/*---------------------------------------------------------------------------*/ static int owreset(void) { int result; OUTP_0(); udelay(tH); OUTP_1(); /* Releases the bus */ udelay(tI); result = INP(); udelay(tJ); return result; }
/*---------------------------------------------------------------------------*/ static unsigned owreadb(void) { unsigned result = 0; int i = 7; do { OUTP_0(); udelay_tA(); OUTP_1(); /* Releases the bus */ udelay(tE); if(INP()) { result |= 0x80; /* LSbit first */ } udelay(tF); if(i == 0) { return result; } i--; result >>= 1; } while(1); }
uint8_t DHT_Read_Data(uint16_t *temperature, uint16_t *humidity){ //data[5] is 8byte table where data come from DHT are stored //laststate holds laststate value //counter is used to count microSeconds uint8_t data[5], laststate = 0, counter = 0, j = 0, i = 0; //Clear array data[0] = data[1] = data[2] = data[3] = data[4] = 0; uint8_t volatile sreg; sreg = SREG; /* Save status register before disabling interrupts. */ cli(); /* Disable interrupts. */ //Set pin Output //Pin High DHT_DRIVE(); mdelay(100); //Wait for 100mS //Send Request Signal //Pin Low OUTP_0(); //20ms Low mdelay(20); //Pin High OUTP_1(); udelay(40); //40us High //Set pin Input to read Bus //Set pin Input DHT_RELEASE(); laststate=DHT_INP(); //Read Pin value //Repeat for each Transistions for (i=0; i<MAXTIMINGS; i++) { //While state is the same count microseconds //led1_on(); //led1_off(); while (laststate==DHT_INP()) { udelay(1); counter++; if (counter>254) break; } if (counter>254) break; //laststate==_BV(DHT_PIN) checks if laststate was High //ignore the first 2 transitions which are the DHT Response //if (laststate==_BV(DHT_PIN) && (i > 2)) { if ((i&0x01) && (i > 2)) { //Save bits in segments of bytes //Shift data[] value 1 position left //Example. 01010100 if we shift it left one time it will be //10101000 data[j/8]<<=1; if (counter >= 15) { //If it was high for more than 40uS //led1_on(); data[j/8]|=1; //it means it is bit '1' so make a logic //led1_off(); } //OR with the value (save it) j++; //making an OR by 1 to this value 10101000 } //we will have the resault 10101001 //1 in 8-bit binary is 00000001 //j/8 changes table record every 8 bits which means a byte has been saved //so change to next record. 0/8=0 1/8=0 ... 7/8=0 8/8=1 ... 15/8=1 16/8=2 laststate=DHT_INP(); //save current state counter=0; //reset counter } SREG = sreg; /* Enable interrupts. */ //printf("HUM %d %d %d %d %d %d",data[0],data[1],data[2],data[3],data[4],(uint8_t)(data[0] + data[1] + data[2] + data[3]) ); //Check if data received are correct by checking the CheckSum if ((uint8_t)(data[0] + data[1] + data[2] + data[3]) == data[4]) { #ifdef DHT11 *humidity = data[0]*100; *temperature = data[2]*100; #else *humidity = ((uint16_t)data[0]<<8 | data[1])*10; *temperature = ((uint16_t)data[2]<<8 | data[3])*10; #endif return 0; }else{ *humidity = 2; *temperature = 2; // uart_puts("\r\nCheck Sum Error"); } return 0xff; // Check Sum Error }