Ⅰ 急求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;}
}