ATtiny10 working with both DHT11 and DHT22 via serial. Still work in progress, but is functional. (programed in Microchip Studio)
/* * ATtiny10-DHT-11_22.c * * Created: 5/27/2022 19:03:57 * Author : Sherm * * Some of the code was ported from: * https://circuitdigest.com/microcontroller-projects/interfacing-dht11-sensor-with-pic16f877a-microcontroller * * Still work in progress.. * Now works with both DHT11 and DHT22: * DHT22 only does C and H for now. * DHT11 does C and H and F DHT11 3 4 1 2C 0022 0000 H 0083 0000C 0022 0000 H 0082 0000C 0022 0000 H 0084 0000 DHT22C 0000 0229 H 0001 0211C 0000 0230 H 0001 0200C 0000 0230 H 0001 0196 * * Original: * ATtiny10-DHT-11.c * * Created: 5/26/2022 16:26:59 * Author : Sherm
Serial Thanks to Ben Heck's Hacks youtube chanel.
* */ #define F_CPU 8000000UL#include <avr/io.h>#include <util/delay.h>#include <avr/interrupt.h> //#define DHT11 #define DHT22 #define DHT_Pin PB2 #define TX PB0#define FIRST_LINE 0x80#define SECOND_LINE 0xC0uint8_t Check_bit, Temp_byte_1, Temp_byte_2, RH_byte_1, RH_byte_2, Sumation;//uint8_t Himudity, RH, Sumation ; //Dht11 related definitionvoid dht_init();void find_response();uint8_t read_dht(); static volatile char Xmit;static volatile uint8_t whichBitOut = 10; ISR(TIM0_COMPA_vect){switch(whichBitOut){case 1://start bitPORTB &= ~(1<<TX); //go low for start bitwhichBitOut = 2;break;case 2 ... 9:if (Xmit & 0x01){PORTB |= (1<<TX);}else{PORTB &= ~(1<<TX);}Xmit >>= 1;whichBitOut++;break;case 10:PORTB|=(1<<TX);whichBitOut = 0;break;}} void sendByte(uint8_t theByte){Xmit = theByte;whichBitOut = 1;while(whichBitOut){} //wait for it to finish before another} void print(const char *data){while(*data != 0x00){sendByte(*data++);}} //leading 0void printNumLeading(uint16_t number){uint16_t divider = 10000;for(uint8_t i=0;i<5;i++){if(number>=divider){sendByte((number/divider)+48);number %= divider;}else{sendByte('0');}divider /=10;}} //no leading 0void printNum(uint16_t number){uint16_t divider = 10000;uint8_t numStart = 0;for(uint8_t i=0;i<5;i++){if(number>=divider){sendByte((number/divider)+48);number %= divider;numStart=1;}else if(numStart){sendByte('0');}divider /=10;}} void txInit(void){//CCP = 0xD8;//CLKPSR = 0b00000000;DDRB |= (1<<TX);//0b00001111;//PUEB = 0b00000100;TCCR0A = 0b00000000;TCCR0B = 0b00001001;// baud = 8000000/9600 = 833.333333OCR0AH = 808 >> 8; //833OCR0AL = 808 & 0xFF;TIMSK0 |= (1<<OCIE0A);sei();} int main(void) { CCP=0xD8;CLKPSR=0x0; txInit(); while(1){ _delay_ms(2500); //must have at least 2 second interval. per datasheet dht_init(); find_response(); //may be fixed.. test//Check_bit=1; //added by sherm if(Check_bit == 1){ RH_byte_1 = read_dht(); RH_byte_2 = read_dht(); Temp_byte_1 = read_dht(); Temp_byte_2 = read_dht(); Sumation = read_dht(); if(Sumation == ((RH_byte_1+RH_byte_2+Temp_byte_1+Temp_byte_2) & 0XFF)){sendByte('C'); //258//sendByte(' '); //258#ifdef DHT11printNum(Temp_byte_1); #else//printNumLeading(Temp_byte_1); //printNumLeading(Temp_byte_2); printNum(Temp_byte_2/10);sendByte('.');if(Temp_byte_2%10){printNum(Temp_byte_2%10);}else{sendByte('0');}#endif//sendByte(' ');//printNumLeading(Temp_byte_2); //sendByte('F');sendByte(' ');#ifdef DHT11uint16_t f = ((Temp_byte_1*10)*18)+3200;printNum((f/100)%1000); sendByte('.');printNum(f%1000);#else //DHT22//uint16_t f = ((Temp_byte_2*10)*18)+3200;//printNum((f/100)%1000); //sendByte('.');//printNum(f%1000);#endifsendByte(' ');sendByte('H'); //258//sendByte(' '); //258#ifdef DHT11printNum(RH_byte_1);sendByte(' '); printNum(RH_byte_2); sendByte(' '); #else //DHT22uint16_t H = RH_byte_1*256+RH_byte_2;//printNumLeading(RH_byte_1);//printNumLeading(RH_byte_2);printNum((H%1000)/10);sendByte('.');if(H%10){printNum(H%10);}else{sendByte('0');}#endif//printNumLeading(Sumation); sendByte(13);//printNum(Sumation);//sendByte(13); }else{print("Check sum error\n"); } }else{sendByte('E'); sendByte(13); /* clear_screen(); lcd_com (0x80); lcd_puts("Error!!!"); lcd_com (0xC0); lcd_puts("No Response.");*/ } //print("End\n"); // _delay_ms(100); }} //Connect to DHT:void dht_init(){// DHT_Pin_Direction= 0; //Configure RD0 as outputDDRB |= (1<<DHT_Pin);// sends 0 to the sensor for 18msPORTB &= ~(1<<DHT_Pin);_delay_ms(18);// sends 1 to the sensor for 30usPORTB |= (1<<DHT_Pin);_delay_us(30);// Configure DHT Data Pin as inputDDRB &= ~(1<<DHT_Pin); } //Check if connected: void find_response(){Check_bit = 0;_delay_us(40);if (!(PINB & (1<<DHT_Pin))){_delay_us(80);//}//shermif (PINB & (1<<DHT_Pin)){Check_bit = 1;} _delay_us(50);} } //read data from DHT11 or 22 uint8_t read_dht(){uint8_t data;for(uint8_t for_count = 0; for_count < 8; for_count++){while(!(PINB & (1<<DHT_Pin))); _delay_us(50); //was 30if((PINB & (1<<DHT_Pin)) == 0){data &= ~(1<<(7 - for_count)); //Clear bit (7-b)}else{data |= (1 << (7 - for_count)); //Set bit (7-b)while((PINB & (1<< DHT_Pin))); //was waiting while high} //Wait until PORTB DHT_Pin goes LOW}return data; }