Ⅰ 急求51单片机控制舵机C语言程序,舵机可以根据设定旋转任意角度!急求啊,大神帮帮忙!非常感谢!
#include<reg52.h>typedef unsigned int uint;typedef unsigned char uchar;sbit pwm=P0^3;//输出PWM信号uchar count=0;uint pwm_value=1500;//初值为1.5msuint value[]={1500,1000,1500,2000,1500};//void delay_ms(uint x){uint i;while(x--)for(i=0;i<125;i++);}void InitTimer(void){TMOD=0x11;//开定时器0,1TH0=-20000/256;//定时20MS,20MS为一个周期TL0=-20000%256;TH1=-1500/256;//定时1.5MS,这时舵机处于0度TL1=-1500%256;EA=1;//开总断TR0=1;//开定时器0ET0=1;TR1=1;//开定时器1ET1=1;}void timer0(void) interrupt 1//定时器0中断函数{pwm=1;TH0=-20000/256;TL0=-20000%256;TR1=1;count++;}void timer1(void) interrupt 3//定时器1中断函数{pwm=0;TH1=-pwm_value/256;TL1=-pwm_value%256;TR1=0;}void main(void)//主函数{uchar i;InitTimer();pwm_value=1500;while(1){for(i=0;i<5;i++){pwm_value=value[i];//通过数组就可以改变舵机的转角度delay_ms(1000);}}}
Ⅱ 求问单片机控制舵机连续来回转动而不停下来的C程序怎么写
因为舵机中有电位器,输出1.5ms宽度的脉冲宽度是正90度,所以你可以写一个for循环,然后引进一个参数,不停地改变0.5ms到2.5ms之间的脉冲宽度就行了。(用延迟函数可以调整速度)希望能帮到你!
Ⅲ 51单片机控制舵机转角的C语言程序
定时器0表示的是脉冲总周期20ms,定时器1表示的是正脉冲宽度(如pwm_value = 1500时表示正脉冲宽度为1.5ms), 而while循序只是为了改变正脉冲宽度。运行过程是:定时器0和定时器,1同时开启,此时pwm = 1,定时器1的时间到了之后pwm = 0,并关闭定时器1,等待脉冲总周期达到20ms即定时器0中断,在定时器0的中断服务中又将pwm赋值为1并开启定时器1……以这样的方式循环就可以控制舵机转动了!
Ⅳ 如何用51单片机控制舵机的单片机程序是怎么写的希望你也能给我发一个编写程序和电路图
单片机系统实现对舵机输出转角的控制,必须首先完成两项任务:首先,产生基本的PWM周期信号,即产生20ms的周期信号;其次,调整脉宽,即单片机调节PWM信号的占空比。单片机能使PWM信号的脉冲宽度实现微秒级的变化,从而提高舵机的转角精度。单片机完成控制算法,再将PWM信号输出到舵机。
发一个自己原来写的简单的。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar count,jd;
sbit pwm=P1^0;
sbit jia=P3^2;
sbit jian=P3^3;
uchar code table[10] = {0x3f,0x06,0x5b,
0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f};
//延时函数
void delay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=125;j>0;j--);
}
//定时器初始化
void Time0_init()
{
TMOD=0x01; //定时器0工作方式1
IE=0x82;
TH0=0xfe;
TL0=0x33; //11.0592MHZ晶振,0.5ms
TR0=1;
}
//定时器0中断程序
void Time0() interrupt 1
{
TH0=0xfe;
TL0=0x33;
if(count<jd) //判断0.5ms次数是否小于角度标识
pwm=1; //是,pwm输出高电平
else
pwm=0; //否,输出低电平
count=count+1;
count=count%40; //次数始终保持为40,即保持周期为20ms
}
//按键扫描
void keyscan()
{
if(jia==0)
{
delay(10);
if(jia==0)
{
jd++; //角度增加 1
count=0; //按键按下则20ms周期重新开始计时
if(jd==6)
jd=5; //已经是180度,保持
while(jia==0);
}
}
if(jian==0)
{
delay(10);
if(jian==0)
{
jd--;
count=0;
if(jd==0)
jd=1; //已经0度,保持
while(jian==0);
}
}
}
//数码管显示
void display()
{
uchar ,shi,ge ;
switch(jd)
{
case 1:
=0;
shi=0;
ge=0;
break;
case 2:
=0;
shi=4;
ge=5;
break;
case 3:
=0;
shi=9;
ge=0;
break;
case 4:
=1;
shi=3;
ge=5;
break;
case 5:
=1;
shi=8;
ge=0;
break;
}
P0=table[];
P2=1;
delay(5);
P0=table[shi];
P2=2;
delay(5);
P0=table[ge];
P2=3;
delay(5);
}
void main()
{
//jd=1;
count=0;
Time0_init();
while(1)
{
keyscan();
display();
}
}
电路图很简单的,几个按键,再在单片机最小系统上接出一个信号线,再给舵机供上电就可以了。
Ⅳ 求51单片机直接控制舵机转动角度的程序
#include<REG52.H>
#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?,(???1382.4,???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?=0.5ms, 45?=1ms, 90?=1.5ms, 135?=2ms, 180?=2.5ms
//2.5 ms??? F700, (12n/11059200=2.5/1000, n=2304, X=65536-2304=63232 > F700)
//return (unit_16)(((float)(2/180)*X+0.5)/1000*11059200/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单片机控制舵机左转90然后延时2秒再右转180,延时两秒回到中间,怎么做
以下程序可供参考
//用一个定时器定时100US产生PWM波
//周期200*100us=20ms
//改变b的值可改变占空比 b=10对应高电平时间1ms
#include<STC12C5A.H>
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P2^0;
uchar a=200,b=15;//b=15对应舵机0度
void Delay1ms(uint i) //1ms延时程序
{
uint j;
for(;i>0;i--)
{
for(j=0;j<125;j++)
{;}
}
}
void main()
{
TMOD=0X01;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
ET0=1;
EA=1;
TR0=1;
P1M0=0X0F; //低4位推挽输出 控制两个电机正反转
P1M1=0X00;
P2M0=0X01; //最低位推挽输出 控制舵机
P2M1=0X00;
while(1)
{
P1=0x0a;
b=15;Delay1ms(2000);//直行
b=10;Delay1ms(1000);//左转
b=15;Delay1ms(2000);//直行
b=20;Delay1ms(1000);//右转
b=15;Delay1ms(2000);//直行
P1=0x05;
b=15;Delay1ms(2000);//后退
b=10;Delay1ms(1000);//后左转
b=15;Delay1ms(2000);//后退
b=20;Delay1ms(1000);//后右转
b=15;Delay1ms(2000);//后退
}
}
void timer0()interrupt 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
a++;
if(a<=b) PWM=1;
else PWM=0;
if(a==200){a=0;PWM=1;}
}