stc12c5a60s2單片機(jī)驅(qū)動舵機(jī)程序
#define uchar unsigned char
#define uint unsigned int //宏定義
#define ulint unsigned long int //宏定義
void delay(uint time);
void Timer_init();
void Timer0(unsignedlongint us);
uint pwm_valu=500;
sbit dj=P1^0;
void main()
{
Timer_init();//定時器初始化
Timer0(31);//通過一個定時值進(jìn)入定時循環(huán)
while(1)
{
pwm_valu=550; //
delay(2000);
pwm_valu=2500; //
delay(2000); //
pwm_valu=1000; //
delay(2000); //
pwm_valu=2000; //
delay(2000); //
pwm_valu=1500; //
delay(2000);
}
}
//軟件延時
void delay(uint time)
{
uint i,j;
for(i=0;i<1000;i++)
for(j=0;j}
//定時器0初始化
void Timer_init()
{
P1M1=0;
P1M0=0XFF;
EA=1; //開總中斷
AUXR|=0xC0;//T0,T1工作在1T
TMOD|= 0x11;//T0工作在方式1,16位
ET0 = 1;//開定時器0中斷
}
//定時器0定時函數(shù)
void Timer0(unsigned long int us) //定時器定時函數(shù)
{
unsignedlongint valu;
valu=us*12;//工作在1T
valu=valu;
valu=0xffff-valu;//
TH0=valu>>8;
TL0=(valu<<8)>>8;
TR0 = 1;//T0開始工作
}
/*************************************
t0 中斷 中斷
*************************************/
void T0zd() interrupt 1
{
static uchar num=1;
switch(num) //
{
case 1:
{
dj=1;
Timer0(pwm_valu);//
} break;
case 2:
{
dj=0;//pwm1變低
Timer0(2500-pwm_valu);//
} break;
case 3:
{
Timer0(2500);//
} break;
case 4:
{
Timer0(2500);//
} break;
case 5:
{
Timer0(2500);//
} break;
case 6:
{
Timer0(2500);//
} break;
case 7:
{
Timer0(2500);//
} break;
case 8:
{
Timer0(2500);//
}break;
case 9:
{
Timer0(2500);//
num=0;
}break;
default:break;
}
num++;
}
本程序完整源碼的下載地址:http://www.51hei.com/bbs/dpj-20521-1.html
評論