直流电机PWM闭环控制实验(完整的C51程序)
扫描二维码
随时随地手机看文章
C源代码:
#include
/*定义字型字位口*/
#define DIGPORT P2
#define WORDPORT P0
/*定义键盘口*/
#define KEYPORT P1
#define uint unsigned int
#define uchar unsigned char
#define uong unsigned long
sbit P_7=P3^7; //PWM信号输出
bit OVER;
uint k,dt;
uong KEYNUM;
uchar qu,NUM;
void Delay(uint ms)
{
while(--ms);
}
void timer0(void) interrupt 1 using 1
{
TL0=-(10248%256);
TH0=-(10248%256);
NUM+=1;
if(NUM>=10){
TR1=0;
TR0=0;
OVER=1;
}
}
uint CK(void)
{
uint number=0;
P_7=1;
OVER=0;NUM=0;
EA=1;ET0=1;
TH0=(10203/256);
TH0=(10203%256);
TH1=0;
TL1=0;
TMOD=0x51;
TR0=1;
TR1=1;
Delay(dt);
P_7=0;
while (!OVER);
OVER=0;
number=TH1*256+TL1;
return(number);
}
void Display_LED(uong nu)
{
uint ii=0;
uong aa,bb;
uint xx[]={-0x10,-0x10,-0x10,-0x10,-0x10,-0x10,-0x10,-0x10};
do
{
bb=nu/10;
aa=nu-bb*10;
xx[ii]=aa;
nu=bb;ii++;
}while(nu>0);
DIGPORT=0xf0; WORDPORT=0x30+xx[0];
DIGPORT=0xf1; WORDPORT=0x30+xx[1];
DIGPORT=0xf2; WORDPORT=0x30+xx[2];
DIGPORT=0xf3; WORDPORT=0x30+xx[3];
DIGPORT=0x0f; WORDPORT=0x30+xx[4];
DIGPORT=0x1f; WORDPORT=0x30+xx[5];
DIGPORT=0x2f; WORDPORT=0x30+xx[6];
DIGPORT=0x3f; WORDPORT=0x30+xx[7];
}
void getkey()
{
qu=0;k=0;
KEYNUM=0;
do
{
switch(KEYPORT)
{
case 0xbd:
KEYNUM=KEYNUM*10;
break;
case 0xf6:
KEYNUM=KEYNUM*10+1;
break;
case 0xf5:
KEYNUM=KEYNUM*10+2;
break;
case 0xf3:
KEYNUM=KEYNUM*10+3;
break;
case 0xee:
KEYNUM=KEYNUM*10+4;
break;
case 0xed:
KEYNUM=KEYNUM*10+5;
break;
case 0xeb:
KEYNUM=KEYNUM*10+6;
break;
case 0xde:
KEYNUM=KEYNUM*10+7;
break;
case 0xdd:
KEYNUM=KEYNUM*10+8;
break;
case 0xdb:
KEYNUM=KEYNUM*10+9;
break;
case 0xbe:
KEYNUM=KEYNUM/10;
break;
case 0xbb:
qu=1;
}
Delay(6000);
Delay(6000);
Delay(6000);
Delay(6000);
Delay(6000);
Delay(6000);
Delay(6000);
Delay(6000);
if(KEYNUM>100000000) break;
Display_LED(KEYNUM);
}while(qu==0);
}
void main(void)
{
uong f;
P_7=0;
dt=1000;
getkey();
for(;;){
f=0;
f=CK()*12;
Display_LED(f);
if((f>KEYNUM)&&(dt>100)) dt=dt-(f-KEYNUM)/10;
if((f
}