Electoday 2025

ไมโครคอนโทรลเลอร์ => AVR and Arduino => Topic started by: ideachanel99 on February 05, 2013, 11:32:24 AM

Title: ใครเคยลอง DHT11 กับ AVR บ้างครับ
Post by: ideachanel99 on February 05, 2013, 11:32:24 AM
ลองทำตาม Datasheet ของ DHT11 แล้วยังไม่ออก ผมเคยลองนำโค๊ดของพี่ท่านหนึ่ง ไม่แน่ใจว่าพี่ Ozuke หรือของพี่ jeng พอดีว่า HDD ของผมเสีย โค๊ดที่เป็นต้นฉบับและที่เอามาดัดแปลงเพิ่มเติมเลยหายหมด 
***มาแจ้งอัพเดท ตอนนี้ผมกู้ข้อมุลเก่ากลับมาได้หมดแล้ว ขอบคุณทุกๆท่านครับ
Title: Re: ใครเคยลอง DHT11 กับ AVR บ้างครั้ง
Post by: avr_lover on February 05, 2013, 11:46:16 AM
ผมใช้ตัวนี้อยู่
http://www.es.co.th/detail.asp?Prod=39100019
และใช้งาน Codevision AVR ลองดูไหมครับ

Code

//********************************************************
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
ฉ Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date    : 25/10/2012
Author  : PerTic@n
Company : If You Like This Software,Buy It
Comments:


Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>// External Interrupt 0 service routine   
#include <delay.h>
#include <string.h>// Declare your global variables here 


#define   DATA     PINB.0
#define   DATAHI() DDRB.0 = 0
#define   DATALO() DDRB.0 = 1

#define   DATA_in      PINB.0 
#define   DATA_out_HI() {DDRB.0 = 1; PORTB.0 = 1;}
#define   DATA_out_LO()  {DDRB.0 = 1; PORTB.0 = 0;}
#define   DATA_in_HI() {DDRB.0 = 0; PORTB.0 = 1;}
#define   DATA_in_LO()  {DDRB.0 = 0; PORTB.0 = 0;}

// #define   SCKHI()  DDRB.1 = 0
// #define   SCKLO()  DDRB.1 = 1
#define   SCKHI()  PORTB.1 = 1
#define   SCKLO()  {DDRB.1 = 1; PORTB.1 = 0;}
/*
#define   DATA     PINC.2
#define   DATAHI() DDRC.2 = 0
#define   DATALO() DDRC.2 = 1

#define   DATA_in      PINC.2 
#define   DATA_out_HI() {DDRC.2 = 1; PORTC.2 = 1;}
#define   DATA_out_LO()  {DDRC.2 = 1; PORTC.2 = 0;}
#define   DATA_in_HI() {DDRC.2 = 0; PORTC.2 = 1;}
#define   DATA_in_LO()  {DDRC.2 = 0; PORTC.2 = 0;}

// #define   SCKHI()  DDRB.1 = 0
// #define   SCKLO()  DDRB.1 = 1
#define   SCKHI()  PORTC.3 = 1
#define   SCKLO()  {DDRC.3 = 1; PORTC.3 = 0;}
*/
    ////////////////////////////////////////////////////   
        // Declare your global variables here
typedef union {
    unsigned int i;
    float f;
} value;
unsigned int  bit_data;
unsigned int data_bit[17];
float humidity_show1,temperature_show1,True_humidity_show1;
#define _nop_()  delay_us(100) //delay_us(2)


                            //adr  command  r/w
#define STATUS_REG_W 0x06       //000   0011    0
#define STATUS_REG_R 0x07       //000   0011    1
#define MEASURE_TEMP 0x03       //000   0001    1
#define MEASURE_HUMI 0x05       //000   0010    1
#define RESET        0x1e       //000   1111    0




//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
//       _____          __   
// DATA:      |_______|   |__
//           ___     ___
// SCK : ___|   |___|   |______
{
    DATA_out_HI();
    SCKLO();                    //Initial state
    _nop_();
    SCKHI();
    _nop_();
     DATA_out_LO();
    _nop_();
    SCKLO();
    _nop_();
    SCKHI();
    _nop_();
    DATA_out_HI();
    _nop_();
    SCKLO();
     _nop_();
    DATA_out_LO();
}



