多舵机控制程序Word格式.docx
《多舵机控制程序Word格式.docx》由会员分享,可在线阅读,更多相关《多舵机控制程序Word格式.docx(8页珍藏版)》请在冰豆网上搜索。
![多舵机控制程序Word格式.docx](https://file1.bdocx.com/fileroot1/2023-1/23/a3d641d0-0460-41dc-86d3-c59f99d7b3ad/a3d641d0-0460-41dc-86d3-c59f99d7b3ad1.gif)
*完成日期:
2009年3月10日
*
*取代版本:
*原作者
:
jinzhikai
2009年3月4日
*
*/
#include<
>
#defineuintunsignedint
#defineucharunsignedchar
sbitledlk=P1^0;
sbitsmglk=P1^1;
sbitsmgbitlk=P1^2;
sbitdj0=P2^0;
/*设置舵机信号输出端口*/
sbitdj1=P2^1;
sbitdj2=P2^2;
sbitdj3=P2^3;
sbitdj4=P2^4;
sbitdj5=P2^5;
sbitdj6=P2^6;
sbitdj7=P2^7;
tha0=(65535-1000)/256;
/*设置tha0代表TH0初值,0代表第一号舵机,a代表高电平,b代表低电平*/
tla0=(65535-1000)%256;
thb0=(65535-(20000-1000))/256;
tlb0=(65535-(20000-1000))%256;
tha1=(65535-1100)/256;
tla1=(65535-1100)%256;
thb1=(65535-(20000-1100))/256;
tlb1=(65535-(20000-1100))%256;
tha2=(65535-1200)/256;
tla2=(65535-1200)%256;
thb2=(65535-(20000-1200))/256;
tlb2=(65535-(20000-1200))%256;
tha3=(65535-1300)/256;
tla3=(65535-1300)%256;
thb3=(65535-(20000-1300))/256;
tlb3=(65535-(20000-1300))%256;
tha4=(65535-1400)/256;
tla4=(65535-1400)%256;
thb4=(65535-(20000-1400))/256;
tlb4=(65535-(20000-1400))%256;
tha5=(65535-1500)/256;
tla5=(65535-1500)%256;
thb5=(65535-(20000-1500))/256;
tlb5=(65535-(20000-1500))%256;
tha6=(65535-1600)/256;
tla6=(65535-1600)%256;
thb6=(65535-(20000-1600))/256;
tlb6=(65535-(20000-1600))%256;
tha7=(65535-1700)/256;
tla7=(65535-1700)%256;
thb7=(65535-(20000-1700))/256;
tlb7=(65535-(20000-1700))%256;
uchardj_set(uchardj_num,uchardp,uchari)
/*设置dj_set函数,确定选择计数器初值,dj_num表示舵机号,dp表示电平,i为1等于TH0初值,i为0等于TL0初值*/
{
if(dp)
if(i)
switch(dj_num)
{
case'
0'
:
return(tha0);
break;
1'
return(tha1);
case'
2'
return(tha2);
3'
return(tha3);
4'
return(tha4);
5'
return(tha5);
6'
return(tha6);
7'
return(tha7);
}
else
return(tla0);
return(tla1);
return(tla2);
return(tla3);
return(tla4);
return(tla5);
return(tla6);
return(tla7);
else
return(thb0);
return(thb1);
return(thb2);
return(thb3);
return(thb4);
return(thb5);
return(thb6);
return(thb7);
return(tlb0);
return(tlb1);
return(tlb2);
return(tlb3);
return(tlb4);
return(tlb5);
return(tlb6);
return(tlb7);
}
voidmain()
uchardj_num=0;
ucharhl=1;
ledlk=0;
smglk=0;
smgbitlk=0;
ET0=1;
ET1=1;
EA=1;
TMOD=0x01;
/*选择0号计时器,16位计时器*/
TR0=1;
TR1=1;
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
while
(1)
for(dj_num=0;
dj_num<
8;
)
while(TF0)
/*发生中断循环*/
if(!
hl)
/*判断低电平中断,换舵机端口*/
dj_num++;
switch(dj_num)
/*按照舵机号,相应端口置反*/
dj0=!
dj0;
dj1=!
dj1;
dj2=!
dj2;
dj3=!
dj3;
dj4=!
dj4;
dj5=!
dj5;
dj6=!
dj6;
dj7=!
dj7;
hl=!
hl;
TH0=dj_set(dj_num,hl,1);
/*调用dj_set()函数设置计时器初值*/
TL0=dj_set(dj_num,hl,0);
TF0=0;
/*清除中断*/
编写了一个多舵机控制程序,不知道能否应用,有示波器的朋友帮忙测试一下。
感谢!