AT89S52定时器2控制舵机,但是总是调不好,哪位帮忙看下程序哪里有问题?
#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharsbitsteering=P2^4;void...
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit steering=P2^4;
void steering_engine_initial() //initialize Timer2
{
T2CON = 0x00;
TCLK = 0;
RCLK = 0;
CP_RL2 = 0;
steering = 1;
ET2 = 1;
}
void steering_engine_up()
{
steering_engine_initial();
TH2 = (65536-2000)/256; //initialize register T2
TL2 = (65536-2000)%256;
while(1)
{
/*---High Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-18000)/256; //set low level previously
RCAP2L = (65535-18000)%256;
while(TR2==1);
/*---High Level:END---*/
/*---Low Level:BEGIN---*/
RCAP2H = (65536-2000)/256; //set high level previously
RCAP2L = (65536-2000)%256;
TR2 = 1;
while(TR2==1);
/*---Low Level:END---*/
}
}
void steering_engine_down() //1000 19000
{
steering_engine_initial();
TH2 = (65536-1000)/256; //initialize register T2
TL2 = (65536-1000)%256;
while(1)
{
/*---High Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-19000)/256; //set low level previously
RCAP2L = (65536-19000)%256;
while(TR2==1);
/*---High Level:END---*/
/*---Low Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-1000)/256; //set high level previously
RCAP2L = (65536-1000)%256;
while(TR2==1);
/*---Low Level:END---*/
}
}
void steering_engine_stop()
{
steering_engine_initial();
TH2 = (65536-1500)/256;
TL2 = (65536-1500)%256;
while(1)
{
/*---High Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-18500)/256; //set low level previously
RCAP2L = (65536-18500)%256;
while(TR2==1);
/*---High Level:END---*/
/*---Low Level:BEGIN---*/
RCAP2H = (65536-1500)/256; //set high level previously
RCAP2L = (65536-1500)%256;
TR2 = 1;
while(TR2==1);
/*---Low Level:END---*/
}
}
void steering_engine_intr() interrupt 5
{
TF2 = 0 ; //问题在这里,加这一句舵机转动角度不够,不加转动角度又太大
TR2 = 0;
steering=~steering;
} 展开
#define uint unsigned int
#define uchar unsigned char
sbit steering=P2^4;
void steering_engine_initial() //initialize Timer2
{
T2CON = 0x00;
TCLK = 0;
RCLK = 0;
CP_RL2 = 0;
steering = 1;
ET2 = 1;
}
void steering_engine_up()
{
steering_engine_initial();
TH2 = (65536-2000)/256; //initialize register T2
TL2 = (65536-2000)%256;
while(1)
{
/*---High Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-18000)/256; //set low level previously
RCAP2L = (65535-18000)%256;
while(TR2==1);
/*---High Level:END---*/
/*---Low Level:BEGIN---*/
RCAP2H = (65536-2000)/256; //set high level previously
RCAP2L = (65536-2000)%256;
TR2 = 1;
while(TR2==1);
/*---Low Level:END---*/
}
}
void steering_engine_down() //1000 19000
{
steering_engine_initial();
TH2 = (65536-1000)/256; //initialize register T2
TL2 = (65536-1000)%256;
while(1)
{
/*---High Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-19000)/256; //set low level previously
RCAP2L = (65536-19000)%256;
while(TR2==1);
/*---High Level:END---*/
/*---Low Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-1000)/256; //set high level previously
RCAP2L = (65536-1000)%256;
while(TR2==1);
/*---Low Level:END---*/
}
}
void steering_engine_stop()
{
steering_engine_initial();
TH2 = (65536-1500)/256;
TL2 = (65536-1500)%256;
while(1)
{
/*---High Level:BEGIN---*/
TR2 = 1;
RCAP2H = (65536-18500)/256; //set low level previously
RCAP2L = (65536-18500)%256;
while(TR2==1);
/*---High Level:END---*/
/*---Low Level:BEGIN---*/
RCAP2H = (65536-1500)/256; //set high level previously
RCAP2L = (65536-1500)%256;
TR2 = 1;
while(TR2==1);
/*---Low Level:END---*/
}
}
void steering_engine_intr() interrupt 5
{
TF2 = 0 ; //问题在这里,加这一句舵机转动角度不够,不加转动角度又太大
TR2 = 0;
steering=~steering;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询