📄 menu4.c
字号:
#include<reg52.h>
#include<intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() _nop_()
sbit cs=P2^0; // ye jing cs
sbit sid=P2^1; //ye jing shu ju xian
sbit scl=P2^2; //ye jing shi zhong xian
sbit sda=P2^4;
sbit sclk=P2^5;
sbit con=P2^6; //ye jing bie guang
sbit dog=P2^3;
uchar mulk,mulb=0,count=0;//,ccount1,ccount2;
uint point2; //eeprom di zhi zhi zhen
uchar address=0,state=0,err=0;//,which=0,;
uchar qi;
bit ms2=0,ms50,ms6,se;
uchar dis[16];
//uchar code lcd0[16]=" 9: 睡眠模式 ";
//uchar code lcd1[16]="通道号: ";
//uchar code lcd2[16]="电压值: ";
//uchar code lcd3[16]="开关量: ";
//uchar code lcd4[16]="11: 返回上级 ";
//uchar code lcd5[16]="号: 0~3 返回:11 ";
//uchar code lcd6[16]="0 1 2 3 4 ";
//uchar code lcd7[16]=" ";
//uchar code lcd8[16]="10: 实时监控 ";
//uchar code lcd9[16]="11: 历史浏览 ";
//uchar code lcd10[16]="9: 轮流显示 ";
//uchar code lcd11[16]="10: 可选显示 ";
//uchar code lcd12[16]="系统初始化中....";
//uchar code lcd13[16]="请选择下面历史点";
//uchar code lcd14[16]="5 6 7 8 9 ";
uchar code lcd15[8]="通道号:";
uchar code lcd16[8]="电压值: ";
uchar code lcd17[8]="开关量: ";
typedef struct ad
{
unsigned char adh;
unsigned char adl;
unsigned char adig;
}idata ad; //bao cun cai ji hui lai de shu ju
ad addate[10][4];
ad adtemp[1][4];
ad temp[4];
//ad red[4];
//**********************延时7秒************************
void delay6ms(uchar qu)
{
uchar ik,p;
for(p=0;p<=qu;p++)
for(ik=0;ik<100;ik++)
{
if(ms6==1)
{ ms6=0;
break;
}
nop();
}
}
void send(uchar sdda) //发送数据给子机
{
SBUF=sdda;
while(!TI)
{
;
}
TI=0;
}
void maxinit()
{
TMOD=0x21; //ding shi qi 1 fang shi 2 ,T0 fang shi 1
SCON=0xd0; //chuan kou fang shi 3 en=1 jie shou yun xu
PCON=0x00; // bo te lv bu bian
ET1=0; // jin zhi ding shi qi 1 zhong duan
TH1=0xFD; // bo te lv 9600
TL1=0xFD;
TR1=1;
TL0=0x00;
TH0=0x4c;
ET0=1;
EX0=0;
ES=1; //WAI BU ZHONG DUAN 0 YOU XIAN JI ZUI GAO // DIAO DIAN BAO HU
PX0=1;
PS=1;
IE0=1;
EA=1;
TR0=1; // chuan xing zhong duan you xian ji zui gao
}
void noop() //I2C EEPROM延时函数
{
_nop_();
_nop_();
_nop_();
_nop_();
}
/*void init1() //I2C初使化
{
sda=1;
noop();
sclk=1;
noop();
}*/
void start() //IIC开始信号
{
sda=1;
noop();
sclk=1;
noop();
sda=0;
noop();
}
void stop() //IIC停止信号
{
sda=0;
noop();
sclk=1;
noop();
sda=1;
noop();
}
void respons() //IIC应答信号
{
sda=1;
noop();
sclk=1;
noop();
//while(sda);
sclk=0;
noop();
sda=1;
noop();
}
void write_byte(uchar date) //写一个字节到I2C芯片中
{
uchar temp,x;
temp=date;
for(x=0;x<8;x++) //一个字节八位,
{
temp=temp<<1; //向左移一位,最低位补0;
sclk=0;
noop();//低电平时,才允许sda线变化
sda=CY;
noop();
sclk=1;
noop();//保持数据
}
sclk=0; //时钟线复位
noop();
sda=1; //释放
noop();
}
uchar read_byte() //读一个字节的数据
{
uchar j,k,y;
sclk=0;
noop();
sda=1;
noop();
for(j=0;j<8;j++)
{
sclk=1;
noop();
if(sda==1)
y=1;
else
y=0;
k=(k<<1)|y;
sclk=0;
noop();
}
noop();
return k;
}
void check() //IIC 写周期的应答查询
{
doo:
start();
write_byte(0xa0);
sclk=1;
noop();
sda=1;
noop();
if(sda==1)
{
goto doo;
}
sclk=0;
noop();
}
void eepwrite(uchar addh,uchar addl,uchar date) //IIC 写数据到eeprom
//指定地址写数据;
{
// init1();
start();
write_byte(0xa0);
respons();
write_byte(addh);
respons();
write_byte(addl);
respons();
write_byte(date);
respons();
stop();
}
uchar eepread(uchar addh,uchar addl) //从eeprom读数据
//指定地址du数据
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(addh);
respons();
write_byte(addl);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
respons();
stop();
return date;
}
void write(uchar dat, bit rs) //液晶屏写数据或命令 RS=0 写命令,RS=1 写数据
{ uchar k,d,t;
sid=1;
for(k=0;k<5;k++)
{
scl=1;
scl=0;
}
sid=0;
scl=1;
scl=0;
sid=rs;
scl=1;
scl=0;
sid=0;
scl=1;
scl=0;
d=dat&0xf0;
for(t=0;t<2;t++)
{
for(k=0;k<8;k++)
{
d=d<<1;
sid=CY;
scl=1;
scl=0;
}
d=dat&0x0f;
d=d<<4;
}
}
void disinit(void) //液晶屏初始化
{
delay6ms(1);
write(0x30,0);
noop();
noop();
write(0x30,0);
noop();
noop();
write(0x0c,0);
noop();
noop();
write(0x01,0);
delay6ms(1);
write(0x06,0);
noop();
noop();
}
uchar key(void) //键盘检测
{
uchar db,da;
P1=0x0f;
da=P1;
if(da==0x0f)
{
return (0xff);
}
else
{
delay6ms(120);
P1=0x0f;
db=P1;//hang zhi
if(da!=db)
return (0xff);
else
{
P1=0xf0;
da=P1; // lie zhi
switch(da|db)
{
case 0xee: return (0);
case 0xde: return (1);
case 0xbe: return (2);
case 0x7e: return (3);
case 0xed: return (4);
case 0xdd: return (5);
case 0xbd: return (6);
case 0x7d: return (7);
case 0xeb: return (8);
case 0xdb: return (9);
case 0xbb: return (10);
case 0xb7: return (11);
}// switch
} // else
}// else
} // end
void ext0() interrupt 0 //中断0 看门狗掉电
{
uchar ji;
con=1;
//**********************point1 he point2 的值存放在最后的4个字节里面******************************
check();
eepwrite(0x7f,0xff,((point2+12)%0x7fef)/256);
check();
eepwrite(0x7f,0xfe,((point2+12)%0x7fef)%256);
check();
for(ji=0;ji<4;ji++)
{
//指向前一个最后保存数据的地址
eepwrite(point2/256,point2%256,adtemp[0][ji].adh);
check();
point2++;
point2=point2%0x7fef;
eepwrite(point2/256,point2%256,adtemp[0][ji].adl);
check();
point2++;
point2=point2%0x7fef;
eepwrite(point2/256,point2%256,adtemp[0][ji].adig);
check();
point2++;
}
while(1);
}
void time0() interrupt 1
{
uchar a;
uint c;
TL0=0x00;
TH0=0x4c;
dog=1;
nop();
dog=0;
nop(); //喂狗
count++;
if(count==19)
{ ms50=1; // 1s 时间到了 开始发送
count=0; //恢复准备
ms2=1; //还不知道用来干什么
err=0;
address=0;
qi=0; //发送前把接收的位置清零 从高位其接收存到addate中
}
if(ms50==1)
{
for(a=0;a<2;a++)
{ se=0;
send(address); //收回来的是哪一路 共有四路模拟量
delay6ms(10); //延时需要7MS 发送1 接收3个 用了4 在给出一半的冗余时间
if(se==1)
{ se=0;
address++;
break ;
}
else
{
if(a==1)
{
adtemp[0][address].adh=0xaa;
adtemp[0][address].adl=0xaa;
adtemp[0][address].adig=0x0f;
address++;
//err++;
}
}
}
// address++;
if((address==4))
{ if(err==7)
{ err=0;
//b=point1;
c=point2;
//point1=(point1+12)%0x7fef;
point2=(point2+12)%0x7fef;
for(a=0;a<4;a++)
{
eepwrite(c/256,c%256,adtemp[0][a].adh);
temp[a].adh=adtemp[0][a].adh;
check();
c=c++;
c=c%0x7fef;
eepwrite(c/256,c%256,adtemp[0][a].adl);
temp[a].adl=adtemp[0][a].adl;
check();
c++;
c=c%0x7fef;
eepwrite(c/256,c%256,adtemp[0][a].adig);
temp[a].adig=adtemp[0][a].adig;
check();
c++;
c=c%0x7fef;
}
}
address=0;
ms50=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -