#include<reg51.h>
sbit speaker=P3^7;
/////////////////////LCD
sbit rs=P3^4;
sbit rw=P3^6;
sbit e=P3^7;
//////////////////////led
sbit led1=P0^0;
sbit led2=P0^1;
sbit led3=P0^2;
sbit led4=P0^3;
sbit led5=P0^4;
sbit led6=P0^5;
sbit led_k=P1^4;
/////////////////键盘
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 k1=P3^3;
sbit k2=P3^5;
///////////////feng ming qi
sbit sound=P3^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);
void initi();
//void initi_rs();
void delay5ms();
void delay100us();
//void delay10us();
//void delay5us();
void display(unsigned int i,char a);
code 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 rs_dmx() interrupt 4 using 2
{
b=SBUF;
SBUF=b;
while(TI==0);TI=0;
delayms(1);
RI=0;
}
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,i;
unsigned int j;
delayms(10);
led_k=shu1=shu2=shu3=shu4=0;
initi();
////////////////////feng ming qi
for(j=0;j<300;j++)
{
sound=!sound;
delayms(1);
}
////////////////lcd
display(0,'G');
display(1,'o');
display(2,'o');
display(3,'d');
display(4,' ');
display(5,'G');
display(6,'i');
display(7,'r');
display(8,'l');
display(9,'!');
// write(1,9);
//while(1);
delayms(1000);
b=read(1);
speaker=0;/*关闭蜂鸣器电源,否则蜂鸣器会发热*/
EA=1;/*开全局中断 */
TR0=1;/*定时器0开始计数 */
ET0=1;/*定时器0开中断 */
// TMOD=0X01;/*定时器0工作在方式1:16位计数模式 */
TMOD=0x21;//2400
TH1=0xF3;
TL1=0xf3;
TR1=1;
EA=1;
SCON=0XD0;
ES=1;
while(1)
{/*INT1和led测试*/
if(k1==0)
{
EA=0;
shu1=shu2=shu3=shu4=0;
led_k=1;
led1=0;
delayms(100);
led2=0;
delayms(100);
led3=0;
delayms(100);
led4=0;
delayms(100);
led5=0;
delayms(100);
led6=0;
delayms(100);
led1=1;
delayms(100);
led2=1;
delayms(100);
led3=1;
delayms(100);
led4=1;
delayms(100);
led5=1;
delayms(100);
led6=1;
delayms(100);
EA=1;
}
/*INT1和led测试*/
if(k2==0)
{
// EA=1;
b=0;
for(i=0;i<10;i++)
{
b+=1111;
if(b>9999)b=0;
delayms(300);
}
}
/*键盘+24c02*/
b_tem=keyboar();/*把按键的代表的值给b */
if(b_tem<17)
{
if(b!=b_tem)
{
delayms(30);
b=b_tem;
write(1,b);/*把b的值写到24c02的地址为1的空间 */
}
}
}
}
///////////////////////////
void initi()
{
delayms(10);
rw=0;
e=1;
rs=0;
P0=0x01;
e=0;
delay5ms();
e=1;
rs=0;
P0=0x38;
e=0;
delay5ms();
e=1;
P0=0x0c;
e=0;
delay5ms();
e=1;
P0=0x04;
e=0;
delay5ms();
e=1;
}
void display(unsigned int i,char a)
{
e=1;
rs=0;
rw=0;
P0=0x80+i;
e=0;
delay100us();
e=1;
rs=1;
P0=a;
e=0;
delay100us();
e=1;
}
void delay100us()
{
unsigned int i=10;
for(;i>0;i--);
}
void delay5ms()
{
unsigned int i=80000;
for(;i>0;i--);
}
////////////////////////////
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;
}