下面是我的Proteus仿真图跟程序,希望大神给意见或指错。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit PWM11 = P1^0; //motor1 引脚
sbit PWM12 = P1^1;
sbit PWM13 = P1^2;
sbit PWM14 = P1^3;
sbit key11 = P1^4;
sbit key12 = P1^5;
sbit key13 = P1^6;
sbit key14 = P1^7;
/* sbit PWM21 = P0^0; //motor2 引脚
sbit PWM22 = P0^1;
sbit PWM23 = P0^2;
sbit PWM24 = P0^3;
sbit key21 = P0^4;
sbit key22 = P0^5;
sbit key23 = P0^6;
sbit key24 = P0^7; */
sbit stop_key2 = P2^0;
sbit stop_key1 = P2^1;
sbit FMQ = P3^6;
uchar ZKB1,ZKB2,ZKB3,ZKB4,ZKB21,ZKB22,ZKB23,ZKB24;
void delaynms(uint z)
{
uint i,j;
for(i = z;i > 0;i--)
for(j = 110;j > 0;j--);
}
void beep(void)
{
uchar t;
for(t=0;t<100;t++)
{
delaynms(1);
FMQ=!FMQ; //产生脉冲
}
FMQ=1; //关闭蜂鸣器
delaynms(300);
}
void main(void)
{
TR0=0; //关闭定时器0,1
TR1=0;
TMOD=0x01; //定时器0,工作方式1
TH0=(65526-100)/256;
TL0=(65526-100)%256; //100us即0.01ms中断一次
TH1=(65536-100)/256;
TL0=(65536-100)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1; //启动定时器T0
ZKB1=3; //占空比初值设定
ZKB2=3; //占空比初值设定
ZKB3=3;
ZKB4=3;
while(1)
{
if(!key11) //电机11正转
{
delaynms(10); //消抖
if(!key11) //确定按键按下
{
if(ZKB1 == 0) //停在最大转速
ZKB1 = 0;
beep();
ZKB1--; //减少ZKB1
}
}
if(!key12) //电机11反转
{
delaynms(10);
if(!key12)
{
if(ZKB2 == 0)
ZKB2 = 0;
beep();
ZKB2--;
}
}
if(!key13) //电机12正转
{
delaynms(10);
if(!key13)
{
if(ZKB3 == 0)
ZKB3 = 0;
beep();
ZKB3--;
}
}
if(!key14) //motor12反转
{
delaynms(10);
if(!key14)
{
if(ZKB4 == 0)
ZKB4 = 0;
beep();
ZKB4--;
}
}
/* if(!key21) //电机21正转
{
delaynms(10); //消抖
if(!key21) //确定按键按下
{
if(ZKB21 == 0) //停在最大转速
ZKB21 = 0;
beep();
ZKB21--; //减少ZKB1
}
}
if(!key22) //电机21反转
{
delaynms(10);
if(!key22)
{
if(ZKB22 == 0)
ZKB22 = 0;
beep();
ZKB22--;
}
}
if(!key23) //电机22正转
{
delaynms(10);
if(!key23)
{
if(ZKB23 == 0)
ZKB23 = 0;
beep();
ZKB23--;
}
}
if(!key24) //motor22反转
{
delaynms(10);
if(!key24)
{
if(ZKB24 == 0)
ZKB24 = 0;
beep();
ZKB24--;
}
} */
if(stop_key1 == 0) //motor1停车
{
ZKB1 = 3;
ZKB2 = 3;
ZKB3 = 3;
ZKB4 = 3;
}
if(stop_key2 == 0) //motor2停车
{
ZKB21 = 3;
ZKB22 = 3;
ZKB23 = 3;
ZKB24 = 3;
}
}
}
void time0(void) interrupt 1
{
static uchar N=0;
TH0=(65526-100)/256;
TL0=(65526-100)%256;
N++;
if(N>3)
N = 0;
if(N<=ZKB1) //电机11 PWM
PWM11 = 0;
if(N > ZKB1)
PWM11 = 1;
if(N<=ZKB2)
PWM12 = 0;
if(N > ZKB2)
PWM12 = 1;
if(N<=ZKB3) //电机12 PWM
PWM13 = 0;
if(N > ZKB3)
PWM13 = 1;
if(N<=ZKB4)
PWM14 = 0;
if(N > ZKB4)
PWM14 = 1;
}
/* void T1_time1(void) interrupt 1
{
static uchar n=0;
TH1=(65526-300)/256;
TL1=(65526-300)%256;
n++;
if(n>3)
n = 0;
if(n<=ZKB21) //电机21 PWM
PWM21 = 0;
if(n > ZKB21)
PWM21 = 1;
if(n<=ZKB22)
PWM22 = 0;
if(n > ZKB22)
PWM22 = 1;
if(n<=ZKB23) //电机22 PWM
PWM23 = 0;
if(n > ZKB23)
PWM23 = 1;
if(n<=ZKB24)
PWM24 = 0;
if(n > ZKB24)
PWM24 = 1;
} */
|