- #include <stc12c5a60s2.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define LongToBin(n) (((n>>21)&0x80)|((n>>18)&0x40)|((n>>15)&0x20)|((n>>12)&0x10)|((n>>9)&0x08)|((n>>6)&0x04)|((n>>3)&0x02)|((n)&0x01))
- #define BIN(n) LongToBin(0x##n##)
- sbit H1=P2^0;
- sbit H2=P2^1;
- sbit L1=P2^2;
- sbit L2=P2^3;
- sbit anjian=P3^2;
- uint yanshi;
- char moshi=0;
- char flag=0;
- void delay_1ms(uint q)
- {
- uint i,j;
- for(i=0;i<q;i++)
- for(j=0;j<120;j++);
- }
- void dh1()
- {
- char i;
- yanshi=400;
- P0=0XFF;
- L1=L2=1;
- L1=L2=0;
- for(i=0;i<12;i++)
- {
- P0=0X01;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- P0=0X02;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- P0=0X04;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- P0=0X08;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- P0=0X10;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- P0=0X20;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- P0=0X40;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- P0=0X80;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(yanshi);
- }
- }
- void dh2()
- {
- char i,j;
- yanshi=500;
- P0=0XFF;
- H1=H2=1;
- H1=H2=0;
- for(i=0;i<1;i++)
- {
- P0=0X00;
- L1=1;
- L1=0;
- P0=0X01;
- L2=1;
- L2=0;
- if(anjian==0){while(anjian==0);moshi=1;break;}
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=P0<<1;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- P0=0X00;
- L2=1;
- L2=0;
- P0=0X01;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- for(j=0;j<7;j++)
- {
- P0=P0<<1;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- P0=0X80;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=P0>>1;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- P0=0X00;
- L1=1;
- L1=0;
-
- P0=0X80;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=P0>>1;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- }
- }
- void dh3()
- {
- char i,j;
- yanshi=400;
- P0=0XFF;
- H1=H2=1;
- H1=H2=0;
- for(i=0;i<2;i++)
- {
- P0=0X01;
- L2=1;
- L2=0;
- if(anjian==0){while(anjian==0);moshi=1;break;}
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=(P0<<1)|0x01;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- P0=0X01;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=(P0<<1)|0x01;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- delay_1ms(5000);
- P0=0X7F;
- for(j=0;j<7;j++)
- {
- P0=P0>>1;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
-
- P0=0X7F;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=P0>>1;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- }
- }
- void dh4()
- {
- char i;
- P0=0X81;
- L1=1;
- L1=0;
- P0=0X02;
- L2=1;
- L2=0;
- for(i=0;i<5;i++)
- {
- P0=0X33;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- P0=0X66;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- P0=0XCC;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- P0=0X99;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- }
- }
- char s1,s2;
- void dh5()
- {
- char i,j;
- yanshi=500;
- P0=0XFF;
- H1=H2=1;
- H1=H2=0;
- for(i=0;i<5;i++)
- {
- s1=0x01;
- s2=0x80;
- P0=s1;
- L1=1;
- L1=0;
- P0=s2;
- L2=1;
- L2=0;
- if(anjian==0){while(anjian==0);moshi=1;break;}
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- s2=(s2>>1)&0x7F;
- P0=s2;
- L2=1;
- L2=0;
- s1=s1<<1;
- P0=s1;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- }
- }
- void dh6()
- {
- char i,j;
- yanshi=500;
- P0=0XFF;
- H1=H2=1;
- H1=H2=0;
- for(i=0;i<5;i++)
- {
- P0=0x00;
- L2=1;
- L2=0;
- P0=0x80;
- L1=1;
- L1=0;
- if(anjian==0){while(anjian==0);moshi=1;break;}
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=(P0>>1)|0x80;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
-
- P0=0x80;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=(P0>>1)|0x80;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- }
- }
- void dh7()
- {
- char i;
- P0=0XFF;
- L1=1;
- L1=0;
- P0=0XFF;
- L2=1;
- L2=0;
- for(i=0;i<5;i++)
- {
- P0=0X11;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(700);
- P0=0X22;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(700);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- P0=0X44;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(700);
- P0=0X88;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(700);
- }
- }
- void dh8()
- {
- char i;
- P0=0XFF;
- L1=1;
- L1=0;
- P0=0XFF;
- L2=1;
- L2=0;
- for(i=0;i<5;i++)
- {
- P0=0X33;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- P0=0X66;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- P0=0XCC;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- P0=0X99;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- }
- }
- void dh9()
- {
- char i;
- P0=0XFF;
- L1=1;
- L1=0;
- P0=0XFF;
- L2=1;
- L2=0;
- for(i=0;i<10;i++)
- {
- if(anjian==0){while(anjian==0);moshi=1;break;}
- P0=0X55;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- P0=0XAA;
- H1=1;
- H1=0;
- H2=1;
- H2=0;
- delay_1ms(600);
- }
- }
- void dh10()
- {
- char i,j;
- yanshi=500;
- P0=0X01;
- H1=H2=1;
- H1=H2=0;
- for(i=0;i<5;i++)
- {
- P0=0x00;
- L2=1;
- L2=0;
- P0=0x80;
- L1=1;
- L1=0;
- if(anjian==0){while(anjian==0);moshi=1;break;}
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=(P0>>1)|0x80;
- L1=1;
- L1=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
-
- P0=0x80;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- for(j=0;j<7;j++)
- {
- P0=(P0>>1)|0x80;
- L2=1;
- L2=0;
- delay_1ms(yanshi);
- if(anjian==0){while(anjian==0);moshi=1;break;}
- }
- }
- }
- uchar STC_ADC() //!!根据数据手册写一个ad读取函数
- {
- uchar i;
- ADC_RES = 0;
- ADC_RESL = 0;
- ADC_CONTR = BIN(10001000);
- i=3;
- while(i--);
- while (1)
- {
- if (ADC_CONTR & BIN(10000))
- {
- break;
- }
- }
- ADC_CONTR = BIN(10000000);
- return( ADC_RESL<<2) ;
- }
- uchar date;
- void init()
- {
- TMOD= 0x10;
- TL1 = (65536-50000)/256; //设置定时初值
- TH1 = (65536-50000)%256; //设置定时初值
- ET1 = 1;
- TR1 = 1;
- EA = 0;
- }
- char h=0,v=0;
- void pp()
- {
- if(date<=15 && date>0)
- {
- h=1;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0X01;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X01;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
- if(date<=30 && date>15)
- {
- h=2;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0X03;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X02;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
-
- if(date<=45 && date>30)
- {
- h=3;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0X07;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X04;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
- if(date<=60 && date>45)
- {
- h=4;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0X0F;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X08;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
- if(date<=75 && date>60)
- {
- h=5;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0X1F;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X10;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
- if(date<=90 && date>75)
- {
- h=6;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0X3F;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X20;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
- if(date<=105 && date>90)
- {
- h=7;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0X7F;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X40;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
- if(date<=130 && date>105)
- {
- h=8;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X80;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X00;
- L1=1;
- L1=0;
- }
- }
- if(date<=145 && date>130)
- {
- h=9;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X01;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X01;
- L1=1;
- L1=0;
- }
- }
- if(date<=160 && date>145)
- {
- h=10;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X03;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X02;
- L1=1;
- L1=0;
- }
- }
- if(date<=175 && date>160)
- {
- h=11;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X07;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X04;
- L1=1;
- L1=0;
- }
- }
- if(date<=190 && date>175)
- {
- h=12;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X0F;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X08;
- L1=1;
- L1=0;
- }
- }
- if(date<=205 && date>190)
- {
- h=13;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X1F;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X10;
- L1=1;
- L1=0;
- }
- }
- if(date<=220 && date>205)
- {
- h=14;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X3F;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X20;
- L1=1;
- L1=0;
- }
- }
- if(date<=235 && date>220)
- {
- h=15;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X7F;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X40;
- L1=1;
- L1=0;
- }
- }
- if(date>235)
- {
- h=16;
- if(v<h)v=h;
- if(flag==0)
- {
- P0=0XFF;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0XFF;
- L1=1;
- L1=0;
- }
- if(flag==1)
- {
- P0=0X00;
- L2=1;
- L2=0;
- delay_1ms(2);
- P0=0X80;
- L1=1;
- L1=0;
- }
- }
- }
- void main()
- {
- P2M0=0xff;// BIN(11111111);//P2组设置为推挽输出
- P2M1=0x00;
- P3M0=0xff;// BIN(11111111);//P3组设置为推挽输出
- P3M1=0x00;
- P1M0=0x00;
- P1M1=0x01;
- P1ASF =1; //设置P1.0为AD口
- AUXR1 =BIN(100);
- init();
- P2=0X00;
- P3=0X04;
- P0=0XFF;
- L1=L2=1;
- L1=L2=0;
- P0=0XFF;
- H1=H2=1;
- H1=H2=0;
- delay_1ms(8000);
- delay_1ms(8000);
- delay_1ms(8000);
- delay_1ms(8000);
- delay_1ms(8000);
- while(1)
- {
- if(moshi==0)
- {
- EA=0;
- if(moshi==0)dh1();
- if(moshi==0)dh7();
- if(moshi==0)dh2();
- if(moshi==0)dh3();
- if(moshi==0)dh4();
- if(moshi==0)dh5();
- if(moshi==0)dh6();
- if(moshi==0)dh8();
- if(moshi==0)dh9();
- if(moshi==0)dh10();
- }
- if(moshi==1)
- {
- EA=1;
- if(anjian==0){while(anjian==0);moshi=0;}
- date=STC_ADC();
- pp();
- delay_1ms(5);
- }
- }
- }
- int t=0,t1,t2;
- void InitTimer1() interrupt 3 // 1毫秒@11.0592MHz
- {
- TL1 = (65536-30000)/256; //设置定时初值
- TH1 = (65536-30000)%256; //设置定时初值
- t++;
- if(t==5)
- {
- t=0;
- if(v==0);
- else v--;
- t1++;
- if(t1>=0 && t1<300)flag=0;
- if(t1>=300 && t1<600)flag=1;
- if(t1>=600)t1=0;
- t2++;
- if(t2>=0 && t2<600)
- {
- P0=0XFF;
- H1=H2=1;
- H1=H2=0;
- }
- if(t2>=600 && t2<1200)
- {
- P0=0X11;
- H1=H2=1;
- H1=H2=0;
- }
- if(t2>=1200)t2=0;
- }
- if(flag==0)
- {
- if(v==16){P0=0X7F;L1=1;L1=0;}
- if(v==15){P0=0X3F;L1=1;L1=0;}
- if(v==14){P0=0X1F;L1=1;L1=0;}
- if(v==13){P0=0X0F;L1=1;L1=0;}
- if(v==12){P0=0X07;L1=1;L1=0;}
- if(v==11){P0=0X03;L1=1;L1=0;}
- if(v==10){P0=0X01;L1=1;L1=0;}
- if(v==9) {P0=0X00;L1=1;L1=0;}
- if(v==8) {P0=0X00;L1=1;L1=0;P0=0X7F;L2=1;L2=0;}
- if(v==7) {P0=0X00;L1=1;L1=0;P0=0X3F;L2=1;L2=0;}
- if(v==6) {P0=0X00;L1=1;L1=0;P0=0X1F;L2=1;L2=0;}
- if(v==5) {P0=0X00;L1=1;L1=0;P0=0X0F;L2=1;L2=0;}
- if(v==4) {P0=0X00;L1=1;L1=0;P0=0X07;L2=1;L2=0;}
- if(v==3) {P0=0X00;L1=1;L1=0;P0=0X03;L2=1;L2=0;}
- if(v==2) {P0=0X00;L1=1;L1=0;P0=0X01;L2=1;L2=0;}
- if(v==1) {P0=0X00;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==0) {P0=0X00;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- }
- if(flag==1)
- {
- if(v==16){P0=0X80;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==15){P0=0X40;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==14){P0=0X20;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==13){P0=0X10;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==12){P0=0X08;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==11){P0=0X04;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==10){P0=0X02;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==9) {P0=0X01;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- if(v==8) {P0=0X00;L1=1;L1=0;P0=0X80;L2=1;L2=0;}
- if(v==7) {P0=0X00;L1=1;L1=0;P0=0X40;L2=1;L2=0;}
- if(v==6) {P0=0X00;L1=1;L1=0;P0=0X20;L2=1;L2=0;}
- if(v==5) {P0=0X00;L1=1;L1=0;P0=0X10;L2=1;L2=0;}
- if(v==4) {P0=0X00;L1=1;L1=0;P0=0X08;L2=1;L2=0;}
- if(v==3) {P0=0X00;L1=1;L1=0;P0=0X04;L2=1;L2=0;}
- if(v==2) {P0=0X00;L1=1;L1=0;P0=0X02;L2=1;L2=0;}
- if(v==1) {P0=0X00;L1=1;L1=0;P0=0X01;L2=1;L2=0;}
- if(v==0) {P0=0X00;L1=1;L1=0;P0=0X00;L2=1;L2=0;}
- }
- }
复制代码
|