舵機速度控制的51單片機程序
#include <12c5a.H> //STC12C5A系列單片機
#include"intrins.h"
void delay(uint16 time); //軟件延時函數(shù)
void Timer_init(); //定時器初始化函數(shù)
void Timer0(uint32 us); //定時器0定時函數(shù)
void qhuan(unsigned int ms50,char zushu);
char n,a=101;
int LK[8]={0},*K;
int shuju[8][8]=
{ //舵機動作數(shù)據(jù)
{2500,2500,2500,2500,2500,2500,2500,2500},
{500,500,500,500,500,500,500,500},
{2500,2500,2500,2500,2500,2500,2500,2500},
{500,500,500,500,500,500,500,500},
{2500,2500,2500,2500,2500,2500,2500,2500},
{500,500,500,500,500,500,500,500},
{2500,2500,2500,2500,2500,2500,2500,2500},
{500,500,500,500,500,500,500,500},
};
int *PWMM,pwm[8]; //PWMM用以尋址動作數(shù)據(jù)的指針,LP接收信息時用以指向數(shù)據(jù)緩存
unsigned int thesea;
sbit pwm8=P0^5;
sbit pwm7=P0^6;
sbit pwm6=P0^7;
sbit pwm5=P4^6;
sbit pwm4=P4^1;
sbit pwm3=P4^5;
sbit pwm2=P4^4;
sbit pwm1=P2^7;
void main(void)
{
uint8 i=0;
P0M1=0; //設置P口為強推免輸出模式,下同
P0M0=0XFF;
P1M1=0;
P1M0=0XFF;
P2M1=0;
P2M0=0XFF;
P3M1=0;
P3M0=0XFF;
Timer_init(); //定時器初始化
Timer0(31); //通過一個定時值進入定時循環(huán)
K=LK;
PWMM=shuju[0];
delay(100);
while(1)
{
;
}
}
void delay(uint16 time)
{
uint16 i;
uint16 j;
for(i=0;i<1000;i++)
for(j=0;j}
void Timer_init()
{
EA=1; //開總中斷
AUXR|=0xC0; //T0,T1工作在1T
TMOD|= 0x11; //T0工作在方式1,16位
ET0 = 1; //開定時器0中斷
ET1 = 1;
TR0=1;
TR1=0;
}
void Timer0(uint32 us)
{
uint32 valu;
valu=us*11; //工作在1T,最大定時時間0xffff/11us
valu=valu;
valu=0xffff-valu; //
TH0=valu>>8;
TL0=valu;
}
評論