#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#include "delay.h"
#include "1602.h"
unsigned char Rx_buf[4],Rxnum=3,temp=21,rh=23;
sbit wei02 = P1^0;
sbit wei03 = P1^1;
sbit BEEP = P1^3;
sbit led01 = P1^4;
sbit led02 = P1^5;
sbit led03 = P1^6;
sbit jiashi = P1^2;
xdata unsigned char dis0[16];//定义显示区域临时存储数组
unsigned char Tx_Buf[12];
unsigned char i;
unsigned char LEDstatus;
unsigned char weiFlag01 = 0;
unsigned char weiFlag02 = 0;
unsigned char weiFlag03 = 0;
unsigned char ZYNum = 0;
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
void UART_send_byte(char dat)
{
SBUF = dat;
while (TI == 0);
TI = 0;
}
/*
* UART 发送字符串
*/
void UART_send_string(unsigned char *buf)
{
while (*buf != '\0')
{
UART_send_byte(*buf++);
}
}
void main(void)
{
unsigned char Tx_Buf[12];
unsigned char LEDstatus;//灯的状态
InitUART(); //初始化串口
LCD_Init(); //初始化液晶
DelayMs(20); //延时有助于稳定
LCD_Clear(); //清屏
while(1)
{
LCD_Write_String(0,0," Feeder ");
LCD_Write_String(0,1,"Low:");
LCD_Write_String(8,1,"High:");
if(wei02 == 0)
{weiFlag02 = 1;led01 = 0; }
else
{weiFlag02 = 0;led01 = 1; }
if(wei03 == 0)
{weiFlag03 = 1;led02 = 0; }
else
{weiFlag03 = 0;led02 = 1; }
if(weiFlag02 == 1)
{LCD_Write_String(4,1,"Y ");
BEEP=1;
led03=1;
}
else
{ LCD_Write_String(4,1,"N ");
BEEP=0;
led03=0;
}
if(wei02 == 1 )
{
jiashi=0;
}
else if(wei02 == 0&& wei03 == 0)
jiashi=1;
if(weiFlag03 == 1)
{LCD_Write_String(13,1,"Y ");}
else
{LCD_Write_String(13,1,"N ");}
Tx_Buf[0]='T';
Tx_Buf[1]=0+0x30;
Tx_Buf[2]=0+0x30;
Tx_Buf[3]=0+0x30;
Tx_Buf[4]=0+0x30;
Tx_Buf[5]=0+0x30;
Tx_Buf[6]=0+0x30;
Tx_Buf[7]=2+0x30;
LEDstatus=jiashi;
Tx_Buf[8]=LEDstatus+0x30;
LEDstatus=led01;
Tx_Buf[9]=LEDstatus+0x30;
LEDstatus=led02;
Tx_Buf[10]=LEDstatus+0x30;
Tx_Buf[11]=3+0x30;
UART_send_string(Tx_Buf);
DelayMs(1000);
}
}
#include "1602.h"
#include "delay.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define _NOP() _nop_()
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
#define DataPort P0
#define DataPIN P0
#define CLR_RS (RS=0)
#define SET_RS (RS=1)
#define CLR_RW (RW=0)
#define SET_RW (RW=1)
#define CLR_EN (EN=0)
#define SET_EN (EN=1)
void DispStr(uchar x,uchar y,uchar *ptr)
{
uchar *temp;
uchar i,n = 0;
temp = ptr;
while(*ptr++ != '\0') n++; //计算字符串有效字符的个数
for (i=0;i<n;i++)
{
Disp1Char(x++,y,temp[i]);
if (x == 0x10)
{
break;
}
}
}
void DispNChar(uchar x,uchar y, uchar n,uchar *ptr)
{
uchar i;
for (i=0;i<n;i++)
{
Disp1Char(x++,y,ptr[i]);
if (x == 0x10)
{
x = 0;
y ^= 1;
}
}
}
void LocateXY(uchar x,uchar y)
{
uchar temp;
temp = x&0x0f;
y &= 0x01;
if(y) temp |= 0x40; //如果在第2行
temp |= 0x80;
LcdWriteCommand(temp,1);
}
void Disp1Char(uchar x,uchar y,uchar data1)
{
LocateXY( x, y );
LcdWriteData( data1 );
}
void LcdReset(void)
{
LcdWriteCommand(0x38, 0); //规定的复位操作
DelayMs(5);
LcdWriteCommand(0x38, 0);
DelayMs(5);
LcdWriteCommand(0x38, 0);
DelayMs(5);
LcdWriteCommand(0x38, 1); //显示模式设置
LcdWriteCommand(0x08, 1); //显示关闭
LcdWriteCommand(0x01, 1); //显示清屏
LcdWriteCommand(0x06, 1); //写字符时整体不移动
LcdWriteCommand(0x0c, 1); //显示开,不开游标,不闪烁
}
void LcdClear(void)
{
LcdWriteCommand(0x01,1);
DelayMs(5);
}
void LcdWriteCommand(uchar cmd,uchar chk)
{
if (chk) WaitForEnable(); // 检测忙信号?
CLR_RS;
CLR_RW;
_NOP();
DataPort = cmd; //将命令字写入数据端口
_NOP();
SET_EN; //产生使能脉冲信号
_NOP();
_NOP();
CLR_EN;
}
void LcdWriteData( uchar data1 )
{
WaitForEnable(); //等待液晶不忙
SET_RS;
CLR_RW;
SET_EN;
_NOP();
DataPort = data1; //将显示数据写入数据端口
_NOP();
//产生使能脉冲信号
_NOP();
_NOP();
CLR_EN;
}
void WaitForEnable(void)
{
unsigned int later=0;
DataPort=0xff;
CLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();
while(((DataPIN&0x80)!=0)&&(later<1000)) //检测忙标
那你说的光电传感器是串口的吗。我猜测看到的代码中可能涉及传感器的就是 UART_send_string(Tx_Buf); 这一部分,至于操作LCD的,那就是LCD开头的那些函数调用吧。
记得点赞采纳哦