STC12c5a60s2驱动舵机程序
扫描二维码
随时随地手机看文章
#include //STC12C5A系列单片机
#define uchar unsigned char
#define uint unsigned int //宏定义
#define ulint unsigned long int //宏定义
void delay(uint time);
void Timer_init();
void Timer0(unsigned long int us);
uint pwm_valu=500;
sbit dj=P1^0;
void main()
{
Timer_init(); //定时器初始化
Timer0(31); //通过一个定时值进入定时循环
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定时函数
void Timer0(unsigned long int us) //定时器定时函数
{
unsigned long int valu;
valu=us*12; //工作在1T
valu=valu;
valu=0xffff-valu;
TH0=valu>>8;
TL0=(valu<<8)>>8;
TR0 = 1;//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++;
}