数码管显示部分的原理图:http://www.51hei.com/f/51hei-5.pdf
源代码:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit wela=P2^7;
- sbit dula=P2^6;
- sbit scl=P2^0;
- sbit sda=P2^1;
- sbit rst=P2^4;
- uchar code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar code smg_we[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
- uchar dis_smg[6];
- uchar flag,keyscan,flag_s,key_set;
- uchar miao,fen,shi;
- bit flag_500ms;
- uchar read_ds1302(uchar add);
- void write_ds1302(uchar add,uchar dat);
- void write_SFM();
- void delay_1ms(uchar t)
- {
- uchar i,j;
- for(i=0;i<t;i++)
- for(j=0;j<120;j++);
- }
- uchar bcdtoasc(uchar cha)
- {
- cha=((cha/16)*10+(cha%16));
- return cha;
- }
- uchar asctobcd(uchar cha)
- {
- cha=((cha/10)*16+(cha%10));
- return cha;
- }
- uchar bdata bdat;
- sbit dat0=bdat^0;
- sbit dat1=bdat^1;
- sbit dat2=bdat^2;
- sbit dat3=bdat^3;
- sbit dat4=bdat^4;
- sbit dat5=bdat^5;
- sbit dat6=bdat^6;
- sbit dat7=bdat^7;
- void smg_s(uchar dat)
- { bdat=dat;
- if(dat0==1)
- dis_smg[0]=0x00;
- if(dat1==1)
- dis_smg[1]=0x00;
- if(dat2==1)
- dis_smg[2]=0x00;
- if(dat3==1)
- dis_smg[3]=0x00;
- if(dat4==1)
- dis_smg[4]=0x00;
- if(dat5==1)
- dis_smg[5]=0x00;
- if(dat6==1)
- dis_smg[6]=0x00;
- if(dat7==1)
- dis_smg[7]=0x00;
-
- }
- void scan()
- {
- static uchar i;
- uchar key;
- keyscan=10;
- keyscan=10;
- P3=0xf0;
- key=P3;
- key=key&0xf0;
- if(key!=0xf0)
- {
- i++;
- if(i>=7)
- if(flag==0)
- {
- flag=1;
- switch(key)
- {
- case 0xe0:keyscan=1;break;
- case 0xd0:keyscan=2;break;
- case 0xb0:keyscan=3;break;
- }
- }
- }
- else{flag=0;i=0;}
- }
- void clock_can()
- {
- delay_1ms(5);
- if(keyscan==1)
- {
- write_ds1302(0x8e,0x00);
- write_ds1302(0x80,0x80);
- write_ds1302(0x8e,0x80);
- key_set++;
- if(key_set==1)
- flag_s=0x30;
- if(key_set==2)
- flag_s=0x0c;
- if(key_set==3)
- flag_s=0x03;
- if(key_set>=4)
- {
- key_set=0;
- flag_s=0x00;
- write_ds1302(0x8e,0x00);
- write_ds1302(0x80,0x00);
- write_ds1302(0x8e,0x80);
- }
- }
- if(keyscan==2)
- {
- if(key_set==1)
- {
- write_ds1302(0x8e,0x00);
- shi=read_ds1302(0x85);
- shi=bcdtoasc(shi);
- shi++;
- if(shi==24)
- shi=0;
- shi=asctobcd(shi);
- write_ds1302(0x84,shi);
- write_ds1302(0x8e,0x80);
- }
- if(key_set==2)
- {
- write_ds1302(0x8e,0x00);
- fen=read_ds1302(0x83);
- fen=bcdtoasc(fen);
- fen++;
- if(fen==60)
- fen=0;
- fen=asctobcd(fen);
- write_ds1302(0x82,fen);
- write_ds1302(0x8e,0x80);
- }
- if(key_set==3)
- {
- write_ds1302(0x8e,0x00);
- miao=read_ds1302(0x81);
- miao=bcdtoasc(miao);
- miao++;
- if(miao==60)
- miao=0;
- miao=asctobcd(miao);
- write_ds1302(0x80,miao);
- write_ds1302(0x8e,0x80);
- }
- }
- if(keyscan==3)
- {
- if(key_set==1)
- {
- write_ds1302(0x8e,0x00);
- shi=read_ds1302(0x85);
- shi=bcdtoasc(shi);
- if(shi==0)
- shi=24;
- shi--;
- shi=asctobcd(shi);
- write_ds1302(0x84,shi);
- write_ds1302(0x8e,0x80);
- }
- if(key_set==2)
- {
- write_ds1302(0x8e,0x00);
- fen=read_ds1302(0x83);
- fen=bcdtoasc(fen);
- if(fen==0)
- fen=60;
- fen--;
- fen=asctobcd(fen);
- write_ds1302(0x82,fen);
- write_ds1302(0x8e,0x80);
- }
- if(key_set==3)
- {
- write_ds1302(0x8e,0x00);
- miao=read_ds1302(0x81);
- miao=bcdtoasc(miao);
- if(miao==0)
- miao=60;
- miao--;
- miao=asctobcd(miao);
- write_ds1302(0x80,miao);
- write_ds1302(0x8e,0x80);
- }
- }
- }
- void write_ds1302(uchar add,uchar dat)
- {
- uchar i;
- rst=1;
- for(i=0;i<8;i++)
- {
- scl=0;
- sda=add&0x01;
- scl=1;
- add>>=1;
- }
- for(i=0;i<8;i++)
- {
- scl=0;
- sda=dat&0x01;
- scl=1;
- dat>>=1;
- }
- rst=0;
- scl=0;
- sda=0;
- }
- uchar read_ds1302(uchar add)
- {
- uchar i,value;
- rst=1;
- for(i=0;i<8;i++)
- {
- scl=0;
- sda=add&0x01;
- scl=1;
- add>>=1;
- }
- for(i=0;i<8;i++)
- {
- scl=0;
- value>>=1;
- if(sda==1)
- value|=0x80;
- scl=1;
- }
- rst=0;
- scl=0;
- sda=0;
- return value;
- }
- void write_SFM()
- {
- scl=0;rst=0;
- write_ds1302(0x8e,0x00);
- write_ds1302(0x80,0x55);
- write_ds1302(0x82,0x59);
- write_ds1302(0x84,0x23);
- write_ds1302(0x8e,0x80);
- }
- void dis_init()
- {
- miao=read_ds1302(0x81);;
- fen =read_ds1302(0x83);;
- shi =read_ds1302(0x85);
- dis_smg[0]=smg_du[miao%16]; ;
- dis_smg[1]=smg_du[miao/16]; ;
- dis_smg[2]=smg_du[fen%16]|0x80; ;
- dis_smg[3]=smg_du[fen/16]; ;
- dis_smg[4]=smg_du[shi%16]|0x80; ;
- dis_smg[5]=smg_du[shi/16];
- }
- void display()
- {
- static uchar i;
- P0=0x00;
- dula=1;dula=0;
- P0=smg_we[i];
- wela=1;wela=0;
- P0=dis_smg[5-i];
- dula=1;dula=0;
- i++;
- if(i>=6)i=0;
- }
- void init_time0()
- {
- TMOD=0X01;
- TR0=1;
- ET0=1;
- EA=1;
- }
- void main()
- {
- write_SFM();
- init_time0();
- while(1)
- {
- if(flag_500ms)
- dis_init();
- else
- smg_s(flag_s);
-
- scan();
- clock_can();
- display();
- }
- }
- void timer0() interrupt 1
- {
- uchar i;
- TH0=0xdb;
- TL0=0xf0;
- i++;
- if(i>=50)
- {
- i=0;
- flag_500ms=~flag_500ms;
- }
- }
复制代码 |