用52单片机跟gy39加上lcd显示做一个光强度检测系统要求lcd上能显示湿度值跟光强度值给出代码
时间: 2024-01-12 12:02:26 浏览: 69
这是一个基于52单片机和GY39模块的光强度检测系统,可以在LCD上显示湿度值和光强度值。以下是代码:
```
#include <reg52.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LCD_DATA P0
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_EN P2_2
sbit SDA = P3^4;
sbit SCL = P3^5;
unsigned char code InitData[]={0x38,0x0c,0x06,0x01,0x80};
unsigned char code LCDChar[]={"0123456789*#"};
unsigned char code LCDLine[] ={0x80,0xc0,0x90,0xd0};
unsigned char code LCDNum[][] = {
{"Humidity:"},
{"Intensity:"}
};
unsigned char Humidity[] = {"000 %"};
unsigned char Intensity[] = {"000 Lux"};
unsigned char buffer[16];
void Delay1ms(unsigned int time);
void IIC_Start();
void IIC_Stop();
void IIC_SendACK(bit ack);
bit IIC_RecvACK();
void IIC_SendByte(unsigned char dat);
unsigned char IIC_RecvByte();
void InitLCD();
void WriteCommand(unsigned char com);
void WriteData(unsigned char dat);
void WriteString(unsigned char line, unsigned char *str);
void WriteNumber(unsigned char line, unsigned char *str);
unsigned int humidity = 0;
unsigned int intensity = 0;
void main()
{
unsigned char i;
TMOD &= 0xf0;
TMOD |= 0x01;
TH0 = 0xfc;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
InitLCD();
while(1)
{
IIC_Start();
IIC_SendByte(0x88);
IIC_SendByte(0x00);
IIC_Stop();
Delay1ms(500);
IIC_Start();
IIC_SendByte(0x89);
humidity = (IIC_RecvByte() << 8) | IIC_RecvByte();
IIC_SendACK(0);
intensity = (IIC_RecvByte() << 8) | IIC_RecvByte();
IIC_SendACK(0);
IIC_Stop();
sprintf(buffer, "%03d", humidity / 10);
memcpy(Humidity, buffer, 3);
WriteNumber(0, Humidity);
sprintf(buffer, "%03d", intensity / 10);
memcpy(Intensity, buffer, 3);
WriteNumber(1, Intensity);
Delay1ms(500);
}
}
void Timer0() interrupt 1
{
TH0 = 0xfc;
TL0 = 0x67;
}
void Delay1ms(unsigned int time)
{
unsigned int i, j;
for(i=0; i<time; i++)
for(j=0; j<110; j++);
}
void IIC_Start()
{
SDA = 1;
SCL = 1;
Delay1ms(1);
SDA = 0;
Delay1ms(1);
SCL = 0;
}
void IIC_Stop()
{
SDA = 0;
SCL = 1;
Delay1ms(1);
SDA = 1;
Delay1ms(1);
}
void IIC_SendACK(bit ack)
{
SDA = ack;
SCL = 1;
Delay1ms(1);
SCL = 0;
Delay1ms(1);
}
bit IIC_RecvACK()
{
bit ack;
SCL = 1;
Delay1ms(1);
ack = SDA;
SCL = 0;
Delay1ms(1);
return ack;
}
void IIC_SendByte(unsigned char dat)
{
unsigned char i;
for(i=0; i<8; i++)
{
SDA = dat & 0x80;
SCL = 1;
Delay1ms(1);
SCL = 0;
dat <<= 1;
}
IIC_RecvACK();
}
unsigned char IIC_RecvByte()
{
unsigned char i, dat = 0;
SDA = 1;
for(i=0; i<8; i++)
{
SCL = 1;
Delay1ms(1);
dat <<= 1;
dat |= SDA;
SCL = 0;
Delay1ms(1);
}
return dat;
}
void InitLCD()
{
unsigned char i;
for(i=0; i<5; i++)
WriteCommand(InitData[i]);
}
void WriteCommand(unsigned char com)
{
LCD_RS = 0;
LCD_DATA = com;
LCD_EN = 1;
Delay1ms(1);
LCD_EN = 0;
}
void WriteData(unsigned char dat)
{
LCD_RS = 1;
LCD_DATA = dat;
LCD_EN = 1;
Delay1ms(1);
LCD_EN = 0;
}
void WriteString(unsigned char line, unsigned char *str)
{
WriteCommand(LCDLine[line]);
while(*str != '\0')
WriteData(*str++);
}
void WriteNumber(unsigned char line, unsigned char *str)
{
unsigned char i;
WriteCommand(LCDLine[line]);
for(i=0; i<10; i++)
WriteData(LCDChar[str[i] - '0']);
WriteData(' ');
WriteString(line, LCDNum[line]);
}
```
请注意,此代码未经过实际测试,可能会存在一些错误或问题。您需要根据实际情况进行调整和修改。此外,该代码仅供参考,您需要对其进行深入理解,并根据自己的需求进行修改。
阅读全文
相关推荐
















