可根据按键修改时间、暂停、选择位等操作
单片机源程序如下:
- #include "reg52.h"
- #include "ds1302.h"
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit beep=P1^5;
- sbit k1=P1^0;
- sbit k2=P1^1;
- sbit k3=P1^2;
- sbit k5=P1^4;
- sbit k6=P1^5;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- u8 x=0,y1=0,y21=0,y22=0,y3=0,y4=0;y5=0,y6=0;
- u8 displaydata[8];
- u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delay(u16 i)
- {
- while(i--);
- }
- void datapros()
- {
- ds1302readtime();
- displaydata[0] = smgduan[time[2]/16]; //时
- displaydata[1] = smgduan[time[2]&0x0f];
- displaydata[2] = 0x40;
- displaydata[3] = smgduan[time[1]/16]; //分
- displaydata[4] = smgduan[time[1]&0x0f];
- displaydata[5] = 0x40;
- displaydata[6] = smgduan[time[0]/16]; //秒
- displaydata[7] = smgduan[time[0]&0x0f];
- }
- void aj1() //每一个循环里面放置一个
- {
- if(k1==0)
- {
- delay(1000);
- if(k1==0)
- {
- x++;
- if(x==7)x%=7;
- }
- }
- while(!k1);
- }
- void aj2()
- {
- if(k2==0) //秒个位归零
- {
- delay(1000);
- if(k2==0)
- {
- ds1302write(0x80,0x70&ds1302read(0x81));
- }
- }
- while(!k2);
- }
- void aj5() //开启写保护
- {
- if(k5==0)
- {
- delay(1000);
- if(k5==0)
- {
- ds1302write(0x8e,0x80);
- }
- }
- while(!k5);
- }
- void aj6() //关闭写保护
- {
- if(k6==0)
- {
- delay(1000);
- if(k6==0)
- {
- ds1302write(0x8e,0x00);
- }
- }
- while(!k6);
- }
- void DigDisplay()
- {
- switch(x)
- {
- u8 i;
- case(0):
- for(i=0;i<8;i++)
- {
- switch(i) //位选,选择点亮的数码管,
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;//显示第0位
- case(1):
- LSA=1;LSB=0;LSC=0; break;//显示第1位
- case(2):
- LSA=0;LSB=1;LSC=0; break;//显示第2位
- case(3):
- LSA=1;LSB=1;LSC=0; break;//显示第3位
- case(4):
- LSA=0;LSB=0;LSC=1; break;//显示第4位
- case(5):
- LSA=1;LSB=0;LSC=1; break;//显示第5位
- case(6):
- LSA=0;LSB=1;LSC=1; break;//显示第6位
- case(7):
- LSA=1;LSB=1;LSC=1; break;//显示第7位
- }
- P0=displaydata[i];//发送数据
- delay(100); //间隔一段时间扫描
- P0=0x00;//消隐
- aj1();
- aj6();
- aj2();
- aj5();
- } break;
- case(1):
- LSA=0;LSB=0;LSC=0;
- P0=displaydata[0];//发送数据
- delay(100); //间隔一段时间扫描
- P0=0x00;//消隐
- LSA=1;LSB=1;LSC=1;
- P0=displaydata[7];
- delay(100);
- P0=0x00;
- aj1();
- aj6();
- aj2();
- if(k3==0)
- {
- delay(1000);
- if(k3==0)
- {
- y1++;
- if(y1==3)y1%=3;
- switch(y1)
- {
- case(0):ds1302write(0x84,0xf&ds1302read(0x85));break;
- case(1):ds1302write(0x84,0x10|ds1302read(0x85));break;
- case(2):ds1302write(0x84,0xf&ds1302read(0x85)|0x20);break;
- }
- }
- }
- while(!k3);
- delay(100);
- aj5();
- break;
- case(2):
- LSA=1;LSB=0;LSC=0;
- P0=displaydata[1];//发送数据
- delay(100); //间隔一段时间扫描
- P0=0x00;//消隐
- LSA=1;LSB=1;LSC=1;
- P0=displaydata[7];
- delay(100);
- P0=0x00;
- aj1();
- aj6();
- aj2();
- if(k3==0)
- {
- delay(1000);
- if(k3==0)
- {
- if(ds1302read(0x85)<0x20)
- {
- y21++;
- if(y21==10)y21%=10;
- switch(y21)
- {
- case(0):ds1302write(0x84,0xf0&ds1302read(0x85));break;
- case(1):ds1302write(0x84,0x1|ds1302read(0x85));break;
- case(2):ds1302write(0x84,0xf0&ds1302read(0x85)|0x2);break;
- case(3):ds1302write(0x84,0x1|ds1302read(0x85));break;
- case(4):ds1302write(0x84,0xf0&ds1302read(0x85)|0x4);break;
- case(5):ds1302write(0x84,0x1|ds1302read(0x85));break;
- case(6):ds1302write(0x84,0xf0&ds1302read(0x85)|0x6);break;
- case(7):ds1302write(0x84,0x1|ds1302read(0x85));break;
- case(8):ds1302write(0x84,0xf0&ds1302read(0x85)|0x8);break;
- case(9):ds1302write(0x84,0x1|ds1302read(0x85));break;
- }
- }
- if(ds1302read(0x85)>=0x20)
- {
- y22++;
- if(y22==4)y22%=4;
- switch(y22)
- {
- case(0):ds1302write(0x84,0xf0&ds1302read(0x85));break;
- case(1):ds1302write(0x84,0x1|ds1302read(0x85));break;
- case(2):ds1302write(0x84,0xf0&ds1302read(0x85)|0x2);break;
- case(3):ds1302write(0x84,0x1|ds1302read(0x85));break;
- }
- }
- }
- }
- while(!k3);
- delay(100);
- aj5();
- break;
- case(3):
- LSA=1;LSB=1;LSC=0;
- P0=displaydata[3];//发送数据
- delay(100); //间隔一段时间扫描
- P0=0x00;//消隐
- LSA=1;LSB=1;LSC=1;
- P0=displaydata[7];
- delay(100);
- P0=0x00;
- aj1();
- aj6();
- aj2();
- if(k3==0)
- {
- delay(1000);
- if(k3==0)
- {
- y3++;
- if(y3==6)y3%=6;
- switch(y3)
- {
- case(0):ds1302write(0x82,0xf&ds1302read(0x83));break;
- case(1):ds1302write(0x82,0x10|ds1302read(0x83));break;
- case(2):ds1302write(0x82,0xf&ds1302read(0x83)|0x20);break;
- case(3):ds1302write(0x82,0x10|ds1302read(0x83));break;
- case(4):ds1302write(0x82,0xf&ds1302read(0x83)|0x40);break;
- case(5):ds1302write(0x82,0x10|ds1302read(0x83));break;
- }
- }
- }
- while(!k3);
- delay(100);
- aj5();
- break;
- case(4):
- LSA=0;LSB=0;LSC=1;
- P0=displaydata[4];//发送数据
- delay(100); //间隔一段时间扫描
- P0=0x00;//消隐
- LSA=1;LSB=1;LSC=1;
- P0=displaydata[7];
- delay(100);
- P0=0x00;
- aj1();
- aj6();
- aj2();
- if(k3==0)
- {
- delay(1000);
- if(k3==0)
- {
- y4++;
- if(y4==10)y4%=10;
- switch(y4)
- {
- case(0):ds1302write(0x82,0xf0&ds1302read(0x83));break;
- case(1):ds1302write(0x82,0x1|ds1302read(0x83));break;
- case(2):ds1302write(0x82,0xf0&ds1302read(0x83)|0x2);break;
- case(3):ds1302write(0x82,0x1|ds1302read(0x83));break;
- case(4):ds1302write(0x82,0xf0&ds1302read(0x83)|0x4);break;
- case(5):ds1302write(0x82,0x1|ds1302read(0x83));break;
- case(6):ds1302write(0x82,0xf0&ds1302read(0x83)|0x6);break;
- case(7):ds1302write(0x82,0x1|ds1302read(0x83));break;
- case(8):ds1302write(0x82,0xf0&ds1302read(0x83)|0x8);break;
- case(9):ds1302write(0x82,0x1|ds1302read(0x83));break;
- }
- }
- }
- while(!k3);
- delay(100);
- aj5();
- break;
- case(5):
- LSA=0;LSB=1;LSC=1;
- P0=displaydata[6];//发送数据
- delay(100); //间隔一段时间扫描
- P0=0x00;//消隐
- LSA=1;LSB=1;LSC=1;
- P0=displaydata[7];
- delay(100);
- P0=0x00;
- aj1();
- aj6();
- aj2();
- if(k3==0)
- {
- delay(1000);
- if(k3==0)
- {
- y5++;
- if(y5==6)y5%=6;
- switch(y5)
- {
- case(0):ds1302write(0x80,0xf&ds1302read(0x81));break;
- case(1):ds1302write(0x80,0x10|ds1302read(0x81));break;
- case(2):ds1302write(0x80,0xf&ds1302read(0x81)|0x20);break;
- case(3):ds1302write(0x80,0x10|ds1302read(0x81));break;
- case(4):ds1302write(0x80,0xf&ds1302read(0x81)|0x40);break;
- case(5):ds1302write(0x80,0x10|ds1302read(0x81));break;
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
DS1302时钟 可按键改写时间!.zip
(47.85 KB, 下载次数: 9)
|