ATmega328p Uno Resets if serial monitor or hyperterminal not open!

the strange thing is that my code works fine to control a single servo while the serial monitor or the hyper terminal is open.

When i close the terminal and the uno resets the led(pin 13) will blink to show it has reset(normal), now i get some kind of overflow which resets my code continuously (over a give period of time(20 seconds or so) !!! now the servo will try to go to it’s globally defined position at start-up.

If i open hyper terminal or sketch pads serial monitor, the uno resets as normal, blink the led, and the every is works as it should do

close the serial monitor, every so often the uno will reset!

THIS IS MY TEST CODE, IT ONLY WORKS WHEN SERIAL MONITOR IS OPEN(even though i’m not using it)

/*

  • TIMER1_16_DAVES.c
  • Created: 13/06/2011 12:23:26
  • Author: Dave
    */

#include <AVR/interrupt.h>
#include <avr/io.h>
#define SEI asm(“SEI”)
#define CLI asm(“CLI”)

#define SERVO_STEP 2
#define TIME_PERIOD (19000 * 2) // SET TIME PERIOD TO 20MS (TCNT1 = TIME_PERIOD_SECS(0.02) / TOSC(1/16exp6) /PRESCALER(8))
#define SETUP_SERVO_POS_MIN_US (600 * 2) + (0xFFFF - TIME_PERIOD) // adjust servo too is minimum duty cycle in micro seconds
#define SETUP_SERVO_POS_MAX_US 2400 * 2 + (0xFFFF - TIME_PERIOD)
#define SETUP_SERVO_POS_MID_US 1500 * 2 + (0xFFFF - TIME_PERIOD)
unsigned int A = 0;
unsigned int move_servo ;
char m = 0;

void setup ()
{

DDRD = _BV(PORTD7); //set pin output
TCNT1 = 0xFFFF - TIME_PERIOD; //load timer value
TCCR1A = _BV(COM1A1) | _BV(COM1B1); //setup timer register normal mode for pwm command compare
TCCR1B = _BV(CS11); //register b, pre scale 8
OCR1AH = SETUP_SERVO_POS_MID_US>>8; //output compare register high byte
OCR1AL = SETUP_SERVO_POS_MID_US; //output compare low byte
TIMSK1 = _BV(OCIE1A) + _BV(TOIE1); //turn on interrupt flag Timer overflow compare reg A
CLI; // clear global interrupt
SEI; // enable globale interrupt
move_servo = SETUP_SERVO_POS_MID_US;// servo start postition into

}
void loop ()
{
//int j =13;
//while(1)
// {
//TODO:: Please write your application code
//j++;
// }
}

unsigned int move_servo_test ()
{

if (m == 0)
{
if (move_servo <= SETUP_SERVO_POS_MAX_US)
{
//move_servo += 100;
move_servo = move_servo +SERVO_STEP;

}else
{
move_servo = SETUP_SERVO_POS_MAX_US-SERVO_STEP;
m = 1;

}
} else if ( m == 1)
{
if ((move_servo > SETUP_SERVO_POS_MIN_US) && (move_servo <= SETUP_SERVO_POS_MAX_US) )
{
move_servo = move_servo -SERVO_STEP;

}else
{
move_servo = SETUP_SERVO_POS_MIN_US+SERVO_STEP;
m = 0;

}
}

}

ISR(TIMER1_COMPA_vect)
{

PORTD &= !_BV(PORTD7); // pulse pin 7, turn off port d bit 7

move_servo_test (); // get new servo postion

A =TCNT1;return; // test value of timer
}

ISR (TIMER1_OVF_vect)
{
PORTD = _BV(PORTD7); //turn on bit 7
TCNT1 = 0xffff - TIME_PERIOD + 4; // i think it takes four inturction tick to update timer., need to find another mode as this could effect compar reg
OCR1AH = move_servo>>8; //move the msb of servo to th lsb
OCR1AL = move_servo;
A =TCNT1;return;
}