51单片机mq7传感器在1602显示浓度代码
时间: 2023-10-31 12:04:35 浏览: 156
以下是基于51单片机和1602液晶屏幕的MQ7传感器浓度显示代码:
```
#include <reg51.h>
#include <intrins.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_EN = P2^2;
sbit MQ7_DO = P3^5;
unsigned int ADC_Value;
void delay_ms(unsigned int ms)
{
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<114; j++);
}
void LCD1602_WriteCommand(unsigned char Command)
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_EN = 1;
_nop_();
LCD1602_DB = Command;
_nop_();
LCD1602_EN = 0;
delay_ms(2);
}
void LCD1602_WriteData(unsigned char Data)
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_EN = 1;
_nop_();
LCD1602_DB = Data;
_nop_();
LCD1602_EN = 0;
delay_ms(2);
}
void LCD1602_Init()
{
LCD1602_WriteCommand(0x38);
LCD1602_WriteCommand(0x0c);
LCD1602_WriteCommand(0x06);
LCD1602_WriteCommand(0x01);
}
void ADC_Init()
{
AUXR |= 0x10;
ADC_CONTR = 0x81;
}
unsigned int ADC_Read(unsigned char Channel)
{
ADC_CONTR = 0x81 | (Channel << 3);
delay_ms(2);
ADC_CONTR |= 0x10;
while(!(ADC_CONTR & 0x20));
ADC_CONTR &= ~0x20;
return ADC_RES;
}
void main()
{
unsigned char High, Low;
float value;
ADC_Init();
LCD1602_Init();
LCD1602_WriteCommand(0x80);
LCD1602_WriteData('M');
LCD1602_WriteData('Q');
LCD1602_WriteData('7');
LCD1602_WriteCommand(0xc0);
LCD1602_WriteData('C');
LCD1602_WriteData('o');
LCD1602_WriteData('n');
LCD1602_WriteData('c');
LCD1602_WriteData('e');
LCD1602_WriteData('n');
LCD1602_WriteData('t');
delay_ms(2000);
while(1)
{
High = ADC_Read(0);
Low = ADC_Read(1);
ADC_Value = (High << 8) | Low;
value = ADC_Value * 5.0 / 1023;
LCD1602_WriteCommand(0x80);
LCD1602_WriteData('C');
LCD1602_WriteData('o');
LCD1602_WriteData('n');
LCD1602_WriteData('c');
LCD1602_WriteData(':');
LCD1602_WriteData(' ');
LCD1602_WriteData((int)value/10 + '0');
LCD1602_WriteData((int)value%10 + '0');
LCD1602_WriteData('p');
LCD1602_WriteData('p');
LCD1602_WriteData('m');
delay_ms(1000);
}
}
```
上述代码中,我们使用了51单片机的ADC模块读取了MQ7传感器的模拟信号,并将其转化为数字信号进行处理。同时,我们通过1602液晶屏幕实时显示MQ7传感器检测到的浓度值。需要注意的是,由于MQ7传感器输出的是模拟信号,因此需要通过ADC模块进行转化,才能得到准确的浓度值。
阅读全文