|
本帖最后由 fujian2014 于 2014-11-11 10:20 编辑
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit rs=P3^5;
- sbit lcden=P3^4;
- //sbit s1=P3^0;//功能键
- //sbit s2=P3^0;//增大键
- //sbit s3=P3^0;//减小键
- //sbit s4=P3^0;//闹钟查看键
- sbit beep=P3^7;
- sbit dscs=P1^4;
- sbit dsas=P1^5;
- sbit dsrw=P1^6;
- sbit dsds=P1^7;
- //sbit dsirq=P2^3;
- uchar count,num1;
- uchar miao,shi,fen,year,month,day,week,amiao,afen,ashi;
- uchar code table[]=" 20 - -"; //年月日显示格式
- uchar code table1[]=" : : "; //时间显示格?
- void delay(uint x)
- {
- uint a,b;
- for(a=x;a>0;a--)
- for(b=10;b>0;b--);
- }
- void didi()
- {
- beep=0;
- delay(100);
- beep=1;
- }
- void write_com(uchar com)
- {
- rs=0;
- lcden=0;
- P0=com;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void write_date(uchar date)
- {
- rs=1;
- lcden=0;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- /***************ds12c887***************/
- /**************************************/
- void ds_write(uchar add,uchar date)
- { dscs=0;
- dsas=1;
- dsds=1;
- dsrw=1;
- P2=add;
- dsas=0;
- dsrw=0;
- P2=date;
- dsas=1;
- dsrw=1;
- dscs=1;
- }
- uchar ds_read(uchar add)
- {
- uchar date;
- dsas=1;
- dsds=1;
- dsrw=1;
- dscs=0;
- P2=add;
- dsas=0;
- dsds=0;
- P2=0xff;
- date=P2;
- dsas=1;
- dsds=1;
- dscs=1;
- return date;
- }
- void set_time()
- {
- ds_write(0,0);
- ds_write(1,0);
- ds_write(2,0);
- ds_write(3,0);
- ds_write(4,0);
- ds_write(5,0);
- ds_write(6,0);
- ds_write(7,0);
- ds_write(8,0);
- ds_write(9,0);
- }
- void init()
- { uchar num;
- ds_write(0x0A,0x20);
- ds_write(0x0B,0x26);
- ds_read(0x0c);
- set_time();
- write_com(0x38); //显示模式设置:16×2显示,5×7点阵,8位数据接口
- delay(10);
- write_com(0x0c); //显示模式设置
- delay(10);
- write_com(0x06); //显示模式设置:光标右移,字符不移
- delay(10);
- write_com(0x01); //清屏幕指令,将以前的显示内容清除
- delay(10);
- num1=0;
- write_com(0x80);
- for(num=0;num<15;num++)
- {
- write_date(table[num]);
- delay(5);
- }
- write_com(0x80+0x40); //在第二行显示数据
- for(num=0;num<12;num++)
- {
- write_date(table1[num]);
- delay(5);
-
- }
- }
- void write_sfm(int add,int date)
- {
- int shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0x80+0x40+add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void write_nyr(uchar add,uchar date)
- {
- char shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0x80+add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void write_week(char we)
- {
- write_com(0x80+12);
- 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('T');
- 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 main()
- {
- //uchar a;
- init(); //1602初始化
- /*write_com(0x80+40); //设置第二行首字符显示的位置,为第二行,第一列
- a=0;
- while(table[a]!='\0') //只要没有写到结束标志,就继续写
- { //这是写字符的第一种方法,用while循环。
- write_date(table1[a]);
- a++;
- }*/
- while(1)
- {
- miao=ds_read(0);
- fen=ds_read(2);
- shi=ds_read(4);
- write_sfm(10,miao);
- write_sfm(7,fen);
- write_sfm(4,shi);
- }
-
- }
复制代码
我的程序在下面
|
|