#includereg51.h#defineucharunsignedchar//宏定义无符号字符型#defineuintunsignedint//宏定义无符号整型/********************************************************************初始定义*********************************************************************/ucharsj;//存储数据值bitwrite=0;//写24C08的标志;uchardat=0xee;//用于存储单片机接收发送缓冲寄存器SBUF里面的内容/*定义八位出为单片机P2口*/sbitK1=P2^0;sbitK2=P2^1;sbitK3=P2^2;sbitK4=P2^3;sbitK5=P2^4;sbitK6=P2^5;sbitK7=P2^6;sbitK8=P2^7;/*定义八位入为单片机P1口*/sbitIN0=P1^0;sbitIN1=P1^1;sbitIN2=P1^2;sbitIN3=P1^3;sbitIN4=P1^4;sbitIN5=P1^5;sbitIN6=P1^6;sbitIN7=P1^7;/********************************************************************24C08读写驱动程序*********************************************************************/sbitscl=P3^6;//24c08SCLsbitsda=P3^7;//24c08SDAvoiddelay1(ucharx){uinti;for(i=0;ix;i++);;}voidflash(){;;}voidx24c08_init()//24c08初始化子程序{scl=1;flash();sda=1;flash();}voidstart()//启动(I方C)总线{sda=1;flash();scl=1;flash();sda=0;flash();scl=0;flash();}voidstop()//停止(I方C)总线{sda=0;flash();scl=1;flash();sda=1;flash();}voidwritex(ucharj)//写一个字节{uchari,temp;temp=j;for(i=0;i8;i++){temp=temp1;scl=0;flash();sda=CY;flash();scl=1;flash();}scl=0;flash();sda=1;flash();}ucharreadx()//读一个字节{uchari,j,k=0;scl=0;flash();sda=1;for(i=0;i8;i++){flash();scl=1;flash();if(sda==1)j=1;elsej=0;k=(k1)|j;scl=0;}flash();return(k);}voidclock()//(I方C)线时钟{unsignedchari=0;scl=1;flash();while((sda==1)&&(i255))i++;scl=0;flash();}/********************************************************************从24c08的地址address中读取一个字节数据*********************************************************************/unsignedcharx24c08_read(unsignedcharaddress){unsignedchari;start();writex(0xa0);clock();writex(address);clock();start();writex(0xa1);clock();i=readx();stop();delay1(10);return(i);}/********************************************************************向24c08的address地址中写入一字节数据*********************************************************************/voidx24c08_write(unsignedcharaddress,unsignedcharinfo){EA=0;start();writex(0xa0);clock();writex(address);clock();writex(info);clock();stop();EA=1;delay1(50);}/********************************************************************延时函数*********************************************************************/voiddelay(uchart){uchari,j;for(i=0;it;i++){for(j=13;j0;j--);{;}}}/********************************************************************功能:串口初始化,波特率9600,方式1*********************************************************************/voidInit_Com(void){TMOD=0x20;PCON=0x00;SCON=0x50;TH1=0xFd;TL1=0xFd;TR1=1;}/********************************************************************接收数据判断函数*********************************************************************/chuankou(){switch(dat)//接收数据判断{uchark;k=10;case0xff:P2=0X00;delay(k);SBUF=dat;dat=0xee;write=1;break;//全开case0x00:P2=0XFF;delay(k);SBUF=dat;dat=0xee;write=1;break;//全关case0x01:K1=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第一路开case0x02:K2=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第二路开case0x03:K3=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第三路开case0x04:K4=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第四路开case0x05:K5=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第五路开case0x06:K6=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第六路开case0x07:K7=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第七路开case0x08:K8=0;delay(k);SBUF=dat;dat=0xee;write=1;break;//第八路开case0xFE:K1=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第一路关case0xFD:K2=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第二路关case0xFC:K3=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第三路关case0xFB:K4=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第四路关case0xFA:K5=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第五路关case0xF9:K6=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第六路关case0xF8:K7=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第七路关case0xF7:K8=1;delay(k);SBUF=dat;dat=0xee;write=1;break;//第八路关case0x55:SBUF=P1;delay(k);dat=0xee;break;//读输入口状态case0xAA:SBUF=P2;delay(k);dat=0xee;write=1;break;//读输出口状态case0x11:P2=~P2;delay(k);SBUF=dat;dat=0xee;write=1;break;//输出口取反default:break;//跳出}return0;}sbitkey1=P1^0;sbitkey2=P1^1;sbitkey3=P1^2;sbitkey4=P1^3;sbitkey5=P1^4;sbitkey6=P1^5;sbitkey7=P1^6;sbitkey8=P1^7;sbitk1=P2^0;sbitk2=P2^1;sbitk3=P2^2;sbitk4=P2^3;sbitk5=P2^4;sbitk6=P2^5;sbitk7=P2^6;sbitk8=P2^7;bitn;voidkeyboard(){n=key1;while(8){if(n!=key1){delay1(10);if(n!=key1){k1=~k1;n=key1;}}if(key2==0){delay1(10);if(key2==0){k2=~k2;while(!key2);}}if(key3==0){delay1(10);if(key3==0){k3=~k3;while(!key3);}}if(key4==0){delay1(10);if(key4==0){k4=~k4;while(!key4);}}if(key5==0){delay1(10);if(key5==0){k5=~k5;while(!key5);}}if(key6==0){delay1(10);if(key6==0){k6=~k6;while(!key6);}}if(key7==1){delay1(10);if(key7==0){k7=~k7;while(!key7);}}if(key8==0){delay1(10);if(key8==0){k8=~k8;while(!key8);}}}}/********************************************************************主函数*********************************************************************/voidmain(){Init_Com();//串口初始化x24c08_init();//初始化24C08sj=x24c08_read(2);//读出保存的数据赋于datP2=sj;//将存储的数据赋予P2口while(1){chuankou();//接收数据判断函数if(RI)//扫描判断是否接收到数据,{dat=SBUF;//接收数据SBUF