This is a 51 single-chip CO2 detection display program data. The T (TXD) of the carbon dioxide sensor MH-Z19 module is connected to the RXD of the single-chip microcomputer, the R (RXD) is connected to the TXD of the single-chip microcomputer, the V+ is connected to the 5v positive pole of the single-chip microcomputer, and the V- is connected to the ground of the single-chip microcomputer (serial TTL communication requires connecting these four wires); the measurement voltage is 5V; the single-chip microcomputer model is STC89C52, the LCD display model is LCD1602, and the circuit connection is according to the test diagram; the serial communication baud rate is 9600, and the crystal oscillator must be 11.0592M, otherwise it will cause communication failure; the sensor needs to be preheated 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 long 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(!IF);
IF = 0;
}
void main(void)
{
uint tmp;
flying i;
TMOD=0x20; // T1 timing mode 2
SCON=0x40; //Serial working mode 1
PCON=0x00; //Not doubled
TH1=TL1=0xfd; //Assign initial value to timer 1 to ensure baud rate is 9600
TR1=1; //Start timer 1
REN=1; // Allow serial port to receive
init_1602(); //LCD initialization
delayms(10);
ES=1; //Open serial port interrupt
EA=1; //Open the general interrupt
while(1)
{
for(i=0;i<9;i++) //Send data to the 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
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 reception completion mark
date[j]=SBUF;
if(date[0]==0xff) //Judge the first received bit as 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; //Enable 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 scale //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:Single chip microcomputer DTH11 temperature and humidity measurement simulation
Next article:51 single chip washing machine control board and C language program
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- FPGA controls TMS320C6678 power-on reset program
- 【TGF4042 signal generator】+6th issue pwm modulation
- Can PCB boards be replaced? Everything you want to know is here!
- Transparent LED display classification
- 2019 TI E-Sports Evaluation Table
- STM32F4, how to deal with unused pins? Can they be left floating?
- EEWORLD University Hall----E2E Chinese Forum Engineer Sharing- New Features and Applications of Bluetooth 5
- EEWORLD University ---- Live Replay: Microchip Timberwolf? Audio Processor Online Seminar
- [This week's topic] What kind of questions will be asked in the control class this year? Bonus!
- How to tell whether an operational amplifier is a current feedback type or a voltage feedback type