many of us need an emulation opportunity in Proteus. This is done by a 12F683 with two analog input, that represents the humidity and temperature.
Code: Select all
/*******************************************************************************
DHT11
____ ____ ____ ___
|___________| |____| |.....................................|
| 18ms | |80us|80us| 5x8 bit |
| PIC | | DHT11 response | end
| request | | 2x80us, intRH, decRH, intT, decT, checksum | of frame
____ ________
|________| |... 0 bit
<-50us-> <-27us->
____ _________________________
|________| |... 1 bit
<-50us-> <---------70us---------->
*******************************************************************************/
unsigned short int Packet[5];
char BitCounter, ByteCounter, Request;
void SendByte(unsigned short int DataByte) {
for(BitCounter=0; BitCounter<8; BitCounter++) {
if((DataByte & 0b00000001)==0) { // if bit is 0...
GPIO.F2 = 0;
delay_us(50);
GPIO.F2 = 1;
delay_us(27);
}
else { // if bit is 1...
GPIO.F2 = 0;
delay_us(50);
GPIO.F2 = 1;
delay_us(70);
}
DataByte = DataByte >> 1;
}
}
void main() {
OSCCON = 0b01110001; // pdf 22.
ANSEL = 0b00000011; // GP0, GP1 analog inputs, pdf 35.
CMCON0 = 0b00000111; // comparator disabled
WPU = 0b00001100; // pdf 36.
TRISIO = 0b00001111; // GP3 always input
GPIO = 0;
ADC_Init();
delay_ms(100);
while(1) {
while(GPIO.F2);
while(!GPIO.F2) {
delay_ms(2);
Request++;
}
// This packet compilation takes 600 us at 8 MHz internal clock.
// A real DHT11 takes only 20...40 us!!!
Packet[0] = ADC_Read(0) / 10; // humidity integer
Packet[1] = 0; // humidity fraction
Packet[2] = ADC_Read(1) / 10; // temperature integer
Packet[3] = 0; // temperature fraction
Packet[4] = Packet[0] + Packet[1] + Packet[2] + Packet[3]; // CRC
if(Request==9) { // 18ms request
Request = 0;
TRISIO.F2 = 0; // switch input to output
GPIO.F2 = 0; // 2x80us sensor present signal
delay_us(80);
GPIO.F2 = 1;
delay_us(68); // additional operations take 12 us
for(ByteCounter=0; ByteCounter<5; ByteCounter++) {
SendByte(Packet[ByteCounter]);
}
TRISIO.F2 = 1; // switch output to input
}
}
}