按键控制电机PWM调速 c程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
sbit PWM_OUT1P=P0^5;
sbit PWM_OUT2P=P0^6;
sbit PWM_OUT3P=P0^7;
sbit key1=P0^0;
sbit key2=P0^1;
bit PWM_OUTDAT1;
bit PWM_OUTDAT2;
bit PWM_OUTDAT3;
bit delaybit1=0; //延时开关
bit delaybit2=0;
unsigned int PWM_t1=0;
unsigned int PWM_t2=0;
unsigned int PWM_t3=0;
unsigned int delaytime_res1=0;//延时长度计数
unsigned int delaytime_res2=0;
unsigned int keybit=0; //按键计数
PWM_OUT1(unsigned int Cycle1,unsigned int PWM_on1);
PWM_OUT2(unsigned int Cycle2,unsigned int PWM_on2);
PWM_OUT3(unsigned int Cycle3,unsigned int PWM_on3);
void main()
{
TMOD=0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%6;
IE=0x82;
TR0=1;
key1=1;
key2=1;
keybit=0;
while(1)
{
if(key1==0)
{
delaybit1=1;
if(delaytime_res1==2)
{
if(key1==0)
{
if(keybit<7)
keybit++;
}
}
}
else
{
delaytime_res1=0;
delaybit1=0;
}
if(key2==0)
{
delaybit2=1;
if(delaytime_res2==2)
{
if(key2==0)
{
if(keybit>0)
keybit--;
}
}
}
else
{
delaybit2=0;
delaytime_res2=0;
}
switch(keybit)
{
case 1:
PWM_OUT1P=PWM_OUT1(1000,900);
PWM_OUT2P=PWM_OUT2(1000,900);
break;
case 2:
PWM_OUT1P=PWM_OUT1(1000,700);
PWM_OUT2P=PWM_OUT2(1000,700);
break;
case 3:
PWM_OUT1P=PWM_OUT1(1000,500);
PWM_OUT2P=PWM_OUT2(1000,500);
break;
case 4:
PWM_OUT1P=PWM_OUT1(1000,300);
PWM_OUT2P=PWM_OUT2(1000,300);
break;
case 5:
PWM_OUT1P=PWM_OUT1(1000,100);
PWM_OUT2P=PWM_OUT2(1000,100);
break;
case 6:
PWM_OUT1P=PWM_OUT1(1000,0);
PWM_OUT2P=PWM_OUT2(1000,0);
break;
}
}
}
void Time_r() interrupt 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%6;
PWM_t1++;
PWM_t2++;
PWM_t3++;
if(delaybit1==1)
{
delaytime_res1++;
}
if(delaybit2==1)
{
delaytime_res2++;
}
}
PWM_OUT1(unsigned int Cycle1,unsigned int PWM_on1)
{
if(PWM_t1==PWM_on1)
PWM_OUTDAT1=0;
else if(PWM_t1==Cycle1)
{
PWM_t1=0;
if(PWM_on1!=0)
PWM_OUTDAT1=1;
}
return PWM_OUTDAT1;
}
PWM_OUT2(unsigned int Cycle2,unsigned int PWM_on2)
{
if(PWM_t2==PWM_on2)
PWM_OUTDAT2=0;
else if(PWM_t2==Cycle2)
{
PWM_t2=0;
if(PWM_on2!=0)
PWM_OUTDAT2=1;
}
return PWM_OUTDAT2;
}