我改成這個樣子了.準備加按鍵調時間.時間可以顯示,只是按鍵調不了
- #include "reg51.h"
- sbit rs=P2^6;
- sbit rw=P2^5;
- sbit re=P2^7;
- sbit K1=P3^1;
- sbit K2=P3^0;
- sbit K3=P3^2;
- typedef unsigned char uchar;
- typedef unsigned int uint;
- uchar led[10]="0123456789";
- uchar led1=':';
- uchar led3[10]="2017/04/07";
- uint cnt=0,num=0;
- uchar sec=0,sec1=0,minut=0,minut1=0,hour=0,hour1=0;
- uchar seca=0,sec1a=0,minta=0,mint1a=0,houra=0,hour1a=0;
- uchar cn,kt,mt;
- void delay(uint x)
- {
- uchar j;
- while(x--)
- for(j=0;j<8;j++)
- {}
- }
- void rwa(uchar i)
- {
- rs=0;
- rw=0;
- re=0;
- P0=i;
- re=1;
- delay(1);
- re=0;
- }
- void rwb(uchar j)
- {
- rs=1;
- rw=0;
- re=0;
- P0=j;
- re=1;
- delay(1);
- re=0;
- }
- void init()
- {
- delay(100);
- rwa(0x38);
- rwa(0x38);
- rwa(0x38);
- rwa(0x0c);
- rwa(0x0f);
- rwa(0x06);
- // rwa(0x01);
- }
- void timerinit()
- {
- TMOD|=0X01;
- TH0=0XFC;
- TL0=0X67;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void show()
- {
- uchar i;
- rwa(0x42+0x80);
- for(i=0;i<10;i++)
- {
- rwb(led3[i]);
- delay(1);
- }
- rwa(0x00+0x80+3);
- rwb(led[hour1]);
- delay(1);
- rwa(0x00+0x80+4);
- rwb(led[hour]);
- delay(1);
- rwa(0x00+0x80+5);
- rwb(led1);
- delay(1);
- rwa(0x00+0x80+6);
- rwb(led[minut1]);
- delay(1);
- rwa(0x00+0x80+7);
- rwb(led[minut]);
- delay(1);
- rwa(0x00+0x80+8);
- rwb(led1);
- delay(1);
- rwa(0x00+0x80+9);
- rwb(led[sec1]);
- delay(1);
- rwa(0x00+0x80+10);
- rwb(led[sec]);
- delay(1);
-
- }
- void key()
- {
- if(K1==0)
- {
- delay(5);
- if(K1==0)
- {
- kt++;
- while(!K1);
- }
- if(kt==1)
- {
- TR0=0;
- rwa(0x00+0x80+10);
- rwb(0x0f);
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- mt++;
- seca=sec+mt;
- if(sec==10)
- {
- sec=0;
- mt=0;
- }
- }
- }
- num=hour1*36000+hour*3600+minut1*600+minut*60+sec1*10+seca;
- while(!K3);
- delay(5);
- }
- if(kt==2)
- {
- rwa(0x00+0x80+9);
- rwb(0x0f);
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- mt++;
- sec1a=sec1+mt;
- if(sec1a==6)
- {
- sec1a=0;
- mt=0;
- }
- }
- }
- num=hour1*36000+hour*3600+minut1*600+minut*60+sec1a*10+seca;
- while(!K3);
- delay(5);
- }
- if(kt==3)
- {
- rwa(0x00+0x80+7);
- rwb(0x0f);
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- mt++;
- minta=minut+mt;
- if(minta==10)
- {
- minta=0;
- mt=0;
- }
- }
- }
- num=hour1*36000+hour*3600+minut1*600+minta*60+sec1a*10+seca;
- while(!K3);
- delay(5);
- }
- if(kt==4)
- {
- rwa(0x00+0x80+6);
- rwb(0x0f);
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- mt++;
- mint1a=minut1+mt;
- if(mint1a==10)
- {
- mint1a=0;
- mt=0;
- }
- }
- }
- num=hour1*36000+hour*3600+mint1a*600+minta*60+sec1a*10+seca;
- while(!K3);
- delay(5);
- }
- if(kt==5)
- {
- rwa(0x00+0x80+4);
- rwb(0x0f);
- if(K3==0)
- {
- delay(5);
- if(K3==0)
- {
- mt++;
- houra=hour+mt;
- if(houra==10)
- {
- houra=0;
- mt=0;
- hour1a++;
- }
- if(houra+mt==24)
- {
- houra=0;
- hour1a=0;
- mt=0;
- }
- }
- }
- num=hour1a*36000+houra*3600+mint1a*600+minta*60+sec1a*10+seca;
- while(!K3);
- delay(5);
- }
-
- if(kt==6)
- {
- cn=0;
- kt=0;
- TR0=1;
- rwb(0x0c);
- }
- }
- }
復制代碼 |