|
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit rs=P2^5;
- sbit rw=P2^6;
- sbit duanxuan=P2^5;
- sbit weixuan=P2^6;
- sbit en=P2^4;
- sbit input_si=P1^0;
- sbit sck=P2^3;
- sbit lck=P1^4;
- sbit dsds=P1^0;
- sbit dsrw=P1^1;
- sbit dsas=P1^2;
- sbit gnd=P3^0;
- sbit moshi=P3^7;
- sbit jia=P3^6;
- sbit jian=P3^5;
- uchar code table[]={0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,0x80,0x90,0X88,0X83,
- 0XC6,0XA1,0X86,0X8E,0X89,0X7F,0XBF};
- uchar code table1[]=" 20 - - ";
- uchar code table2[]=" : : ";
- uchar ii,bb,flag;
- char shi,fen,miao,nian,yue,ri,week,ashi,afen,amiao;
- //函数申明
- void write_ds(uchar add,uchar date);
- uchar read_ds(uchar add);
- void write_week(uchar date);
- void init();
- void write_date(uchar aa);
- void write_com(uchar num);
- void write_sfm(char sh,fe,mia);
- void start();
- void respond();
- void stop();
- void write_byte(uchar date);
- uchar read_byte();
- void write_at(uchar address, date);
- uchar read_at(uchar address);
- void keyscan();
- void write_nyr(char nia2,yu,r);
- void hc595(uchar date);
- void set_time();
- void alarm(char ashi,uchar afen,uchar amiao);
- void delay(uchar z)
- {
- uchar x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void delay1()
- {;;}
- void main()
- {
- init();
- while(1)
- {
- write_ds(0x0B,0x26);
- keyscan();
- if(flag==0)
- {
- miao=read_ds(0);
- fen=read_ds(2);
- shi=read_ds(4);
- nian=read_ds(9);
- yue=read_ds(8);
- ri=read_ds(7);
- week=read_ds(6);
- write_week(week);
- write_sfm(shi,fen,miao);
- write_nyr(nian,yue,ri);
- }
- P1=read_ds(0x0b);
- }
- }
- void write_ds(uchar add,uchar date)
- {
- hc595(0x02);
- dsas=1;
- dsds=1;
- dsrw=1;
- // P0=0xff;
- P0=add;
- dsas=0;
- dsrw=0;
- P0=date;
- dsrw=1;
- dsas=1;
- hc595(0xff);
- }
- uchar read_ds(uchar add)
- {
- uchar ds_date;
- hc595(0x02);
- dsas=1;
- dsds=1;
- dsrw=1;
- P0=add;
- dsas=0;
- dsds=0;
- P0=0xff;
- ds_date=P0;
- dsds=1;
- dsas=1;
- hc595(0xff);
- return ds_date;
- }
- void set_time()
- { write_ds(0x0b,0x06);
- write_ds(0x06,1);
- write_ds(0x07,9);
- write_ds(0x08,11);
- write_ds(0x09,15);
- write_ds(0,30);
- write_ds(2,59);
- write_ds(4,23);
- }
- //函数初始化模块
- void init()
- {
- write_ds(0x0B,0x26);
- alarm(0x15,0x11,0x0a);
- rw=0;
- en=0;
- gnd=0;
- EA=1;
- EX0=1;
- IT0=0;
- duanxuan=1;
- P0=0xff;
- duanxuan=0;
- weixuan=1;
- P0=0xff;
- weixuan=0;
- set_time();
- write_com(0x38);
- write_com(0x06);
- write_com(0x0c);
- write_com(0x80);
- for(ii=0;ii<15;ii++)
- {
- write_date(table1[ii]);
- delay(50);
- }
- write_com(0x80+0x40+4);
- for(ii=0;ii<8;ii++)
- {
- write_date(table2[ii]);
- delay(50);
- }
- }
- //1602显示模块
- void write_date(uchar aa)
- {
- rs=1;
- P0=aa;
- delay(5);
- en=1;
- delay(5);
- en=0;
- }
- void write_com(uchar num)
- {
- rs=0;
- P0=num;
- delay(5);
- en=1;
- delay(5);
- en=0;
- }
- void write_sfm(char sh,fe,mia)
- {
- write_com(0x80+0x40+10);
- write_date(0x30+mia/10);
- delay(5);
- write_date(0x30+mia%10);
- delay(5);
- write_com(0x80+0x40+7);
- write_date(0x30+fe/10);
- delay(5);
- write_date(0x30+fe%10);
- delay(5);
- write_com(0x80+0x40+4);
- write_date(0x30+sh/10);
- delay(5);
- write_date(0x30+sh%10);
- delay(5);
- }
- void write_nyr(char nia2,yu,r)
- {
- write_com(0x80+3);
- write_date(0x30+nia2/10);
- delay(5);
- write_date(0x30+nia2%10);
- delay(5);
- write_com(0x80+6);
- write_date(0x30+yu/10);
- delay(5);
- write_date(0x30+yu%10);
- delay(5);
- write_com(0x80+9);
- write_date(0x30+ri/10);
- delay(5);
- write_date(0x30+ri%10);
- delay(5);
- }
- void write_week(uchar date)
- {
- switch(date)
- {
- case 1: write_com(0x80+12);
- write_date('M');
- write_date('O');
- write_date('N');
- break;
- case 2: write_com(0x80+12);
- write_date('T');
- write_date('U');
- write_date('S');
- break;
- case 3: write_com(0x80+12);
- write_date('W');
- write_date('E');
- write_date('D');
- break;
- case 4: write_com(0x80+12);
- write_date('T');
- write_date('H');
- write_date('U');
- break;
- case 5: write_com(0x80+12);
- write_date('F');
- write_date('R');
- write_date('I');
- break;
- case 6: write_com(0x80+12);
- write_date('S');
- write_date('T');
- write_date('A');
- break;
- case 7: write_com(0x80+12);
- write_date('S');
- write_date('U');
- write_date('N');
- break;
- }
- }
- void hc595(uchar date)
- {
- uchar i;
- sck=0;
- for(i=0;i<8;i++)
- { date=date<<1;
- lck=0;
- input_si=CY;
- lck=1;
- }
- lck=1;
- sck=1;
- }
- void keyscan()
- { uchar num1,ii;
- if(moshi==0)
- {
- delay(5);
- if(moshi==0)
- {
- flag=1;
- num1++;
- switch(num1)
- {
- case 1: write_com(0x80+0x40+11);
- write_com(0x0f);
- break;
- case 2: write_com(0x80+0x40+8);
- // write_com(0x0f);
- break;
- case 3: write_com(0x80+0x40+5);
- // write_com(0x0f);
- break;
- case 4: write_com(0x80+14);
- // write_com(0x0f);
- break;
- case 5: write_com(0x80+10);
- // write_com(0x0f);
- break;
- case 6: write_com(0x80+7);
- // write_com(0x0f);
- break;
- case 7: write_com(0x80+4);
- // write_com(0x0f);
- break;
- case 8: write_ds(0,miao);
- write_ds(2,fen);
- write_ds(4,shi);
- write_ds(6,week);
- write_ds(7,ri);
- write_ds(8,yue);
- write_ds(9,nian);
- write_com(0x0c);
- flag=0;
- num1=0;
- break;
- }
- while(!moshi);
- }
- }
- if(num1!=0)
- {
- if(jia==0)
- {
- delay(5);
- if(jia==0)
- {
- while(!jia);
- switch(num1)
- {
- case 1:
- miao++;
- if(miao==60)miao=0;
- write_sfm(shi,fen,miao);
- write_com(0x80+0x40+11);
- break;
- case 2: fen++;
- if(fen==60)fen=0;
- write_sfm(shi,fen,miao);
- write_com(0x80+0x40+8);
- break;
- case 3: shi++;
- if(shi==24)shi=0;
- write_sfm(shi,fen,miao);
- write_com(0x80+0x40+5);
- break;
- case 4: week++;
- if(week==8)week=1;
- write_week(week);
- write_com(0x80+14);
- break;
- }
- }
- }
- if(jian==0)
- {
- delay(5);
- if(jian==0)
- {
- while(!jian);
- switch(num1)
- {
- case 1: miao--;
- if(miao==-1)miao=59;
- write_sfm(shi,fen,miao);
- write_com(0x80+0x40+11);
- break;
- case 2: fen--;
- if(fen==-1)fen=59;
- write_sfm(shi,fen,miao);
- write_com(0x80+0x40+8);
- break;
- case 3: shi--;
- if(shi==-1)shi=23;
- write_sfm(shi,fen,miao);
- write_com(0x80+0x40+5);
- break;
- case 4: week--;
- if(week==0)week=7;
- write_week(week);
- write_com(0x80+14);
- break;
- }
- }
- }
- }
- }
- void alarm(char ashi,uchar afen,uchar amiao)
- {
- write_ds(1,amiao);
- write_ds(3,afen);
- write_ds(5,ashi);
- }
- void exter() interrupt 0
- {
- uchar c,i;
- c=read_ds(0x0C);
- for(i=30;i>0;i--)
- {
- P1=0x55;
- delay(200);
- P1=0xaa;
- delay(200);
- }
- }
复制代码
|
-
-
贴吧仿真.7z
142.42 KB, 下载次数: 96, 下载积分: 黑币 -5
|