单片机写入程序后1602没有显示
源程序如下
#include<reg52.h> //头文件
#include<intrins.h>
#include "eeprom52.h"
#include "api_c.h"
#define uchar unsigned char //宏定义
#define uint unsigned int
#define LCD1602_dat P0
sbit LCD1602_rs=P2^5;//IO 定义
sbit LCD1602_rw=P2^6;
sbit LCD1602_e=P2^7;
sbit beep=P2^0;
sbit led_1=P1^0;
sbit led_2=P1^1;
sbit key_1=P1^5;
sbit key_2=P1^6;
sbit key_3=P1^7;
bit alARM_1,alarm,once;
uint sum;
uchar RH,RH_H=12,RH_L=8,state,ms,time_num,cs,sec;
bit beep1,zt,s1,mode,send;
sbit key_s=P3^7;
void delay(uint T) //延时函数
{
while(T--);
}
void LCD1602_write(uchar order,dat) //1602 一个字节 处理
{
LCD1602_e=0;
LCD1602_rs=order;
LCD1602_dat=dat;
LCD1602_rw=0;
LCD1602_e=1;
delay(1);
LCD1602_e=0;
}
void LCD1602_writebyte(uchar *prointer) //1602 字符串 处理
{
while(*prointer!=' ')
{
LCD1602_write(1,*prointer);
prointer++;
}
}
void LCD1602_cls() //1602 初始化
{
LCD1602_write(0,0x01); //1602 清屏 指令
delay(1500);
LCD1602_write(0,0x38); // 功能设置 8位、5*7点阵
delay(1500);
LCD1602_write(0,0x0c); //设置 光标 不显示开关、不显示光标、字符不闪烁
LCD1602_write(0,0x06);
LCD1602_write(0,0xd0);
delay(1500);
}
void show()
{
if(state==0)
{
LCD1602_write(0,0x80);
LCD1602_writebyte("Water level:");
if(RH>9)LCD1602_write(1,0x30+RH/10%10);
else LCD1602_writebyte(" ");
LCD1602_write(1,0x30+RH%10);
LCD1602_writebyte("cm");
LCD1602_write(0,0xC0);
LCD1602_writebyte(" ");
/* if(zt==0)
{
LCD1602_writebyte("Manul ");
}else
{
LCD1602_writebyte(" Aoto ");
}*/
}else
{
LCD1602_write(0,0x80);
LCD1602_writebyte("Water_H:");
if(state==1&&s1==1)
{
LCD1602_writebyte(" ");
}else
{
if(RH_H>9)LCD1602_write(1,0x30+RH_H/10%10);
else LCD1602_writebyte(" ");
LCD1602_write(1,0x30+RH_H%10);
}
LCD1602_writebyte("cm ");
LCD1602_write(0,0xC0);
LCD1602_writebyte("Water_L:");
if(state==2&&s1==1)
{
LCD1602_writebyte(" ");
}else
{
if(RH_L>9)LCD1602_write(1,0x30+RH_L/10%10);
else LCD1602_writebyte(" ");
LCD1602_write(1,0x30+RH_L%10);
}
LCD1602_writebyte("cm ");
}
}
void key()
{
if(!key_1)
{
delay(888);
if(!key_1)
{
while(!key_1) show();
state=(state+1)%3;
}
}
if(!key_2)
{
delay(888); //按键去抖
if(!key_2)
{
while(!key_2)show();
if(state==1)
{
if(RH_H<100)RH_H++;
SectorErase(0x2000); //保存上限值
byte_write(0x2000,RH_H);
}else if(state==2)
{
if(RH_L<RH_H-1)RH_L++;
SectorErase(0x2200); //保存上限值
byte_write(0x2200,RH_L);
}
}
}
if(!key_3)
{
delay(888);
if(!key_3)
{
while(!key_3)show();
if(state==1)
{
if(RH_H>RH_L+1)RH_H--;
SectorErase(0x2000); //保存上限值
byte_write(0x2000,RH_H);
}else if(state==2)
{
if(RH_L>0)RH_L--;
SectorErase(0x2200); //保存上限值
byte_write(0x2200,RH_L);
}
}
}
}
void proc()
{
if(zt==0)
{
if(RH>=RH_H)
{
alarm_1=1;
led_1=0;
}else
{
led_1=1;
}
if(RH<=RH_L)
{
alarm_1=0;
led_2=0;
}else
{
led_2=1;
}
if(RH>=RH_H|RH<=RH_L)
{
beep1=1;
}else
{
beep1=0;
}
}else
{
beep1=0;
led_1=led_2=1;
}
}
void main()
{
float Ad_dat=0;
uchar TxBuf[20]={0}; //
uchar RxBuf[20]={0};
alarm_1=1;
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
ET0=1;
TR0=1;
EA=1;
init_NRF24L01() ;
delay(40000);
LCD1602_cls();
RH_H=byte_read(0x2000);
RH_L=byte_read(0x2200);
if((RH_H>99)||(RH_L>99)||(RH_L>=RH_H)) {RH_H=14; RH_L=7;}
nRF24L01_TxPacket(TxBuf);
delay(8000);
while(1)
{
if(alarm_1!=alarm||once==1)
{
if(alarm_1==0) TxBuf[1]=2;
else TxBuf[1]=1;
send=1;
alarm=alarm_1;
once=0;
}
if(send==1)
{
nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
TxBuf[1] = 0x00;
TxBuf[2] = 0x00;
send=0;
delay(2000);
}
SetRX_Mode();
nRF24L01_RxPacket(RxBuf);
Ad_dat=RxBuf[0];
if(Ad_dat>2)
{
Ad_dat=(float)((Ad_dat-2)/2.5); //计算水位
RH=(uchar)(Ad_dat);
if(RH>=99)RH=99;
}
else
RH=0;
delay(2000);
RxBuf[1] = 0x00;
RxBuf[2] = 0x00;
show();
key();
proc();
}
}
void UART_1() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
ms++;
if(ms%10==0)
{
once=1;
}
if(ms%5==0)
{
s1=!s1;
if(beep1==1)
{
beep=!beep;
}else
{
beep=1;
}
}
if(ms>19)
{
ms=0;
mode=!mode;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>