楼主的源码:
- #include"reg51.h"
- #include"LCD1602.h"
- sbit zheng = P3^0;
- sbit fan = P3^1;
- sbit K1 = P1^0;
- sbit K2 = P1^1;
- sbit K3 = P1^2;
- sbit K4 = P1^3;
- unsigned int t=50,tim=0,num=0;
- unsigned char flag=0,sec=0,dir=0,disp=0;
- void delay_ms(unsigned int tim)
- {
- unsigned int i,j;
- for(i=0;i<tim;i++);
- for(j=0;j<120;j++);
- }
- void key_control()
- {
- if(K1==0&&flag==1)
- {
- delay_ms(100);
- //while(!K1);
- if(t<100) t++;
- }
- if(K2==0&&flag==1)
- {
- delay_ms(100);
- //while(!K2);
- if(t>0) t--;
- }
- if(K3==0)
- {
- delay_ms(10);
- while(!K3);
- flag=1-flag;
- }
- if(K4==0)
- {
- delay_ms(10);
- while(!K4);
- dir=1-dir;
- }
- }
- void Display()
- {
- if(flag==1)
- {
- if((100-t)/100==0)
- LCDDispChar(6,1,' ');
- else
- LCDDispNum(6,1,(100-t)/100);
- if((100-t)/100==0&&(100-t)%100/10==0)
- LCDDispChar(7,1,' ');
- else
- LCDDispNum(7,1,(100-t)%100/10);
-
- LCDDispNum(8,1,(100-t)%10);
- LCDDispChar(9,1,'%');
- LCDDispString(11,1,"On ");
- if(dir)
- {
- LCDDispChar(14,1,'Z');
- LCDDispChar(15,1,'z');
- }
- else
- {
- LCDDispChar(14,1,'F');
- LCDDispChar(15,1,'z');
- }
- }
- else
- {
- LCDDispChar(6,1,' ');
- LCDDispChar(7,1,'-');
- LCDDispChar(8,1,'-');
- LCDDispChar(9,1,'-');
- LCDDispChar(14,1,' ');
- LCDDispChar(15,1,' ');
- LCDDispString(11,1,"Off");
- }
- }
- void Display_Speed()
- {
- if(num/100==0)
- LCDDispChar(7,2,' ');
- else
- LCDDispNum(7,2,num/100);
- if(num/100==0&&num%100/10==0)
- LCDDispChar(8,2,' ');
- else
- LCDDispNum(8,2,num%100/10);
- LCDDispNum(9,2,num%10);
- }
- void Timer_Init()
- {
- TMOD = 0x11;
- TH0 = 0x3c;
- TL0 = 0xb0;
- ET0 = 1;
- TR0 = 1;
-
- EX0 = 1;
- IT0 = 1;
-
- TH1 = (65536-1000)/256;
- TL1 = (65536-1000)%256;
- ET1 = 1;
- TR1 = 1;
- EA = 1;
- }
- void main()
- {
- LCDInit();
- LCDDispString(0,1,"Duty :");
- LCDDispString(0,2,"Speed: r/s");
- Timer_Init();
- while(1)
- {
- key_control();
- Display();
- if(disp)
- {
- disp=0;
- Display_Speed();
- num=0;
- }
- }
- }
- void Interrupt1() interrupt 0
- {
- num++;
- }
- void Timer1() interrupt 3
- {
- TH1 = (65536-1000)/256;
- TL1 = (65536-1000)%256;
- if(flag)
- {
- tim++;
- if(dir)
- {
- fan=0;
- if(t<=tim)
- zheng=1;
- else
- zheng=0;
- if(tim==101)
- {
- tim=0;
- zheng=~zheng;
- }
- }
- else
- {
- zheng=0;
- if(t<=tim)
- fan=1;
- else
- fan=0;
- if(tim==101)
- {
- tim=0;
- fan=~fan;
- }
- }
- }
- else
- {
- zheng=1;
- fan =1;
- }
- }
- void Timer0() interrupt 1
- {
- TH0 = 0x3c;
- TL0 = 0xb0;
- sec++;
- if(sec==20)
- {
- sec=0;
- disp=1;
- }
- }
复制代码 |