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.
#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]);
}
Previous article:How to use 51 microcontroller to implement traffic lights
Next article:51 microcontroller entry light LED
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- ADXL345 sensor acceleration detection in vibration environment
- Contactless facial recognition access control system
- Look! Moderator benefits have increased again! New rules for moderators announced
- Basic knowledge of NFC architecture and standards involved
- Calculation and setting of baud rate of msp430 microcontroller
- What challenges do you face in powering handling and harvesting robots? Let’s talk with Vicor engineers!
- Does DDR design require back drilling?
- [Integrated subway security control system based on Raspberry Pi 400] Material unboxing - Raspberry Pi 400
- SinA332.0 development board Linux & Qt adjust VGA resolution to 1280x720
- Single chip microcomputer CPU card programming system main frequency setting