本程序中用到的头文件 12c5a.H下载: http://www.51hei.com/f/12c5a.rar
//分时控制舵机,采用的单片机是STC12c5a 51增强型单片机,晶振11.0592MHZ
#include <12c5a.H>
void delay(uint16 time);
void Timer_init();
void Timer0(uint32 us);
uint16 xdata pwm[8]={1500,1500,1500,1500,1500,1500,1500,1500};
sbit pwm1=P0^0;
sbit pwm2=P0^1;
sbit pwm3=P0^2;
sbit pwm4=P0^3;
sbit pwm5=P0^4;
sbit pwm6=P0^5;
sbit pwm7=P0^6;
sbit pwm8=P0^7;
void main()
{
P0M1=0;
P0M0=0XFF;
Timer_init();
Timer0(100);
delay(100);
while(1)
{
;
}
}
void delay(uint16 time)
{
uint16 i;
uint16 j;
for(i=0;i<1000;i++)
for(j=0;j<time;j++);
}
void Timer_init()
{
EA=1; //开总中断
AUXR|=0xC0; //T0,T1工作在1T
TMOD|= 0x11; //T0工作在方式1,16位
ET0 = 1; //开定时器0中断
}
void Timer0(uint32 us)
{
uint32 valu;
valu=us*11;
valu=valu;
valu=0xffff-valu;
TH0=valu>>8;
TL0=valu;
TR0 = 1;
}
void T0zd(void) interrupt 1
{
static uint8 i=1;
switch(i)
{
case 1:
{
pwm1=1;
Timer0(pwm[0]);
} break;
case 2:
{
pwm1=0;
Timer0(2500-pwm[0]);
} break;
case 3:
{
pwm2=1;
Timer0(pwm[1]);
} break;
case 4:
{
pwm2=0;
Timer0(2500-pwm[1]);
} break;
case 5:
{
pwm3=1;
Timer0(pwm[2]);
} break;
case 6:
{
pwm3=0;
Timer0(2500-pwm[2]);
} break;
case 7:
{
pwm4=1;
Timer0(pwm[3]);
} break;
case 8:
{
pwm4=0;
Timer0(2500-pwm[3]);
} break;
case 9:
{
pwm5=1;
Timer0(pwm[4]);
} break;
case 10:
{
pwm5=0;
Timer0(2500-pwm[4]);
} break;
case 11:
{
pwm6=1;
Timer0(pwm[5]);
} break;
case 12:
{
pwm6=0;
Timer0(2500-pwm[5]);
} break;
case 13:
{
pwm7=1;
Timer0(pwm[6]);
} break;
case 14:
{
pwm7=0;
Timer0(2500-pwm[6]);
} break;
case 15:
{
pwm8=1;
Timer0(pwm[7]);
} break;
case 16:
{
pwm8=0;
Timer0(2500-pwm[7]);
i=0;
} break;
default:break;
}
i++;
}