#include 〈reg52
h〉#define Stop 0 //宏定义,停止#define Left 1 //宏定义,左转#define Right 2 //宏定义,右转sbit ControlPort = P2^0; //舵机信号端口sbit KeyLeft = P1^0; //左转按键端口sbit KeyRight = P1^1; //右转按键端口sbit KeyStop = P1^2; //归位按键端口unsigned char TimeOutCounter = 0,LeftOrRight = 0; //TimeOutCounter:定时器溢出计数 LeftOrRight:舵机左右旋转标志void InitialTimer ( void ) { TMOD=0x10; //定时/计数器 1 工作于方式 1 TH1 = ( 65535 — 500 ) / 256; //0
25msTL1 = ( 65535 — 500 ) % 256; EA=1; //开总中断 ET1=1; //允许定时/计数器 1 中断 TR1=1; //启动定时/计数器 1 中断}void ControlLeftOrRight ( void ) //控制舵机函数{if( KeyStop == 0 ){//while (
KeyStop ); //使标志等于 Stop(0),在中断函数中将用到LeftOrRight = Stop;}if( KeyLeft == 0 ){//while (
KeyLeft ); //使标志等于 Left(1),在中断函数中将用到LeftOrRight = Left;}if( KeyRight == 0 ){//while (
KeyRight ); //使标志等于 Right(2),在中断函数中将用到LeftOrRight = Right;}}void ma