Hi,servo motor using microcontroller,time delay issue..

Status
Not open for further replies.

puremobi

New Member
Hi all
Iam doing a tetrapod robot .
Iam using futuba 3003 servo motor.
All the code is written in C inkeil compiler.
Iam using 89c51 to control a servo motor.
The thing is i when i give an external interrupt,the servo should rotate between minimum and extreme position once.

I have tried a code which does this operation in a continous manner in an erratic timing manner.But not with an interrupt.


when i introduce interrupt,The issue iam facing is the motor makes a very small movement in reverse direction alone.
I will explain the following code,pls correct the issue..

=======================================================
#include "Servo.h"


//global variables.

#ifdef SERVO_0_ENABLE
unsigned char servo_0_posn = SERVO_POSN_MID;
#endif
#ifdef SERVO_1_ENABLE
unsigned char servo_1_posn = SERVO_POSN_MID;
#endif
#ifdef SERVO_2_ENABLE
unsigned char servo_2_posn = SERVO_POSN_MID;
#endif
#ifdef SERVO_3_ENABLE
unsigned char servo_3_posn = SERVO_POSN_MID;
#endif
#ifdef SERVO_4_ENABLE
unsigned char servo_4_posn = SERVO_POSN_MID;
#endif
#ifdef SERVO_5_ENABLE
unsigned char servo_5_posn = SERVO_POSN_MID;
#endif
#ifdef SERVO_6_ENABLE
unsigned char servo_6_posn = SERVO_POSN_MID;
#endif
#ifdef SERVO_7_ENABLE
unsigned char servo_7_posn = SERVO_POSN_MID;
#endif


/*****************************************************************************
*
* Private Function Prototypes
*
*****************************************************************************/
//void timer2_isr(void) interrupt 3 using 2;
unsigned int get_phase_0_time(unsigned char servo_posn);
void set_timer2(unsigned int phase_time);
bit forward=0;
static void input (void);
unsigned int count=0,jm,itim1,i;
static unsigned char servo = SERVO_3;
static bit phase = 0;
void servostart();
void delayer(unsigned int);



/*****************************************************************************
*
* servo_init()
*
*****************************************************************************/
void servo_init(void)
{
SERVO_PORT = 0; // Clear all servo outputs.
TMOD &= 0x0F; // Clear upper nibble of timer control.
TMOD |= 0x10; // Timer 1 mode 1 not gated, internal clock, 16 bit,
ET1=1;
EX0=1;
IT0=1;
EA=1 ; // Enable Global interrupt.
}

//this is the external interrupt which gives the command to make the rotation
static void input (void) interrupt 0
{
forward=1;
}
void main(void)
{

// Initialise the Servo module.
servo_init();

// Cycle servo 3 continuously between extreme positions.
while(1){
if (forward==1)
{
ET1=1;count=0;

servo_3_posn = SERVO_POSN_MIN;
phase=0;
servostart();

phase=1;
servo_3_posn = SERVO_POSN_MAX;
servostart();

forward=0;
}

}
}

void servostart()
{
// Start with 1st servo
// Start with phase 0 of servo
// output cycle
static unsigned int phase_0_time;

if (phase == 0)
{
phase_0_time = get_phase_0_time(servo_3_posn);
SERVO_3_OUT = 1;
set_timer2(CYCLES_FOR_OVERFLOW - phase_0_time);


delayer(1);


}
else
{
SERVO_3_OUT = 0;
set_timer2(CYCLES_FOR_OVERFLOW - (CYCLES_FOR_2MS5
- phase_0_time));
delayer(1);


}
//i dont know wh the following code is used. so i disabled it
//else
// set_timer2(CYCLES_FOR_OVERFLOW - CYCLES_FOR_2MS5);
// servo = SERVO_4;
}
//this is just to calculate the timing for rotation
unsigned int get_phase_0_time(unsigned char servo_posn) using 2
{
return (unsigned int)(CYCLES_FOR_1MS + ((CYCLES_FOR_2MS - CYCLES_FOR_1MS)
/ SERVO_POSN_MAX * servo_posn));
}

/*****************************************************************************
*
* set_timer2()
*
*****************************************************************************/
//and set timer value for each phase
void set_timer2(unsigned int phase_time) using 2
{

TR1 = 0; // Stop Timer 1
TH1 = phase_time >> 8; // Reload Timer 1 high byte.
TL1 = phase_time & 0xFF; // Reload Timer 1 low byte.
TR1 = 1;

// Restart Timer 1
}

//iam using this timer interrupt Timer 1 becoz as soon as timer overflows for any phase it will stop the timer from overflowing again and perhaps stop the servo motor
void timer2_isr(void) interrupt 3 using 2
{
TR1=0;
SERVO_3_OUT = 0;

}






void delayer(unsigned int itim1)
{
for(jm=0;jm<itim1;jm++)
{

for(i=0;i<200;i++)
{

TMOD=00000001;
TL0=0x00;
TH0=0xee;
TR0=1;
while(TF0==0);
TF0=0;
}
}}


// i have attached servo.h file for understanding the syntax..its prety simple.
Pls help me
thank you very much.
 

Attachments

  • Servo.h
    4.8 KB · Views: 312
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…