/*
按下4×4键盘任意一个键,记下数码管显示的值。复位单片机后数码管显示刚才显示的值
*/
#include<reg51.h>
sbit speaker=P3^7;
/////////////////键盘
sbit v1=P2^0;
sbit v2=P2^1;
sbit v3=P2^2;
sbit v4=P2^3;
sbit h1=P2^4;
sbit h2=P2^5;
sbit h3=P2^6;
sbit h4=P2^7;
/////////////////显示
sbit shu1=P1^3;/*第1位数码管共阴端*/
sbit shu2=P1^2;/*第2位数码管共阴端*/
sbit shu3=P1^1;/*第3位数码管共阴端*/
sbit shu4=P1^0;/*第4位数码管共阴端*/
////////////////24c02
sbit sda=P3^4;
sbit scl=P3^2;
void start();
void delay1();
unsigned int read(unsigned int word_address);
void write(unsigned int word_address,da);
void delayms(unsigned int i);
unsigned char yima[]={0xeb,0x88,0xb3,0xba,0xd8,0x7a,0x7b,0xa8,0xfb,0xfa};/*译码表,此表数据和硬件相关*/
unsigned int b=0;/*要显示的数据*/
unsigned int b_count=0;/*扫描次数*/
void delayms(unsigned int i);
unsigned char keyboar();
void t0()interrupt 1 using 1 /*中断程序负责显示b的值 */
{
unsigned char a1=0,a2=0,a3=0,a4=0;
static int k=0;
/*数码管扫描显示*/
a1=b/1000;/*取b的千位*/
a2=b%1000/100;/*取b的百位*/
a3=b%100/10;/*取b的十位*/
a4=b%10;/*取b的个位*/
if(k==0){shu4=1;shu1=0;shu2=0;shu3=0;P0=yima[a1];}
else if(k==1){shu4=0;shu1=1;shu2=0;shu3=0;P0=yima[a4];}
else if(k==2){shu4=0;shu1=0;shu2=1;shu3=0;P0=yima[a3];}
else if(k==3){shu4=0;shu1=0;shu2=0;shu3=1;P0=yima[a2];}
k++;
if(k>3)k=0;
TH0=240;
}
main()
{
//write(1,60);
unsigned char b_tem;
b=read(1);
speaker=0;/*关闭蜂鸣器电源,否则蜂鸣器会发热*/
EA=1;/*开全局中断 */
TR0=1;/*定时器0开始计数 */
ET0=1;/*定时器0开中断 */
TMOD=0X01;/*定时器0工作在方式1:16位计数模式 */
while(0)
{
}
while(1)
{
b_tem=keyboar();/*把按键的代表的值给b */
if(b_tem<17)
{
if(b!=b_tem)
{
delayms(30);
b=b_tem;
write(1,b);/*把b的值写到24c02的地址为1的空间 */
}
}
}
}
void delayms(unsigned int i)
{
unsigned int j;
for(;i>0;i--)
for(j=100;j>0;j--);
}
unsigned char keyboar()/*线反转法 */
{
static unsigned int a=0;
unsigned char a1=0,b1=0;
/*行线为输入线,列线为输出线 */
v1=v2=v3=v4=0;
h1=h2=h3=h4=1;
if(P2<0xf0)/*检查行是否有按键按下 */
{
delayms(10);
if(P2<0xf0)
{
/*行线为输入线,列线为输出线 */
v1=v2=v3=v4=0;
h1=h2=h3=h4=1;
a1=P2;
/*行线为输出线,列线为输入线 */
h1=h2=h3=h4=0;
v1=v2=v3=v4=1;
b1=P2;
a=a1|b1; /*与0x03相或是为了屏蔽p2.0和p2.1的影响*/
}
}
if (a==0xe7){return 7;}//chu
else if(a==0xeb){return 8;}
else if(a==0xed){return 9;}
else if(a==0xee){return 13;}
else if(a==0xd7){return 4;}//
else if(a==0xdb){return 5;}
else if(a==0xdd){return 6;}
else if(a==0xde){return 12;}
else if(a==0xb7){return 1;}//-
else if(a==0xbb){return 2;}
else if(a==0xbd){return 3;}
else if(a==0xbe){return 11;}
else if(a==0x77){return 0;}//+
else if(a==0x7b){return 15;}
else if(a==0x7d){return 16;}
else if(a==0x7e){return 10;}
else return 17;//cuo wu
}
unsigned int read(unsigned int word_address)
{unsigned int i=0,m=0,j=0,k=0,read_d=0xa1,write=0xa0,b=0,da2=0;
bit a=1,c=0;
start();
j=0;
while(1)//device address
{
k=(write<<j)&0x80;j++;
if(k>1)b=1;
else b=0;
sda=b;
delay1();
scl=1;
delay1();
scl=0;
delay1();
sda=1;
if(sda==0)break;
}
//sda=1;//check ack
//a=sda;
scl=1;
delay1();
scl=0;
j=0;
while(1)//word address
{
k=(word_address<<j)&0x80;j++;
if(k>1)b=1;
else b=0;
sda=b;
delay1();
scl=1;
delay1();
scl=0;
delay1();
sda=1;
if(sda==0)break;
}
//sda=1;//check ack
//a=sda;
scl=1;
delay1();
scl=0;
start();
j=0;
for(i=0;i<8;i++)
{
k=(read_d<<j)&0x80;
if(k>1)b=1;
else b=0;
sda=b;
delay1();
scl=1;
delay1();
scl=0;
j++;
}
// sda=1;//check ack
//read_d data
for(i=0;i<8;i++)
{
scl=1;
delay1();
scl=0;
b=sda;
b=b<<7;
da2=(da2>>1)|b;
}
sda=1;
delay1();
scl=1;
delay1();
sda=0;
// scl=0;
return da2;
}
void write(unsigned int word_address,da)
{ unsigned int i=0,m=0,j=0,k=0,//word_addr=0x00,
read_d=0xa1,write=0xa0;
bit b=0;
bit a=1,c=0;
start();
while(1)//device address
{
k=(write<<j)&0x80;
if(k>1)b=1;
else b=0;
sda=b;
delay1();
scl=1;
delay1();
scl=0;
delay1();
sda=1;
if(sda==0)break;
j++;
}
sda=1;//check ack
while(sda==0)break;
a=sda;
// j=0;
scl=1;
delay1();
scl=0;
while(sda==1)break;
j=0;
while(1)//word address
{
k=(word_address<<j)&0x80;
if(k>1)b=1;
else b=0;
sda=b;
delay1();
scl=1;
delay1();
scl=0;
delay1();
sda=1;
if(sda==0)break;
delay1();
j++;
}
sda=1;//check ack
while(sda==0)break;
a=sda;
// j=0;
scl=1;
delay1();
scl=0;
while(sda==1)break;
for(i=0;i<8;i++)//write data
{
c=(da>>i)&0x01;
sda=c;
delay1();
scl=1;
delay1();
scl=0;
}
sda=1;//check ack
a=sda;
while(sda==0)break;//check ack;
scl=1;
delay1();
scl=0;
a=sda;
delay1();
sda=0;
delay1();
scl=1;
delay1();
sda=1;
a=sda;//stop write
delay1();
scl=0;
delay1();
}
void delay1()
{unsigned int i=100;
for(;i>0;i--);
}
void start()
{
scl=1;
scl=0;
sda=1;
scl=1;
sda=0;
scl=0;
}