本帖最后由 daniu 于 2014-12-26 14:28 编辑
好东西 还有仿真 适合初学者这里贴一下楼主的代码:
- #include<reg51.h>
- #include<intrins.h>
-
- #define uint unsigned int
- #define uchar unsigned char
- uchar second,minute,hour,day,month,year,week,keynum;
- sbit daio=P3^6;
- sbit rst=P3^4;
- sbit sclk=P3^5;
-
- sbit rs=P2^0;
- sbit rw=P2^1;
- sbit ep=P2^2;
-
- sbit keyfw=P2^5;
- sbit keyjia=P2^6;
- sbit keyjian=P2^7;
- void delaynus(unsigned char n)
- {
- unsigned char i;
- for(i=0;i<n;i++);
- }
-
- void write1302(uchar dat)
- {
- uchar i;
- sclk=0;
- delaynus(2);
- for(i=0;i<8;i++)
- {
- daio=dat&0x01;
- delaynus(2);
- sclk=1;
- delaynus(2);
- sclk=0;
- dat>>=1;
- }
-
- }
-
- void writeset1302(uchar add,uchar dat)
- {
- rst=0;
- sclk=0;
- rst=1;
- delaynus(2);
- write1302(add);
- write1302(dat);
- sclk=1;
- rst=0;
- }
-
- uchar read1302()
- {
- uchar i,dat;
- delaynus(2);
- for(i=0;i<8;i++)
- {
- dat>>=1;
- if(daio==1)
- dat|=0x80;
- sclk=1;
- delaynus(2);
- sclk=0;
- delaynus(2);
- }
- return dat;
- }
-
-
- uchar readset1302(uchar add)
- {
- uchar dat;
- rst=0;
- sclk=0;
- rst=1;
- write1302(add);
- dat=read1302();
- sclk=1;
- rst=0;
- return dat;
- }
-
- void Init_1302()
- {
- writeset1302(0x8E,0x00);
- writeset1302(0x80,((56/10)<<4|(56%10)));
- writeset1302(0x82,((59/10)<<4|(59%10)));
- writeset1302(0x84,((23/10)<<4|(23%10)));
- writeset1302(0x86,((26/10)<<4|(26%10)));
- writeset1302(0x88,((12/10)<<4|(12%10)));
- writeset1302(0x8a,((5/10)<<4|(5%10)));
- writeset1302(0x8c,((14/10)<<4|(14%10)));
- }
-
- void delay1ms()
- {
- unsigned char i,j;
- for(i=0;i<10;i++)
- for(j=0;j<33;j++);
- }
-
- void delaynms(unsigned char n)
- {
- unsigned char i;
- for(i=0;i<n;i++)
- delay1ms();
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
-
- }
-
- void write_com(uchar com)
- {
- rs=0;
- rw=0;
- ep=0;
- P0=com;
- delay(2);
- ep=1;
- delay(2);
- ep=0;
- delay(2);
- }
- void write_dat(uchar dat)
- {
- rs=1;
- rw=0;
- ep=0;
- P0=dat;
- delay(2);
- ep=1;
- delay(2);
- ep=0;
- delay(2);
- }
- void write_zfc(uchar *p)
- {
- while(*p!=0)
- write_dat(*p++);
- }
- void init_1602()
- {
- write_com(0x38);
- write_com(0x01);
- write_com(0x06);
- write_com(0x0c);
- write_com(0x80);
- write_zfc(" 20 - - ");
- write_com(0xc0);
- write_zfc(" : : ");
- }
- void write_1602(uchar add,uchar dat)
- {
- write_com(add);
- write_dat(dat+0x30);
- }
-
- void DisplaySecond(uchar x)
- {
- unsigned char shi,ge;
- shi=x/10;
- ge=x%10;
-
- write_1602(0xc0+9,shi);
- write_1602(0xc0+10,ge);
- delaynms(50);
- }
-
- void DisplayMinute(uchar x)
- {
- unsigned char shi,ge;
- shi=x/10;
- ge=x%10;
- write_1602(0xc0+6,shi );
- write_1602(0xc0+7,ge );
- delaynms(50);
- }
-
- void DisplayHour(uchar x)
- {
- unsigned char shi,ge;
- shi=x/10;
- ge=x%10;
- write_1602(0xc0+3,shi);
- write_1602(0xc0+4,ge);
- delaynms(50);
- }
-
- void DisplayDay(uchar x)
- {
- unsigned char shi,ge;
- shi=x/10;
- ge=x%10;
- write_1602(0x80+9,shi );
- write_1602(0x80+10,ge );
- delaynms(50);
- }
- void Displayweek()
- {
- write_1602(0x80+12,week+0x30);
- switch(week)
- {
- case 1: write_com(0x80+12);
- write_zfc("mon");
- break;
- case 2:write_com(0x80+12);
- write_zfc("tue");
- break;
- case 3:write_com(0x80+12);
- write_zfc("wed");
- break;
- case 4:write_com(0x80+12);
- write_zfc("thu");
- break;
- case 5:write_com(0x80+12);
- write_zfc("fri");
- break;
- case 6:write_com(0x80+12);
- write_zfc("sta");
- break;
- case 7:write_com(0x80+12);
- write_zfc("sun");
- break;
- }
-
- }
-
- void DisplayMonth(uchar x)
- {
- uchar shi,ge;
- shi=x/10;
- ge=x%10;
- write_1602(0x80+6,shi );
- write_1602(0x80+7,ge );
- delaynms(50);
- }
-
- void DisplayYear(unsigned char x)
- {
- unsigned char shi,ge;
- shi=x/10;
- ge=x%10;
- write_1602(0x80+3,shi);
- write_1602(0x80+4,ge);
- delaynms(50);
- }
- void keyscan()
- {
- if(keyfw==0)
- {
- delay(2);
- if(keyfw==0)
- {
- while(!keyfw);
- {
- keynum++;
- switch(keynum)
- {
- case 1:write_com(0x0f);
- write_com(0xc0+10);
- break;
- case 2:write_com(0x0f);
- write_com(0xc0+7);
- break;
- case 3:write_com(0x0f);
- write_com(0xc0+4);
- break;
- case 4:write_com(0x0f);
- write_com(0x80+12);
- break;
- case 5:write_com(0x0f);
- write_com(0x80+10);
- break;
- case 6:write_com(0x0f);
- write_com(0x80+7);
- break;
- case 7:write_com(0x0f);
- write_com(0x80+4);
- break;
- case 8: keynum=0;
- write_com(0x0c);
- writeset1302(0x80,((second/10)<<4|(second%10)));
- writeset1302(0x82,((minute/10)<<4|(minute%10)));
- writeset1302(0x84,((hour/10)<<4|(hour%10)));
- writeset1302(0x86,((day/10)<<4|(day%10)));
- writeset1302(0x88,((month/10)<<4|(month%10)));
- writeset1302(0x8a,((week/10)<<4|(week%10)));
- writeset1302(0x8c,((year/10)<<4|(year%10)));
-
- break;
-
- }
- }
-
- }
-
- }
- if(keynum!=0)
- {
- if(keyjia==0)
- {
- delay(5);
- if(keyjia==0)
- {
- while(!keyjia);
- {
- switch(keynum)
- {
- case 1:second++;
- if(second==60)
- second=0;
- write_com(0xc0+12);
- DisplaySecond(second);
- break;
- case 2:minute++;
- if(minute==60)
- minute=0;
- DisplayMinute(minute);
- write_com(0xc0+7);
- break;
- case 3:hour++;
- if(hour==24)
- hour=0;
- DisplayHour(hour);
- write_com(0xc0+4);
- break;
- case 4:week++;
- if(week==8)
- week=0;
- Displayweek();
- write_com(0x80+12);
- break;
- case 5:day++;
- if(day==32)
- day=1;
- DisplayDay(day);
- write_com(0x80+10);
- break;
- case 6:month++;
- if(month==13)
- month=1;
- DisplayMonth(month);
- write_com(0x80+7);
- break;
- case 7:year++;
- if(year==100)
- year=0;
- DisplayYear(year);
- write_com(0x80+4);
- break;
-
- }
-
- }
-
- }
-
- }
-
-
- }
- if(keynum!=0)
- {
- if(keyjian==0)
- {
- delay(2);
- if(keyjian==0)
- {
- while(!keyjian);
- {
- switch(keynum)
- {
- case 1:second--;
- if(second==-1)
- second=59;
- write_com(0xc0+12);
- DisplaySecond(second);
- break;
- case 2:minute--;
- if(minute==-1)
- minute=59;
- DisplayMinute(minute);
- write_com(0xc0+7);
- break;
- case 3:hour--;
- if(hour==-1)
- hour=23;
- DisplayHour(hour);
- write_com(0xc0+4);
- break;
- case 4:week--;
- if(week==-1)
- week=7;
- Displayweek();
- write_com(0x80+12);
- break;
- case 5:day++;
- if(day==0)
- day=31;
- DisplayDay(day);
- write_com(0x80+10);
- break;
- case 6:month--;
- if(month==0)
- month=12;
- DisplayMonth(month);
- write_com(0x80+7);
- break;
- case 7:year--;
- if(year==-1)
- year=99;
- DisplayYear(year);
- write_com(0x80+4);
- break;
-
- }
-
- }
-
- }
-
- }
-
-
- }
- }
-
- void main()
- {
-
- uchar ReadValue;
- init_1602();
- Init_1302();
- while(1)
- {
- keyscan();
- if(keynum==0)
- {
- ReadValue =readset1302(0x8b);
- week=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
- Displayweek();
-
- ReadValue = readset1302(0x81);
- second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- DisplaySecond(second);
-
- ReadValue = readset1302(0x83);
- minute=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- DisplayMinute(minute);
-
- ReadValue = readset1302(0x85);
- hour=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- DisplayHour(hour);
-
- ReadValue = readset1302(0x87);
- day=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- DisplayDay(day);
-
- ReadValue = readset1302(0x89);
- month=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- DisplayMonth(month);
-
- ReadValue = readset1302(0x8d);
- year=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- DisplayYear(year);
- }
- }
- }
复制代码 |