- #include<stdio.h> //sb1:run sb2:stop sb3:clear
- #include<stc15f2k60s2.h>
- #include<intrins.h>
- #define LEDshu P0
- #define LCD_1602 P2
- #define uchar unsigned char
- sbit cs1=P1^0;
- sbit cs2=P1^1;
- sbit wr=P1^2;
- sbit RS=P1^3;
- sbit RW=P1^4;
- sbit E=P1^5;
- sbit BF=P2^7;
- sbit row0=P3^0; sbit row1=P3^1; sbit row2=P3^2; sbit row3=P3^3;
- sbit col0=P3^4; sbit col1=P3^5; sbit col2=P3^6; sbit col3=P3^7;
- unsigned char k=10;
- unsigned char v;
- int q;
- void Delay1us();
- void Delay1000ms();
- void Write_Data(uchar dat);
- void Write_Command(uchar com);
- void Init1602();
- void String1602(uchar *str);
- void Adderss1602(uchar addr);
- unsigned char keyscan();
- unsigned char shu2();
- /////////////////数码管///////////////////////
- unsigned char shu1();
- unsigned char L1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
- unsigned char L2[12]={0,1,2,3,4,5,6,7,8,9,10,11};
- unsigned char a;//1110 1111 0xef
- unsigned char b;//1111 0111 0xf7
- //1111 1011 0xfb
- unsigned char c;//1111 1101 0xfd
- unsigned char d;//1111 1110 0xfe
- ////////////////////////////////////////
- unsigned char keyscan()
- {
- col0=col1=col2=col3=row0=row2=row3=1;
- row0=0;
- if(col0==0)k=0;
- else if(col1==0)k=1;
- else if(col2==0)k=2;
- row0=1;
- return k;
- }
- bit busy1602()
- {
- bit busy;
- LCD_1602=0xff;
- RS=0;
- RW=1;
- Delay1us();
- E=1;
- busy=BF;
- E=0;
- Delay1us();
- return busy;
- }
- main()
- {
- P0M0=0xff;
- P0M1=0X00;
- TL0=(65536-50000)%0x100;
- TH0=(65536-50000)/0x100;
- TMOD=0x01; //T0方式1即16位定时
- EA=1; //开总中断
- ET0=1; //开T0中断
- Init1602();
- while(1)
- {
- RS=RW=E=1;
- keyscan();
- if(k==0){v=0;shu2();TR0=1;shu1();}
- if(k==1){v=1;shu2();TR0=0;shu1();}
- if(k==2){v=2;shu2();a=b=c=d=0;shu1();}
- }
- }
- unsigned char shu2()
- {
- if(v==0){Adderss1602(0x00);String1602("------run------");}
- if(v==1){ Adderss1602(0x00);String1602("------stop-----");}
- if(v==2){ Adderss1602(0x00);String1602("-----clear-----");}
- }
- unsigned char shu1()
- {
- cs1=0;cs2=1;
- LEDshu=L1[L2[a]];
- wr=1;
- wr=0;
- cs1=1;
- cs2=0;
- LEDshu=0xef;
- wr=1;
- wr=0;
- Delay1us();
- cs1=cs2=0;
- LEDshu=0xff;
- wr=1;
- wr=0;
- ///////////////////////////////////
- cs1=0;cs2=1;
- LEDshu=L1[L2[b]];
- wr=1;
- wr=0;
- cs1=1;
- cs2=0;
- LEDshu=0xf7;
- wr=1;
- wr=0;
- Delay1us();
- cs1=cs2=0;
- LEDshu=0xff;
- wr=1;
- wr=0;
- //////////////////////////////////////////
- cs1=0;cs2=1;
- LEDshu=L1[L2[11]];
- wr=1;
- wr=0;
- cs1=1;
- cs2=0;
- LEDshu=0xfb;
- wr=1;
- wr=0;
- Delay1us();
- cs1=cs2=0;
- LEDshu=0xff;
- wr=1;
- wr=0;
-
- ///////////////////////////////////////////////
- cs1=0;cs2=1;
- LEDshu=L1[L2[c]];
- wr=1;
- wr=0;
- cs1=1;
- cs2=0;
- LEDshu=0xfd;
- wr=1;
- wr=0;
- Delay1us();
- cs1=cs2=0;
- LEDshu=0xff;
- wr=1;
- wr=0;
-
- //////////////////////////////////////////
- cs1=0;cs2=1;
- LEDshu=L1[L2[d]];
- wr=1;
- wr=0;
- cs1=1;
- cs2=0;
- LEDshu=0xfe;
- wr=1;
- wr=0;
- Delay1us();
- cs1=cs2=0;
- LEDshu=0xff;
- wr=1;
- wr=0;
-
- }
- //////////////////////////////////
- void Write_Data(uchar dat)
- {
- while(busy1602()==1);
- RS=1;RW=0;
- Delay1us();
- E=1;
- LCD_1602=dat;
- Delay1us();
- E=0;
- }
- void Write_Command(uchar com)
- {
- while(busy1602()==1);
- RS=0;RW=0;
- Delay1us();
- E=1;
- LCD_1602=com;
- Delay1us();
- E=0;
- }
- void Init1602()
- {
- Write_Command(0x38);
- Write_Command(0x0f);
- Write_Command(0x06);
- Write_Command(0x01);
- }
- void String1602(uchar *str)
- {
- while(*str != '\0')
- {
- Write_Data(*str++);
- }
- }
- void Adderss1602(uchar addr)
- {
- Write_Command(addr | 0x80);
- }
- /////////////////////////////////////////
- void Delay1us() //@11.0592MHz
- {
- _nop_();
- _nop_();
- _nop_();
- }
- void Delay1000ms() //@11.0592MHz
- {
- unsigned char i, j, h;
- _nop_();
- _nop_();
- i = 43;
- j = 6;
- h = 203;
- do
- {
- do
- {
- while (--h);
- } while (--j);
- } while (--i);
- }
- time0() interrupt 1
- {
- {
- TL0=(65536-50000)%0x100;
- TH0=(65536-50000)/0x100;//50ms
- q++;
- if(q>=20)
- {
- q=0;//1s
- ////////////////////////////////
- if(d<10)
- {
- d++;
- if(d>=10&&c<6)
- {
- d=0;c++;
- if(c>=6&&b<10)
- {
- c=0;b++;
- if(b>=10&&a<6)
- {
- b=0;a++;
- if(a>=6)
- {
- a=0;
- }
- }
- }
- }
- }
- ///////////////////////////////////////
- }
- }
- }
复制代码 |