/************************************************************************
Program: ICC-AVR
Function: Baud rate 9600 Serial port sends temperature and humidity data
Data format: humidity temperature checksum For
example, the serial port sends data 59 00 24 00 53, then the humidity is 59, the temperature is 24, and the checksum is 53
Company: DHT11
Chip: M8
Crystal oscillator: external 7.3728Mhz
******************************************************** **********************/
#include
#include
typedef unsigned char U8; /* defined for unsigned 8-bits integer variable Unsigned 8-bit integer variable */
typedef signed char S8; /* defined for signed 8-bits integer variable Signed 8-bit integer variable */
typedef unsigned int U16; /* defined for unsigned 16-bit integer variable /* defined for signed 16-bit integer variable */
typedef signed int S16; /* defined for signed 16-bit integer variable */
typedef unsigned long U32; /* defined for unsigned 32-bit integer variable */
typedef signed long S32; /* defined for signed 32-bit integer variable */
typedef float F32; /* single precision floating point variable (32bits) Single precision floating point number (32-bit length) */
typedef double F64; /* double precision floating point variable (64bits) Double precision floating point number (64-bit length) */
/**************************************************************************
I/O space definition
*************************************************************************/
// B0_IN DDRB &= ~(1 << PB0) /*Set input*/
// B0_OUT DDRB |= (1 << PB0) /*Set output*/
// B0_CLR PORTB &= ~(1 << PB0) /*Set low level*/
// B0_SET PORTB |= (1 << PB0) /*Set high level*/
// B0_R PINB & (1 <
#define C4_OUT DDRC |= (1 << PC4) /*Set output*/
#define C4_CLR PORTC &= ~(1 << PC4) /*Set low level*/
#define C4_SET PORTC |= (1 << PC4) /*Set high level*/
#define C4_R PINC & (1 <
Communication I/O space definition
***********************************************************************/
#define COM_IN C4_IN
#define COM_OUT C4_OUT
#define COM_CLR C4_CLR
#define COM_SET C4_SET
#define COM_R C4_R
/******************************************************************************
Global variable definition area
*******************************************************************/
U8 i;
U8 U8FLAG,k;
U8 U8count,U8temp;
U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8 U8comdata;
U16 ReceiveHighByte;
U16 ReceiveLowByte;
/**************************************************************************
***************************************************************************/
void port_init(void)
{
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x03;
DDRC = 0x0C;
PORTD = 0x00;
DDRD = 0xF0;
}
void watchdog_init(void)
{
//WDR(); //this prevents a timout on enabling
//WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}
void Delay(unsigned int i)
{
while(i--);
}
void DelaymS (U16 dly)
{
U16 i;
for ( ; dly>0; dly--)
{
WDR();
for (i=0; i<1032; i++);
}
}
void Delay_10us(void)
{
U8 i;
i++;i++;i++;i++;i++;i++;i++;i++;i++;
i++;i++;i++;i++;i++;i++;i++;i++;i++;
i++;i++;i++;i++;i++;i++;i++;i++;i++;
i++;i++;i++;i++;i++;i++;i++;i++;i++;
i++;i++;i++;i++;i++;i++;i++;i++;i++;
i++;i++;i++;i++;i++;i++;i++;i++;i++;
i++;i++;i++;i++;i++;i++;i++;i++;i++;
i++;i++;
}
void COM(void)
{
U8 i;
for(i=0;i<8;i++)
{
U8FLAG=2;
while((!(COM_R))&&U8FLAG++);
if(U8FLAG==1)break;
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
U8temp=0;
if(COM_R)U8temp=1;
U8FLAG=2;
while((COM_R)&&U8FLAG++);
//超时则跳出for循环
if(U8FLAG==1)break;
//判断数据位是0还是1
// 如果高电平高过预定0高电平值则数据位为 1
U8comdata<<=1;
U8comdata|=U8temp; //0
}//rof
}
//--------------------------------
//-----湿度读取子程序 ------------
//--------------------------------
//----以下变量均为全局变量--------
//----温度高8位== U8T_data_H------
//----温度低8位== U8T_data_L------
//----湿度高8位== U8RH_data_H-----
//----湿度低8位== U8RH_data_L-----
//----校验 8位 == U8checkdata-----
//----调用相关子程序如下----------
//---- Delay();, Delay_10us();,COM();
//--------------------------------
void RH(void)
{
//Host pulls down 18ms
COM_OUT;
COM_CLR;
DelaymS(18);
COM_IN;
COM_SET;
//The bus is pulled up by the pull-up resistor and the host delays 20us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
//Host is set as input to judge the response signal of the slave
//P2_0=1;
//Judge whether the slave has a low-level response signal. If it does not respond, it will jump out. If it responds, it will run downward.
// if(!P2_0) //T !
if(COM_R)U8FLAG=1;
while(!(COM_R))
{
U8FLAG=2;
//Judge whether the slave sends a low-level response signal of 80us and whether it ends
//while((!P2_0)&&U8FLAG++);
while((!(COM_R))&&U8FLAG++);
if(U8FLAG==1)break;
U8FLAG=2;
//Judge whether the slave sends out a high level of 80us. If so, enter the data receiving state.
//while((P2_0)&&U8FLAG++);
while((COM_R)&&U8FLAG++);
if(U8FLAG==1)break;
//Data receiving state
COM();
if(U8FLAG==1)break;
U8T_data_H_temp=U8comdata;
COM();
if(U8FLAG==1)break;
U8T_data_L_temp=U8comdata;
COM();
if(U8FLAG==1)break;
U8RH_data_H_temp=U8comdata;
COM();
if(U8FLAG==1)break;
U8RH_data_L_temp=U8comdata;
COM();
if(U8FLAG==1)break;
U8checkdata_temp=U8comdata;
break;
}//while
//P2_0=1;
COM_IN;
COM_SET;
//Data check
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
{
U8RH_data_H=U8T_data_H_temp;
U8RH_data_L=U8T_data_L_temp;
U8T_data_H=U8RH_data_H_temp;
U8T_data_L=U8RH_data_L_temp;
U8checkdata=U8checkdata_temp;
}//fi
COM_IN;
COM_SET;
}
/*
Serial port initialization function
System frequency: 2MHz
Stop bit: 1
Baud rate: 9600
Data length: 8
*/
void Uart_Init(void) {
UCSRA = 0x02; /*2x speed */
UCSRB = 0x18; /*Allow receiving and sending*/
UCSRC = 0x06; /*8-bit data */
UBRRH = 0x00;
//UBRRL = 47; /*9600 3.6864M */
UBRRL = 95; /*9600 7.3728M */
}
/*
Send string data in
query mode
*/
void Uart_Transmit(unsigned char i) {
/* Wait for the send buffer to be empty*/
while (!(UCSRA & (1<
UDR = i;
}
/*
Receive string data
in query mode
*/
unsigned char Uart_Receive( void ) {
/* Wait for receiving data*/
while (!(UCSRA & (1<
// Uart_Transmit(UDR);
return UDR;
}
void init_devices(void)
{
unsigned char i;
CLI();
port_init();
watchdog_init();
Uart_Init();
MCUCR = 0x03;
GICR = 0x00; // Turn off external interrupt 0
TIMSK = 0x00; // Turn off timer 1 interrupt
SEI(); //re-enable interrupts
}
/******************************************************************************
Main function
**************************************************************************/
void main(void)
{
init_devices() ;
COM_IN;
COM_SET;
while(1)
{
//Read DHT11
RH();
//Convert the hexadecimal data of DHT11 into decimal format
//Humidity Temperature Checksum
//For example, if the serial port sends data 59 00 24 00 53, the humidity is 59, the temperature is 24, and the checksum is 53
U8temp=U8RH_data_H/10;
U8temp<<=4;
U8temp|=U8RH_data_H%10;
U8RH_data_H=U8temp;
Uart_Transmit(U8RH_data_H);
Uart_Transmit(U8RH_data_L);
U8temp=U8T_data_H/10;
U8temp<<=4;
U8temp|=U8T_data_H%10;
U8T_data_H=U8temp;
Uart_Transmit(U8T_data_H);
Uart_Transmit(U8T_data_L);
Uart_Transmit(U8checkdata);
//Delay 2S
for(U8temp=0;U8temp<200;U8temp++)
{
DelaymS(10);
}//rof
}
}
Previous article:avr 12864 matrix keyboard
Next article:How to sample 8-channel AD in AVR microcontroller
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Keysight Technologies Live Review: Oscilloscope Basics Training
- Remote control jog problem
- How to solve the problem of field effect tube heating when used as a switch
- How to configure slave device registers via SPI in tms57ls
- [RVB2601 Creative Application Development] 9~11 Game End Restart and Optimize Code, etc.
- Evaluation of domestic FPGA Gaoyun GW1N-4 series development board - stepper motor control (first edition)
- [Rapoo V10RGB Colorful Backlight Gaming Mouse Hard Pad Disassembly Review] Disassembly First
- Ideas for Intelligent Renovation of College Dormitories Using MSP430 Single Chip Microcomputer
- micropython update: 2020.8
- How much do you need to learn STM32 to get started? There are too many things I am not used to when switching from 51 to STM32.