- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit rs=P2^0;
- sbit lcden=P1^2;
- sbit rw=P2^1;
- sbit s4=P3^3;
- sbit s5=P3^2;
- sbit s6=P3^1;
- sbit LCD_PSB=P2^6;
- sbit dsce=P1^3;
- sbit dsio=P2^3;
- sbit dssc=P1^7;
- sbit ACC_7=ACC^7;
- char miao,shi,fen,year,month,day,week,amiao,afen,ashi;
- uchar code table[]=" 20 - - ";
- uchar code table1[]=" : : ";
- void write_ds(uchar,uchar);
- uchar read_ds(uchar);
- uchar s1num;
- void set_time();
- void cls_buzz(void)
- {
- P2 = (P2&0x1f|0xA0);
- P0 = 0x00;
- P2 = 0x1f;
-
- P2 = (P2&0x1f|0x80);
- P0 = 0xff;
- P2 = 0x1f;
-
- P2 = (P2&0x1f|0xc0);
- P0 = 0x00;
- P2 = 0x1f;
-
- P2 = (P2&0x1f|0xe0);
- P0 = 0xff;
- P2 = 0x1f;
- }
- 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;
- lcden=0;
- P0=com;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void write_date(uchar date)
- {
- rs=1;
- rw=0;
- lcden=0;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void lcd_pos(uchar x,uchar y)
- {
- uchar postion;
- if(x==0)
- x=0x80;
- else if(x==1)
- x=0x90;
- else if(x==2)
- x=0x88;
- else if(x==3)
- x=0x98;
- postion=x+y;
- write_com(postion);
- }
- void init()
- {
- uchar num;
- s1num=0;
- week=1;
- lcden=0;
- LCD_PSB = 1;
- write_com(0x30);
- delay(5);
- write_com(0x0C);
- delay(5);
- write_com(0x01);
- delay(5);
- lcd_pos(0,0);
- for(num=0;num<15;num++)
- {
- write_date(table[num]);
- delay(5);
- }
- lcd_pos(1,0);
- for(num=0;num<11;num++)
- {
- write_date(table1[num]);
- delay(5);
- }
- }
- void write_sfm(uchar add,char date)
- {//4Îaê±£¬7Îa·Ö£¬10ÎaÃë
- char shi,ge;
- shi=date/10;
- ge=date%10;
- lcd_pos(1,add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void write_nyr(uchar add,char date)
- {//3ÎaÄ꣬6Îa·Ö£¬9ÎaÃë
- char shi,ge;
- shi=date/10;
- ge=date%10;
- lcd_pos(0,add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void write_week(char we)
- {
- lcd_pos(0,8);
- switch(we)
- {
- case 1: write_date('M');delay(5);
- write_date('O');delay(5);
- write_date('N');
- break;
- case 2: write_date('T');delay(5);
- write_date('U');delay(5);
- write_date('E');
- break;
- case 3: write_date('W');delay(5);
- write_date('E');delay(5);
- write_date('D');
- break;
- case 4: write_date('T');delay(5);
- write_date('H');delay(5);
- write_date('U');
- break;
- case 5: write_date('F');delay(5);
- write_date('R');delay(5);
- write_date('I');
- break;
- case 6: write_date('S');delay(5);
- write_date('A');delay(5);
- write_date('T');
- break;
- case 7: write_date('S');delay(5);
- write_date('U');delay(5);
- write_date('N');
- break;
- }
- }
- void keyscan()
- {
- if(s4==0)
- {
- delay(5);
- if(s4==0)
- {
- s1num++;
- while(!s4);
- switch(s1num)
- {
- case 1: lcd_pos(1,10);
- write_com(0x0f);
- break;
- case 2: lcd_pos(1,7);
- break;
- case 3: lcd_pos(1,4);
- break;
- case 4: lcd_pos(0,12);
- break;
- case 5: lcd_pos(0,9);
- break;
- case 6: lcd_pos(0,6);
- break;
- case 7: lcd_pos(0,3);
- break;
- case 8: s1num=0;
- write_com(0x0c);
- write_ds(80,miao);
- write_ds(82,fen);
- write_ds(84,shi);
- write_ds(0x8a,week);
- write_ds(86,day);
- write_ds(88,month);
- write_ds(0x8c,year);
- break;
- }
- }
- }
- if(s1num!=0)
- {
- if(s5==0)
- {
- delay(1);
- if(s5==0)
- {
- while(!s5);
- switch(s1num)
- {
- case 1: miao++;
- if(miao==60)
- miao=0;
- write_sfm(10,miao);
- lcd_pos(1,10);
- break;
- case 2: fen++;
- if(fen==60)
- fen=0;
- write_sfm(7,fen);
- lcd_pos(1,7);
- break;
- case 3: shi++;
- if(shi==24)
- shi=0;
- write_sfm(4,shi);
- lcd_pos(1,4);
- break;
- case 4: week++;
- if(week==8)
- week=1;
- write_week(week);
- lcd_pos(0,12);
- break;
- case 5: day++;
- if(day==32)
- day=1;
- write_nyr(9,day);
- lcd_pos(0,9);
- break;
- case 6: month++;
- if(month==13)
- month=1;
- write_nyr(6,month);
- lcd_pos(0,6);
- break;
- case 7: year++;
- if(year==100)
- year=0;
- write_nyr(3,year);
- lcd_pos(0,3);
- break;
- }
- }
- }
- if(s6==0)
- {
- delay(1);
- if(s6==0)
- {
- while(!s6);
- switch(s1num)
- {
- case 1: miao--;
- if(miao==-1)
- miao=59;
- write_sfm(10,miao);
- lcd_pos(1,10);
- break;
- case 2: fen--;
- if(fen==-1)
- fen=59;
- write_sfm(7,fen);
- lcd_pos(1,7);
- break;
- case 3: shi--;
- if(shi==-1)
- shi=23;
- write_sfm(4,shi);
- lcd_pos(1,4);
- break;
- case 4: week--;
- if(week==0)
- week=7;
- write_week(week);
- lcd_pos(0,12);
- break;
- case 5: day--;
- if(day==0)
- day=31;
- write_nyr(9,day);
- lcd_pos(0,9);
- break;
- case 6: month--;
- if(month==0)
- month=12;
- write_nyr(6,month);
- lcd_pos(0,6);
- break;
- case 7: year--;
- if(year==-1)
- year=99;
- write_nyr(3,year);
- lcd_pos(0,3);
- break;
- }
- }
- }
- }
- }
- void write_ds(uchar addr,uchar dat)
- {
- uchar i,temp;
- dsce=0;
- dssc=0;
- dsce=1;
- for(i=8;i>0;i--)
- {
- dssc=0;
- temp=addr;
- dsio=(bit)(temp&0x01);
- addr>>=1;
- dssc=1;
- }
- for(i=8;i>0;i--)
- {
- dssc=0;
- temp=dat;
- dsio=(bit)(temp&0x01);
- dat>>=1;
- dssc=1;
- }
- dsce=0;
- }
- uchar read_ds(uchar addr)
- {
- uchar i,temp,dat1,dat2;
- dsce=0;
- dssc=0;
- dsce=1;
- for(i=8;i>0;i--)
- {
- dssc=0;
- temp=addr;
- dsio=(bit)(temp&0x01);
- addr>>=1;
- dssc=1;
- }
- for(i=8;i>0;i--)
- {
- ACC_7=dsio;
- dssc=1;
- ACC>>=1;
- dssc=0;
- }
- dsce=0;
- dat1=ACC;
- dat2=dat1/16;
- dat1=dat1%16;
- dat1=dat1+dat2*10;
- return dat1;
- }
- void main()
- {
- init();
- cls_buzz();
- while(1)
- {
- keyscan();
- year=read_ds(0x8d);
- month=read_ds(0x89);
- day=read_ds(0x87);
- week=read_ds(0x8b);
- shi=read_ds(0x85);
- fen=read_ds(0x83);
- miao=read_ds(0x81);
- write_sfm(10,miao);
- write_sfm(7,fen);
- write_sfm(4,shi);
- write_week(week);
- write_nyr(3,year);
- write_nyr(6,month);
- write_nyr(9,day);
- }
- }
复制代码 代码如上,很多内容不需要看的,我觉得问题应该出在时钟芯片上,但是我没怎么搞懂它的读写,读写函数write_ds和read_ds是直接在网上抄的。有人能帮我改一下吗?不胜感激。
|