舵机是通过占空比,来控制它的旋转角度,控制信号从单片机输出之后,不能直接接舵机,因为功率不够,中间接一个非门就可以了。控制信号的编程应该来说是很简单的,就是总周期是固定的,角度不同,高电平的持续时间就不同,然后,比如,50的高电平,150的低电平,那么这样的周期要多少个呢,这也是一个问题,太少了,旋转时间不够,太多了,连续的角度变化的话就会不流畅。多尝试一下吧,还有,这种例子也很多,我们那个时候调了一星期才调好,单片机的型号,引脚关系大嘛?只要是通用的io口都可以用啊
#include<REG52H>
#define OUT P0
#define CON P2
#define ms0_5Con 461
#define ms2_5Con 2304
typedef unsigned char unit8;
typedef unsigned short unit_16;
sbit servo0=OUT^0;
sbit servo1=OUT^1;
sbit servo2=OUT^2;
sbit servo3=OUT^3;
sbit servo4=OUT^4;
sbit servo5=OUT^5;
sbit servo6=OUT^6;
sbit servo7=OUT^7;
sbit KEY1=CON^0;
sbit KEY2=CON^1;
unit_16 pwm[8]={1382,1382,1382,1382,1382,1382,1382,1382}; //90,(13824,1382)
void Inter_Form()
{
TMOD|=0x01;
TH0 =0xf7;
TL0 =0x00;
TR0 =1;
ET0 =1;
EA =1;
}
void delay(unit8 x)
{
unit8 i=0;
while(x--)
{
for(i=0;i<125;i++);
}
}
/--------------------------------------------------/
/unit_16 Transform(uchar val)
{
//0=05ms, 45=1ms, 90=15ms, 135=2ms, 180=25ms
//25 ms F700, (12n/11059200=25/1000, n=2304, X=65536-2304=63232 > F700)
//return (unit_16)(((float)(2/180)X+05)/100011059200/12);
unit_16 a = (val+46)10;
if(a<ms0_5Con)
a=ms0_5Con;
if(a>ms2_5Con)
a=ms2_5Con;
return a;
} /
/----------------------------------------------------/
void Steering_UP(unit8 val)
{
if(pwm[val]>ms2_5Con)
pwm[val]=ms2_5Con;
pwm[val]=pwm[val]+10;
}
void Steering_Down(unit8 val)
{
if(pwm[val]<ms0_5Con)
pwm[val]=ms0_5Con;
pwm[val]=pwm[val]-10;
}
void main(void)
{
Inter_Form();
while(1)
{
if(!KEY1)
{
delay(2);
if(!KEY1)
Steering_UP(0);
}
else if(!KEY2)
{
delay(2);
if(!KEY2)
Steering_Down(0);
}
}
}
/--------------------------------------------------/
void SteeringGear() interrupt 1
{
static unit8 pwm_flag=0;
switch(pwm_flag)
{
case 1: servo0=1; TH0=-pwm[0]>>8; TL0=-pwm[0]; break;
case 2: servo0=0; TH0=-(ms2_5Con-pwm[0])>>8; TL0=-(ms2_5Con-pwm[0]); break;
case 3: servo1=1; TH0=-pwm[1]>>8; TL0=-pwm[1]; break;
case 4: servo1=0; TH0=-(ms2_5Con-pwm[1])>>8; TL0=-(ms2_5Con-pwm[1]); break;
case 5: servo2=1; TH0=-pwm[2]>>8; TL0=-pwm[2]; break;
case 6: servo2=0; TH0=-(ms2_5Con-pwm[2])>>8; TL0=-(ms2_5Con-pwm[2]); break;
case 7: servo3=1; TH0=-pwm[3]>>8; TL0=-pwm[3]; break;
case 8: servo3=0; TH0=-(ms2_5Con-pwm[3])>>8; TL0=-(ms2_5Con-pwm[3]); break;
case 9: servo4=1; TH0=-pwm[4]>>8; TL0=-pwm[4]; break;
case 10: servo4=0; TH0=-(ms2_5Con-pwm[4])>>8; TL0=-(ms2_5Con-pwm[4]); break;
case 11: servo5=1; TH0=-pwm[5]>>8; TL0=-pwm[5]; break;
case 12: servo5=0; TH0=-(ms2_5Con-pwm[5])>>8; TL0=-(ms2_5Con-pwm[5]); break;
case 13: servo6=1; TH0=-pwm[6]>>8; TL0=-pwm[6]; break;
case 14: servo6=0; TH0=-(ms2_5Con-pwm[6])>>8; TL0=-(ms2_5Con-pwm[6]); break;
case 15: servo7=1; TH0=-pwm[7]>>8; TL0=-pwm[7]; break;
case 16: servo7=0; TH0=-(ms2_5Con-pwm[7])>>8; TL0=-(ms2_5Con-pwm[7]); break;
default:TH0=0xff; TL0=0x80; pwm_flag=0;
}
pwm_flag++;
}
/---------------------------------------------------------------------------------
如有问题可再咨询
-----------------------------------------------------------------------------------/
以上就是关于51单片机控制舵机(MG995).全部的内容,包括:51单片机控制舵机(MG995).、求51单片机直接控制舵机转动角度的程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)