//----------------------------------------------------------------------------------
void s_clk(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart         
// SCK : __| |__
{
        _nop_();
        SCKLO();
        _nop_();
        SCKHI();
         _nop_();
        SCKLO();
        _nop_();
}

//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//       _____________________________________________________   
// DATA:                                                     
//          _    _    _    _    _    _    _    _    _       
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |_____
{
    unsigned char i;
     _nop_();
     DATA_out_HI();
      _nop_();
    SCKLO();     
     _nop_();                //Initial state
    for (i = 0; i < 9; i++)     //9 SCK cycles
    { 
    s_clk();
    }
   
   
   

}
//----------------------------------------------------------------------------------
void print_serail_data_timing(void)
//----------------------------------------------------------------------------------
{

  printf("**********Humidity***************");
      printf("\r\n");             
      printf("%d",data_bit[0] );
      printf(",");   
      printf("%d",data_bit[1]);
      printf(",");       
      printf("%d",data_bit[2] );
      printf(","); 
      printf("%d",data_bit[3] );
      printf(",");
      printf("%d",data_bit[4] );
      printf(",");   
      printf("%d",data_bit[5] );
      printf(",");       
      printf("%d",data_bit[6] );
      printf(","); 
      printf("%d",data_bit[7] );
      printf(", ");     
      printf("%d",data_bit[8] );
      printf(",");
      printf("%d",data_bit[9] );
      printf(",");   
      printf("%d",data_bit[10] );
      printf(",");       
      printf("%d",data_bit[11] );
      printf(",");
      printf("%d",data_bit[12] );
      printf(",");   
      printf("%d",data_bit[13] );
      printf(",");       
      printf("%d",data_bit[14] );
      printf(","); 
      printf("%d",data_bit[15] );
      printf(", Humidity Linear=   "); 
      printf("%f", humidity_show1);
      printf(" %"); 
      printf("\r\n");   
      printf(" Humidity True=   ");
      printf("%f",   True_humidity_show1); // For senser V4
      printf("\r\n");   
      printf("**********Temp***************");
      printf("\r\n");         

      printf("%d",data_bit[0] );
      printf(",");   
      printf("%d",data_bit[1]);
      printf(",");       
      printf("%d",data_bit[2] );
      printf(","); 
      printf("%d",data_bit[3] );
      printf(",");
      printf("%d",data_bit[4] );
      printf(",");   
      printf("%d",data_bit[5] );
      printf(",");       
      printf("%d",data_bit[6] );
      printf(","); 
      printf("%d",data_bit[7] );
      printf(", ");     
      printf("%d",data_bit[8] );
      printf(",");
      printf("%d",data_bit[9] );
      printf(",");   
      printf("%d",data_bit[10] );
      printf(",");       
      printf("%d",data_bit[11] );
      printf(",");
      printf("%d",data_bit[12] );
      printf(",");   
      printf("%d",data_bit[13] );
      printf(",");       
      printf("%d",data_bit[14] );
      printf(","); 
      printf("%d",data_bit[15] );
      printf(", Actual=   "); 
      //printf("%f",temperature_show2 );
      printf("%d",data_bit[16]  );     
      printf("'C");
      printf("\r\n");
      printf(", Temp 22=   "); 
      printf("%f", temperature_show1);     
      printf("'C"); 
      printf("\r\n");     
   

}
//----------------------------------------------------------------------------------
void s_address(unsigned char val)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//      __              _____
// DATA:  |_____________|   
//          _    _    _       
// SCK : __| |__| |__| |__
{
     _nop_();
     DATA_out_LO();       
      _nop_();
      s_clk();   //0
      _nop_(); 
      s_clk();  //0
      _nop_();
      s_clk(); //0
      _nop_();
     
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//      __            __       __
// DATA:  |__________|  |_____|   
//          _    _    _    _    _    _       
// SCK : __| |__| |__| |__| |__| |__| |__     
   _nop_();
     DATA_out_LO();     //0 0
     s_clk();
     s_clk();
       
     if (val == 1)
     {
     DATA_out_HI();   //1 val ==1 for Humidity
     }
     else
     {
      DATA_out_LO();   //0   val ==0 for Temperature
     }
    s_clk();
if (val == 1)
     {
     DATA_out_LO();   //0    //0 val ==0 for Humidity
     }
     else
     {
      DATA_out_HI();    //0   val ==0 for Temperature
     }
     s_clk();
     DATA_out_HI();    //1
    s_clk();
   
   
    DATA_in_HI();   //Pull for ACK
    s_clk();
    _nop_(); 
   
wait_ack:
        if(DATA_in ==1)goto wait_ack;       //Wait for ACK

/// read MSB byte
                      for (bit_data = 0; bit_data < 8; bit_data++)
                {
                _nop_();
                data_bit[bit_data]=0;
                if(DATA_in ==1 )data_bit[bit_data]=1;
                   s_clk();                 
                }   
               
                DATA_out_LO();  //send ACK
                _nop_();
                _nop_();
                s_clk();
                DATA_in_HI();   
           
/// read LSB byte
               
           for (bit_data = 8; bit_data < 16; bit_data++)
                {
                _nop_();
                data_bit[bit_data]=0;
                if(DATA_in==1 )data_bit[bit_data]=1;
                s_clk();               
                }           
               
                DATA_out_LO();  //send ACK
                s_clk();
                DATA_in_HI(); 
               
/////////////// conver BCD to decimal

                data_bit[16]=0;

        for (bit_data = 0; bit_data < 15; bit_data++)
                {
                if(data_bit[bit_data] == 1)
                  {                                                   
                   data_bit[16]=data_bit[16] + 1;
                  }
                  else
                  {

                  }

                  data_bit[16]  <<= 1;//                     
                }
                   

                if (val ==1)humidity_show1 = (-6.5468 +(0.0367*data_bit[16] )+ ((-1.5955E-6*data_bit[16])*(-1.5955E-6*data_bit[16])));
                if (val ==0)temperature_show1= ( (0.01*data_bit[16] )-39.8);
                 True_humidity_show1= ((temperature_show1-25)*(0.01+(0.00008*humidity_show1))+humidity_show1);
// Whit off set  //printf("%f", (-6.5468 +(0.0367*data_bit[16] )+ ((-1.5955E-6*data_bit[16])*(-1.5955E-6*data_bit[16])))); // For senser V4
//printf("%f", (-2.0468 +(0.0367*data_bit[16] )+ ((-1.5955E-6*data_bit[16])*(-1.5955E-6*data_bit[16])))); // For senser V4
// printf("%f", (-4.0000 +(0.0405*data_bit[16] )+ ((-2.8000E-6*data_bit[16])*(-2.8000E-6*data_bit[16])))); // For senser V3


}

//////////////////////////////////////////////////////////////////


#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];

#if RX_BUFFER_SIZE <= 256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif

// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer[rx_wr_index++]=data;
#if RX_BUFFER_SIZE == 256
   // special case for receiver buffer size=256
   if (++rx_counter == 0) rx_buffer_overflow=1;
#else
   if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      }
