📄 try.c
字号:
#include<reg51.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 select,move=0;//,pause=0;
bit pause=0;
uchar count=0;//,ccount1,ccount2;
uint point1,point2; //eeprom di zhi zhi zhen
uchar which=0,address=0; //what[8] 放的是总的子机数
uchar adp1,adp2,qi;
uchar ms2=0,ms1=0,ms50,ms6,se;
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[14][4];
ad adtemp[1][4];
//***********延时3秒**********************************
void delay3ms(uchar qr)
{
uchar ik,p;
for(p=0;p<=qr;p++)
for(ik=0;ik<100;ik++)
{
if(ms1==1)
{ ms1=0;
break;
}
nop();
}
}
//**********************延时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;
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 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 disinit(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 ext0() interrupt 0 //中断0 看门狗掉电
{
uchar ji;
//uchar ki;
con=1;
//**********************point1 he point2 的值存放在最后的4个字节里面******************************
check();
eepwrite(0x7f,0xff,((point2+12)%0x7fef)/256);
check();
//noop();
eepwrite(0x7f,0xfe,((point2+12)%0x7fef)%256);
check();
//noop();
/* eepwrite(0x7f,0xfd,((point2+12)%0x7fef)/256);
check();
eepwrite(0x7f,0xfc,((point2+12)%0x7fef)%256);
check();
*/
//*****************************最后的一行写进去*************************************************
/* for(ji=4;ji>0;ji--)
{
point2--; //指向前一个最后保存数据的地址
if(point2>=0x7fef)
point2=0x7fef;
eepwrite(point2/256,point2%256,addate[9][ji].adig);
check();
point2--;
if(point2>=0x7fef)
point2=0x7fef;
eepwrite(point2/256,point2%256,addate[9][ji].adl);
check();
point2--;
if(point2>=0x7fef)
point2=0x7fef;
eepwrite(point2/256,point2%256,addate[9][ji].adh);
check();
}
*/
// point2=0;
// for(ki=0;ki<10;ki++)
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++;
}
//*****************************当前正在转换的路数或者转到哪里也要在这里写进去**********************
eepwrite(0x7f,0xfb,which);
check();
//******************************自动显示或者循环显示也要写进去*************************************
eepwrite(0x7f,0xfa,select);
check();
eepwrite(0x7f,0xf9,move);//刷新显示或者是滚动
check();
eepwrite(0x7f,0xf8,pause);//暂停与否
check();
while(1);
}
void time0() interrupt 1
{
uchar y,a;
uint b,c;
//TR0=1;
TL0=0x00;
TH0=0x4c;
dog=1;
nop();
dog=0;
nop(); //喂狗
//ms2=1;
count++;
if(count==19)
{ ms50=1; // 1s 时间到了 开始发送
count=0; //恢复准备
ms2=1; //还不知道用来干什么
address=0;
qi=0; //发送前把接收的位置清零 从高位其接收存到addate中
}
if(ms50==1)
{
for(y=0;y<2;y++)
{ se=0;
send(address); //收回来的是哪一路 共有四路模拟量
delay6ms(10); //延时需要7MS 发送1 接收3个 用了4 在给出一半的冗余时间
if(se==1)
{ se=0;
address++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -