要求是用5V电源供电,设计四个按钮K1为全速,K2为停止,K3为半速,K4为自然风(刚开始每4s的时间内有1s在转动,并过8s后每4s的时间内有2s在转动,再过8s后每4s的时间内有3s在转动,再过8s后全速转动,再过8s后4s中又只有1s在转动,如此循环复始)。并分别用四个指导灯指示这四种状态,但是自然风按下,没效果,而且其他键也用不了了,若要修改需要怎么做,请教大神。
#include"reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit dj=P1^0;
sbit k1=P3^1; //半速键
sbit k2=P3^0; //全速键
sbit k3=P3^2; //自然风键
sbit k4=P3^3; //停止键
sbit smgwei1=P2^2;
sbit smgwei2=P2^3;
sbit smgwei3=P2^4;
u16 T1_H,T1_L,T0_H,T0_L;
u8 a,fr; //a表示占空比:0-100,fr*100后为PWM频率值:1-255。
u8 code smgduan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0-F段码
void delay(u16 i)
{
while(i--);
}
void init()
{
TMOD=0X01;
EA=1;
ET0=1;
}
void display()
P0=smgduan[a/10];smgwei1=1; smgwei2=0;smgwei3=1;delay(100);P0=0X00;
P0=smgduan[a%10];smgwei1=0; smgwei2=0;smgwei3=1;delay(100);P0=0X00; }
void key()
{
if(k1==0) {delay(1000);while(!k1);TR0=1;a=50;}
if(k2==0) {delay(1000);while(!k2);TR0=1;a=100;}
if(k3==0) {delay(1000);while(!k3);TR0=1;
a=100;
delay(1000);
a=0;
delay(3000);
a=100;
delay(1000);
a=0;
delay(3000); //前8秒
a=100;
delay(2000);
a=0;
delay(2000);
a=100;
delay(2000);
a=0;
delay(2000); //又8秒
a=100;
delay(3000);
a=0;
delay(1000);
a=100;
delay(3000);
a=0;
delay(1000); //又8秒
a=100;
delay(8000);
}
if(k4==0) {delay(1000);while(!k4);TR0=0;dj=0;a=0;}
}
void time0 ()interrupt 1
{
dj=~dj;
if(dj==1) { TH0=T1_H;TL0=T1_L; }
if(dj==0) { TH0=T0_H;TL0=T0_L; }
}
void chuzhi()
{
T0_H=(65535-10000/fr+a*100/fr)/256;
T0_L=(65535-10000/fr+a*100/fr)%256;
//断电时间初值;
T1_H=(65535-a*100/fr)/256;
T1_L=(65535-a*100/fr)%256;
//供电时间初值;
}
main()
{
dj=0;fr=5;
init();
while(1)
{
chuzhi();
key();
display();
}
}
|