基于51单片机的MQ2可燃性气体监测+液晶LCD1602显示
程序源代码
/**************MQ2可燃性气体监测+液晶LCD1602显示程序源代码***************
单片机型号:STC15W4K56S4,内部晶振:22.1184M。
功能:MQ2可燃性气体监测+液晶LCD1602显示功能测试。
操作说明:
利用MQ2传感器监测可燃性气体含量,并将可燃性气体含量数值显示在液晶LCD1602上。**************************************************************************/
#include "stc15.h"                                  //包含头文件stc15.h
#include <intrins.h> //包含头文件intrins.h
#include <math.h>        //包含头文件math.h
#define Busy 0x80 //LCD忙
sbit LCD_D0 = P0^0;                      //LCD_D0对应P0.0
sbit LCD_D1 = P0^1;                      //LCD_D1对应P0.1
sbit LCD_D2 = P0^2;                      //LCD_D2对应P0.2
sbit LCD_D3 = P0^3;                          //LCD_D3对应P0.3
sbit LCD_D4 = P0^4;                      //LCD_D4对应P0.4
sbit LCD_D5 = P0^5;                      //LCD_D5对应P0.5
sbit LCD_D6 = P0^6;                      //LCD_D6对应P0.6
sbit LCD_D7 = P0^7;                          //LCD_D7对应P0.7
sbit LCD_RS = P1^0;                      //LCD_RS对应P1.0
sbit LCD_RW = P1^1;                      //LCD_RW对应P1.1
sbit LCD_EN = P3^4;                      //LCD_EN对应P3.4
void delay(unsigned int t);      //delay延时函数
void delay_us(unsigned int t);          //delay_us延时函数
void delay_ms(unsigned int t);              //delay_ms延时函数
void Delay5Ms(void);    //5Ms延时函数
void GPIO_1602_Configuration(void);  //LCD1602液晶IO口初始化void WriteDataLCD(unsigned char WDLCD);  //LCD写数据函数
void WriteCommandLCD(unsigned char WCLCD,BuysC); //LCD写命令函数
unsigned char ReadDataLCD(void); //LCD读数据函数
unsigned char ReadStatusLCD(void); //LCD读状态函数
void LCDInit(void);  //LCD初始化
void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData);
//LCD显示一个字符
void DisplayListChar(unsigned char X,unsigned char Y,unsigned char code *DData); //LCD显示一个字符串
#define ADC_POWER  0x80                          //将0x80宏定义成ADC_POWER
#define ADC_FLAG    0x10    //将0x10宏定义成ADC_FLAG
#define ADC_START  0x08    //将0x08宏定义成ADC_START #define ADC_SPEEDLL 0x00    //将0x03宏定义成ADC_SPEEDLL #define ADC_SPEEDL  0x20    //将0x20宏定义成ADC_SPEEDL #define ADC_SPEEDH  0x40    //将0x40宏定义成ADC_SPEEDH #define ADC_SPEEDHH 0x60    //将0x60宏定义成ADC_SPEEDHH
void MQ2_INIT(void);    //MQ2初始化
unsigned char GetMQ2Result(unsigned char ch);      //获取MQ2值
unsigned char code TITLE[] = {"MQ-2"};    //LCD显示内容MQ2
unsigned char code MQ2[]  = {"MQ2:    PPM"};    //LCD显示内容MQ2:    PPM float MQ2_DATA;
void delay(unsigned int t)          //delay延时函数
{while(t--);}
void delay_us(unsigned int t)        //delay_us延时函数
{
unsigned char i;
while(t--)
{
i = 3;
while(i--) delay(1);
}
}
void delay_ms(unsigned int t)            //delay_ms延时函数
{
while(t--)
{
delay_us(t);
}
}
void Delay5Ms(void)    //5ms延时函数
{
unsigned int TempCyc = 3552;
while(TempCyc--);
}
void MQ2_INIT(void)    //MQ2初始化
{
P1ASF = 0x08;
ADC_RES = 0;
ADC_CONTR = ADC_POWER|ADC_SPEEDLL;
}
unsigned char GetMQ2Result(unsigned char ch)            //获取MQ2值{
ADC_CONTR = ADC_POWER|ADC_SPEEDLL|ch|ADC_START;
delay(4);
while(!(ADC_CONTR&ADC_FLAG));
ADC_CONTR&=~ADC_FLAG;
return ADC_RES;
}
void Mq2_value(void)    //获取MQ2值{
u8 Mq2_RESULT;
float Mq2_temp;
float ppm1;
unsigned char Mq2_disbuff[5]={0,0,0,0,0};
Mq2_RESULT = GetMQ2Result(0x03);                      //读取电压值 Mq2_temp =((float)Mq2_RESULT/256)*5*2;                //数值转换
ppm1 = pow(11.5428*35.904*Mq2_temp/(25.5-5.1*Mq2_temp),1.0/0.6549);  MQ2_DATA = ppm1;
Mq2_disbuff[4]=(unsigned int)ppm1/10000+0x30;
Mq2_disbuff[3]=(unsigned int)ppm1/1000%10+0x30;
Mq2_disbuff[2]=(unsigned int)ppm1/100%10+0x30;
Mq2_disbuff[1]=(unsigned int)ppm1/10%10+0x30;
Mq2_disbuff[0]=(unsigned int)ppm1%10+0x30;
DisplayOneChar(4,1,Mq2_disbuff[4]);
delay_ms(10);                                        //延时
DisplayOneChar(5,1,Mq2_disbuff[3]);
delay_ms(10);                                        //延时
DisplayOneChar(6,1,Mq2_disbuff[2]);
4k电影源代码delay_ms(10);                                        //延时
DisplayOneChar(7,1,Mq2_disbuff[1]);
delay_ms(10);                                        //延时
DisplayOneChar(8,1,Mq2_disbuff[0]);
delay_ms(10);                                        //延时
}
void GPIO_1602_Configuration(void)                  //LCD1602液晶IO口初始化{
P0M1 = P3M1&0x00;
P0M0 = P3M0&0x00;
P1M1 = P3M1&0xfc;
P1M0 = P3M0&0xfc;
P3M1 = P4M1&0xef;
P3M0 = P4M0&0xef;
}
unsigned char ReadStatusLCD(void)            //测试LCD忙碌状态
{
LCD_D7 = 1;        //LCD的D7置1
LCD_RS = 0;        //LCD管脚RS设置成低电平 LCD_RW = 1;        //LCD管脚RW设置成高电平 LCD_EN = 0;            //LCD管脚E设置成低电平 LCD_EN = 0;        //LCD管脚E设置成低电平 LCD_EN = 1;          //LCD管脚E设置成高电平 while(LCD_D7);          //检测忙信号
return(Busy);        //表示当前忙
}
void WriteCommandLCD(unsigned char WCLCD,BuysC)    //BuysC为0时忽略忙检测{
if(BuysC) ReadStatusLCD();                        //根据需要检测忙
LCD_EN = 0;                                        //LCD管脚E设置成低电平 _nop_(); //空操作,延时
_nop_();                                        //空操作,延时
_nop_();                                        //空操作,延时
_nop_();                                    //空操作,延时
_nop_();                                    //空操作,延时
_nop_();                                    //空操作,延时
_nop_();                                    //空操作,延时
_nop_();                                    //空操作,延时
LCD_RS = 0; //LCD管脚RS设置成低电平 LCD_RW = 0; //LCD管脚RW设置成低电平 _nop_();    //空操作,延时
_nop_();                                          //空操作,延时
P0 = WCLCD;                                        //将数据送入P0口
_nop_();                //空操作,延时
_nop_();    //空操作,延时
_nop_();    //空操作,延时
_nop_();                                          //空操作,延时
LCD_EN = 1;                                        //E置高电平
_nop_();    //空操作,延时
_nop_();    //空操作,延时
_nop_();    //空操作,延时
_nop_();                                          //空操作,延时
LCD_EN = 0;
//当E由高电平跳变成低电平时,液晶模块开始执行命令
}
void WriteDataLCD(unsigned char WDLCD) //LCD写数据函数
{
ReadStatusLCD(); //读取LCD状态
LCD_EN  = 0;                                      //LCD管脚E设置成低电平 _nop_(); //空操作,延时
_nop_(); //空操作,延时
_nop_(); //空操作,延时
_nop_(); //空操作,延时
_nop_(); //空操作,延时
_nop_(); //空操作,延时
_nop_(); //空操作,延时
_nop_(); //空操作,延时
LCD_RS = 1;  //LCD管脚RS设置成高电平 LCD_RW = 0; //LCD管脚RW设置成低电平 P0 = WDLCD;                                        //将数据送入P0口
_nop_();    //空操作,延时
_nop_();      //空操作,延时
_nop_();        //空操作,延时
_nop_();                                          //空操作,延时
LCD_EN = 1;                                        //E置高电平
_nop_();        //空操作,延时
_nop_(); //空操作,延时
_nop_();    //空操作,延时
_nop_();                                          //空操作,延时
LCD_EN = 0;
//当E由高电平跳变成低电平时,液晶模块开始执行命令
}
void LCDInit(void)                                  //LCD初始化
{
WriteCommandLCD(0x38,0);                          //三次显示模式设置
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();