当前位置:文档之家› 8路舵机控制

8路舵机控制

8路舵机控制器程序(2010-04-25 13:02:21)转载标签: 杂谈
8路舵机控制器程序

*=================================================================================
8路舵机控制器

芯片:AT89S52
晶振:12MHz
===================================================================================*/
#i nclude
#define uint8 unsigned char
#define uint16 unsigned int


sbit key1=P1^4;
sbit key2=P1^5;

//PWM的输出端口
sbit PWM_OUT0=P0^0;
sbit PWM_OUT1=P0^1;
sbit PWM_OUT2=P0^2;
sbit PWM_OUT3=P0^3;
sbit PWM_OUT4=P0^4;
sbit PWM_OUT5=P0^5;
sbit PWM_OUT6=P0^6;
sbit PWM_OUT7=P0^7;


//PWM的数据值
uint16 PWM_Value[8]={1500,1000,1500,1000,1750,2000,2500,2000};

uint8 order1; //定时器扫描序列

void timer0(void) interrupt 1 using 1
{
switch(order1)
{
case 1: PWM_OUT0=1;
TH0=-PWM_Value[0]/256;
TL0=-PWM_Value[0]%256;
break;
case 2: PWM_OUT0=0;
TH0=-(2700-PWM_Value[0])/256;
TL0=-(2700-PWM_Value[0])%256;
break;
case 3: PWM_OUT1=1;
TH0=-PWM_Value[1]/256;
TL0=-PWM_Value[1]%256;
break;
case 4: PWM_OUT1=0;
TH0=-(2700-PWM_Value[1])/256;
TL0=-(2700-PWM_Value[1])%256;
break;
case 5: PWM_OUT2=1;
TH0=-PWM_Value[2]/256;
TL0=-PWM_Value[2]%256;
break;
case 6: PWM_OUT2=0 ;
TH0=-(2700-PWM_Value[2])/256;
TL0=-(2700-PWM_Value[2])%256;
break;
case 7: PWM_OUT3=1;
TH0=-PWM_Value[3]/256;
TL0=-PWM_Value[3]%256;
break;
case 8: PWM_OUT3=0;
TH0=-(2700-PWM_Value[3])/256;
TL0=-(2700-PWM_Value[3])%256;
break;
case 9: PWM_OUT4=1;
TH0=-PWM_Value[4]/256;
TL0=-PWM_Value[4]%256;
break;
case 10: PWM_OUT4=0;
TH0=-(2700-PWM_Value[4])/256;
TL0=-(2700-PWM_Value[4])%256;
break;
case 11: PWM_OUT5=1;
TH0=-PWM_Value[5]/256;
TL0=-PWM_Value[5]%256;
break;
case 12: PWM_OUT5=0;
TH0=-(2700-PWM_Value[5])/256;
TL0=-(2700-PWM_Value[5])%256;
break;
case 13: PWM_OUT6=1;
TH0=-PWM_Value[6]/256;
TL0=-PWM_Value[6]%256;
break;
case 14: PWM_OUT6=0;
TH0=-(2700-PWM_Value[6])/256;
TL0=-(2700-PWM_Value[6])%256;
break;
case 15: PWM_OUT7=1;
TH0=-PWM_Value[7]/256;
TL0=-PWM_Value[7]%256;
break;
case 16: PWM_OUT7=0;
order1=0;
TH0=-(2700-PWM_Value[7])/256;
TL0=-(2700-PWM_Value[7])%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[0]<2500)
PWM_Value[0]++;
}

if(key2==0)
{
if(PWM_Value[0]>500)
PWM_Value[0]--;
}

delay();
}
}


相关主题
文本预览
相关文档 最新文档