#endif
   }
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index++];
#if RX_BUFFER_SIZE != 256
if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#endif
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

// TWI functions
#include <twi.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=P
PORTB=0x01;
DDRB=0x02;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=P
PORTD=0x01;
DDRD=0x06;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// Mode: TWI Master
// Bit Rate: 100 kHz
twi_master_init(100);

// Global enable interrupts
#asm("sei")
     s_connectionreset();
     
while (1)
      {

        s_connectionreset();        //reset sensor
        s_transstart();             //transmission start
        s_address(1);               //read Humidity
         delay_ms(800);          //(be sure that the compiler doesn't eliminate this line!)
        s_connectionreset();        //reset sensor
        s_transstart();             //transmission start
        s_address(0);                //read Temp
        print_serail_data_timing();   
       
       
       //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------     
        delay_ms(800);          //(be sure that the compiler doesn't eliminate this line!)
        //---------------------------------------------------------------
      }
}

Title: Re: ใครเคยลอง DHT11 กับ AVR บ้างครั้ง
Post by: avr_lover on February 05, 2013, 12:02:41 PM
โอ้ขอบคุณครับท่าน Jeng ผมรีบจัด ไม่ทันดู :)
Title: Re: ใครเคยลอง DHT11 กับ AVR บ้างครั้ง
Post by: avr_lover on February 05, 2013, 01:28:55 PM
แถมๆอันนี้น่าจะตรงตัวกว่านะ
http://www.spelecshop.com/1402480/404301/Temperature---Huminity-Sensor.html
:)