51下載的程序,想做成按鍵按下旋轉90度,按鍵松開舵機歸零,如此反復。 修改了但是控制會有問題,大神求解
單片機源程序如下:
#include<reg52.h>
sbit SG_PWM=P3^2;
sbit Key_1=P1^1;
unsigned char count=0;
unsigned char PWM_count=1; //1--0度,2--45度,3--90度,4--135度,5--180度
void delay(unsigned char i) //延時
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void Timer_Init()
{
TMOD=0X01; //T0定時方式1
TH0=0Xfe;
TL0=0X33; //計數初值設置為0.5ms 每0.5ms進入一次中斷,晶振頻率:11.0592MHZ
ET0=1; //打開定時器0的中斷
TR0=1; //打開定時器0
EA=1; //開總中斷
}
void Timer() interrupt 1 //特別注意此處,0--外部中斷0,1--定時器中斷0,2--外部中斷1,3--定時器中斷1,4--串行口中斷1
{
TR0=0;
TH0=0Xfe;
TL0=0X33; //重新賦計數初值為0.5ms
if(count<=PWM_count)
{
SG_PWM=1;
}
else
{
SG_PWM=0;
}
count++;
if(count>=40)
{
count=0;
}
TR0=1;
}
void main()
{
Timer_Init();
while(1)
{
if(Key_1==0)
{
delay(10);
EA=0;
if(Key_1==0)
{
PWM_count=3;
count=0;
EA=1;
}
while(!Key_1);
PWM_count=1;
}
}
}
|