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;
}
}
}
|