51 microcontroller CO2 detection display program analysis

Publisher:Changsheng520Latest update time:2023-06-26 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is a 51 microcontroller CO2 detection and display program data. The T (TXD) of the carbon dioxide sensor MH-Z19 module is connected to the RXD of the microcontroller, R (RXD) is connected to the TXD of the microcontroller, V+ is connected to the 5v positive pole of the microcontroller, and V- is connected to the ground of the microcontroller ( Serial TTL communication requires connecting these four lines); the measurement voltage is 5V; the microcontroller model is STC89C52, the liquid crystal display model is LCD1602, and the circuit connection is in accordance with the test diagram; the serial communication baud rate is 9600, pay attention to the crystal oscillator if it is 11.0592M, otherwise it will Causes communication failure; the sensor needs to warm up for 3 minutes when powered on.

51 microcontroller CO2 detection display program analysis

51 microcontroller CO2 detection display program analysis

#include "reg52.h"

#include "stdio.h"

#include "intrins.h"

#include "lcd1602.H"

#define uchar unsigned char

#define uint unsigned int

#define ulong unsigned long

uchar code tab[9]={0xff,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};

uchar date[]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

void delayms(uint xms)

{

uint x, y;

for(x=xms;x》0;x--)

for(y=110;y》0;y--);

}

void send(uchar dat) //Send one byte

{

SBUF=dat;

while(!TI);

TI=0;

}

void main(void)

{

uint tmp;

uchar i;

TMOD=0x20; // T1 timing mode 2

SCON=0x40;//Serial working mode 1

PCON=0x00; //Do not double

TH1=TL1=0xfd; //Assign an initial value to timer 1 to ensure the baud rate is 9600

TR1=1; //Open timer 1

REN=1; //Allow serial port reception

init_1602(); //LCD initialization

delayms(10);

ES=1; //Open serial port interrupt

EA=1; //Enable total interrupt

while(1)

{

for(i=0;i《9;i++) //Send data to co2 sensor

{

send(tab[i]);

}

delayms(1000); //Delay one second to refresh the measured data

tmp=date[2]*256+date[3]; //Calculate the co2 concentration value

if(tmp》9999)

tmp=0;

WRITE_LCD1602_COM(0x80+8);

if((tmp/1000)》0)

WRITE_LCD1602_DAT(LCD1602_Table[tmp/1000]);

else WRITE_LCD1602_DAT(' ');

WRITE_LCD1602_DAT(LCD1602_Table[tmp00/100]);

WRITE_LCD1602_DAT(LCD1602_Table[tmp0/10]);

WRITE_LCD1602_DAT(LCD1602_Table[tmp]);

}

}

void serial()interrupt 4 //Serial port interrupt service function

{

static uchar j;

ES=0; //Disable interrupts

if(!RI);

RI=0; //Clear receiving completion flag

date[j]=SBUF;

if(date[0]==0xff) //Judge that the first bit received is the data sent by the sensor.

j++;

else

j=0;

if (j==9) //Receive 9 bytes of data sent by the sensor

j=0;

ES=1; //Allow serial port interrupt

}

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

lcd1602.H:

#define uchar unsigned char

#define uint unsigned int

sbit RS=P2^5;

sbit RW=P2^6;

sbit EN=P2^7;

uchar LCD1602_Table[]=“0123456789”;

//LCD1602 display ruler//0123456789abcdef

uchar code DIS_TIM[16] = {“ PPM= ”}; //Display temperature and humidity

uchar code DIS_STA[16] = {“ Test is OK! ”}; //

uchar code DIS[16] = {“alarm ppm”}; //

extern void delayms(uint xms);

void WRITE_LCD1602_DAT(uchar date)

{

EN=0;

RS=1;

P0=date;

EN=1;

delayms(2);

EN=0;

}

void WRITE_LCD1602_COM(uchar date)

{

EN=0;

RS=0;

P0=date;

EN=1;

delayms(2);

EN=0;

}

void init_1602()

{

unsigned char i;

RW=0;

WRITE_LCD1602_COM(0x38);

WRITE_LCD1602_COM(0x0c);

WRITE_LCD1602_COM(0x06);

WRITE_LCD1602_COM(0x01);

WRITE_LCD1602_COM(0x80);

for(i=0;i《8;i++)

WRITE_LCD1602_DAT(DIS_TIM[i]);

}


Reference address:51 microcontroller CO2 detection display program analysis

Previous article:How to use 51 microcontroller to implement traffic lights
Next article:51 microcontroller entry light LED

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号