Smart home control temperature, light and gas detection based on 51 single chip microcomputer

Publisher:风轻迟Latest update time:2021-12-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

hardware design

A. The design is based on 51 single chip microcomputer for smart home control, temperature, light and air monitoring;

B. LCD1602 is used as the display, DS18B20 is used for temperature, photodiode is used for illumination, and it has an audible and visual alarm device. The threshold can be set through three buttons;


The simulation diagram is as follows:

insert image description here

programming:

#include //Include header file, generally no need to change, the header file contains the definition of special function registers

#include "intrins.h"    


#define u8  unsigned char

#define u16    unsigned int

#define uchar  unsigned char

#define uint    unsigned int


uchar yushe_wendu=50; //temperature preset value

uchar yushe_yanwu=100; //Lighting preset value

uchar yushe_tianranqi=100; //Natural gas preset value

uint wendu; //temperature value global variable

uchar yanwu; //Used to read ADC data

uchar tianranqi; //Used to read ADC data

uint times = 0; //Variable times, count, used for LCD switching display

uchar shezhi_flag = 0;    //Variable flag, used to switch the display setting of natural gas preset value


//Running mode  

uchar Mode=0; //=1 is to set the temperature threshold =2 is to set the light threshold =0 is the normal monitoring mode

uint Buzzer=0;

uint count = 0;

//Pin declaration

sbit Led_Yellow =P2^4; //yellow light

sbit speaker    =P2^0; //buzzer



sbit cg=P2^3; //main switch

sbit c1_k=P1^1;    //Curtain 1 switch

sbit c2_k=P1^4;   //Curtain 2 switch



/********************************************************************

* Name: delay_1ms()

* Function: Delay 1ms function

* Input: q

* Output: None

***************************************************************************/

void delay_ms(uint q)

{

uint i,j;

for(i=0;i for(j=0;j<110;j++);

}

/***************************************************************************************************************

LCD1602 related functions

*******************************************************************************************************************/


//LCD pin declaration (RW pin is directly grounded, because this design only uses LCD write operation, RW pin is always low level)

sbit LCDRS = P2^7;

sbit LCDEN = P2^6;

sbit D0 = P0^0;

sbit D1 = P0^1;

sbit D2 = P0^2;

sbit D3 = P0^3;

sbit D4 = P0^4;

sbit D5 = P0^5;

sbit D6 = P0^6;

sbit D7 = P0^7;




//LCD delay

void LCDdelay(uint z)   //The delay is about 100us (not precise, the delay of LCD operation does not require very precise)

{

  uint x,y;

  for(x=z;x>0;x--)

    for(y=10;y>0;y--);

}

void LCD_WriteData(u8 dat)   

{

if(dat&0x01)D0=1;else D0=0;

if(dat&0x02)D1=1;else D1=0;

if(dat&0x04)D2=1;else D2=0;

if(dat&0x08)D3=1;else D3=0;

if(dat&0x10)D4=1;else D4=0;

if(dat&0x20)D5=1;else D5=0;

if(dat&0x40)D6=1;else D6=0;

if(dat&0x80)D7=1;else D7=0;

}

//Write command

void write_com(uchar com)

{

  LCDRS=0;   

LCD_WriteData(com);

// DAT=com;

  LCDdelay(5);

  LCDEN=1;

  LCDdelay(5);

  LCDEN=0;

}

//Write data

void write_data(uchar date)

{

  LCDRS=1;

LCD_WriteData(date);

// DAT=date;

  LCDdelay(5);

  LCDEN=1;

  LCDdelay(5);

  LCDEN=0;

}


/*------------------------------------------------

              Select write location

------------------------------------------------*/

void SelectPosition(unsigned char x,unsigned char y) 

{     

if (x == 0) 

{     

write_com(0x80 + y); // indicates the first line

}

else 

{      

write_com(0xC0 + y); // indicates the second line

}        

}

/*------------------------------------------------

              Write String Function

------------------------------------------------*/

void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) 

{     

SelectPosition(x,y) ;

while (*s) 

{     

write_data( *s);     

s++;     

}

}


Link: https://pan.baidu.com/s/1hfpynLhRsGrz7ThPg0pnIw

Withdraw code: 9oo8

Reference address:Smart home control temperature, light and gas detection based on 51 single chip microcomputer

Previous article:Traffic light control system course design based on 51 single chip microcomputer
Next article:Two-phase stepping motor control system based on 51 single chip microcomputer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号