📄 pe.c
字号:
#include<reg51.h>
#include<intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() _nop_()
void write(uchar dat, bit rs) ;
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
uchar code ts[]="oh! my god! it is unbelievable!";
uchar bi;
uchar hl[2];
/*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[8][10];
*/
uchar sdate;
void init485(void)
{
TMOD=0x21; //ding shi qi 1 fang shi 2
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;
EX0=0; //WAI BU ZHONG DUAN 0 YOU XIAN JI ZUI GAO // DIAO DIAN BAO HU
PS=1; // chuan xing zhong duan you xian ji zui gao
}
void ext0() interrupt 0 //中断0 看门狗掉电
{
uchar ji,ki;
for(ji=0;ji<8;ji++)
for(ki=0;ki<10;ki++)
{ ;
//eeprom(addate[ji][ki]);
}
}
void time0() interrupt 1
{
}
void ext1() interrupt 2
{
}
void time1() interrupt 3
{
}
void serial() interrupt 4 //串行中断 接收数据
{
if(TI==1)
TI=0;
else
{ if(RI==1)
{
sdate=SBUF;
RI=0;
hl[bi]=sdate;
bi++;
bi=bi%2;
}
}
}
void send(uchar sdda) //发送数据给子机
{
nop();
nop();
SBUF=sdda;
while(!TI)
{
;
}
TI=0;
}
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 delay(uchar ms) //延时
{
uchar i,j;
for(i=0;i<=ms;i++)
{
for(j=0;j<=100;j++)
nop();
}
}
void write(uchar dat, bit rs) //液晶屏写数据或命令 RS=0 写命令,RS=1 写数据
{ uchar k,d,t;
cs=1;
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 init(void) //液晶屏初始化
{
delay(100);
write(0x30,0);
delay(1);
write(0x30,0);
delay(1);
write(0x0c,0);
delay(1);
write(0x01,0);
delay(100);
write(0x06,0);
delay(1);
}
uchar key(void) //键盘检测
{
uchar db,da;
P1=0x0f;
da=P1;
if(da==0x0f)
{
return (0xff);
}
else
{
delay(200);//xiao chu dao dong shi jian
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 main()
{ uchar i,j,led,hw,lw;
SP=0x60;
con=0; // 开液晶屏
cs=1; // iic eeprom 有效
init();
init485(); // 485 chu shi hua
ES=1; //串行中断允许
EX0=0; // yun xu chuan xing zhong duan
IE0=0; //外中断0 中断请求标志清零
EA=1; // kai zong zhong duan
bi=0; //初始化接收回来的数组下标
write(0x80,0); //液晶屏初始位置
j=strlen(ts); //计算字符串的长度
{
for(i=0;i<j;i++)
{
write(ts[i],1);
if((i+1)%16==0)
{
write(0x90,0);
}
}
}
while(1)
{
hw=hl[0]/10+0x30;
write(0x98,0);
write(hw,1);
lw=hl[0]%10+0x30;
write(0x99,0);
write(lw,1);
hw=hl[1]/10+0x30;
write(0x9a,0);
write(hw,1);
lw=hl[1]%10+0x30;
write(0x9b,0);
write(lw,1);
led=key();
if(led==0xff)
continue;
else
{
eepwrite(0x00,0x01,led);
check();
write(0x88,0);
write(eepread(0x00,0x01)+0x30,1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -