各位大侠帮小弟看看这个八个舵机控制程序原理呀
本人最近在做八舵机的机器人,在网上找了几个用51单片机写的程序但是还不是很多,希望各位大侠指点一下#include<reg52.h>
#define uint8unsigned char
#define uint16 unsigned int
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit key4=P3^5;
//PWM的输出端口
sbit PWM_OUT0=P1^0;
sbit PWM_OUT1=P1^1;
sbit PWM_OUT2=P1^2;
sbit PWM_OUT3=P1^3;
sbit PWM_OUT4=P1^4;
sbit PWM_OUT5=P1^5;
sbit PWM_OUT6=P1^6;
sbit PWM_OUT7=P1^7;
//PWM的数据值
uint16 PWM_Value={1960,1455,1455,1000,1750,2000,2500,2000};
uint8 order1;//定时器扫描序列
/*===================================================================================
定时器T0的中断服务程序
一个循环20MS= 8*2.5ms
=====================================================================================*/
void timer0(void) interrupt 1 using 1
{
switch(order1)
{
case 1:PWM_OUT0=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 2:PWM_OUT0=0;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
break;
case 3:PWM_OUT1=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 4:PWM_OUT1=0;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
break;
case 5:PWM_OUT2=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 6:PWM_OUT2=0 ;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
break;
case 7:PWM_OUT3=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 8:PWM_OUT3=0;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
break;
case 9:PWM_OUT4=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 10: PWM_OUT4=0;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
break;
case 11: PWM_OUT5=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 12: PWM_OUT5=0;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
break;
case 13: PWM_OUT6=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 14: PWM_OUT6=0;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
break;
case 15: PWM_OUT7=1;
TH0=-PWM_Value/256;
TL0=-PWM_Value%256;
break;
case 16: PWM_OUT7=0;
order1=0;
TH0=-(2700-PWM_Value)/256;
TL0=-(2700-PWM_Value)%256;
order1=0;
break;
default : order1=0;
}
order1++;
}
/*===================================================================================
初始化中断
=====================================================================================*/
void InitPWM(void)
{
order1=1;
TMOD |=0x11;
TH0=-1500/256;
TL0=-1500%256;
EA=1;
EX0=0;
ET0=1; TR0=1;PT0=1;PX0=0;
}
void delay(void)
{
uint16 i=100;
while(i--);
}
void main(void)
{
InitPWM();
while(1)
{
if(key1==0)
{
if(PWM_Value<2200)
PWM_Value+=2;
}
if(key2==0)
{
if(PWM_Value>1000)
PWM_Value-=2;
}
if(key3==0)
{
if(PWM_Value<2100)
PWM_Value+=2;
}
if(key4==0)
{
if(PWM_Value>850)
PWM_Value-=2;
}
delay();
}
}
硬件图呢? 要把原理图发下,才能配合看程序的。 我照这个做了,没问题的
页:
[1]