Anything wrong with the following Code:

The following code from Sparkfun GeoChron. What I change is if the unit not use the external power, then shut off everything but RTC and wake up every 5 minutes. I use global int MIN control the while loop, but the problem is when I insert an external power, the unit is hangle up. The LED light nerver on. What is the problem? I didn’t use interupt routine. Is there a interupt generate when I plug external power?

Code:

if(mode == 2)

{

if (NotUseExternalPower() )

{

stat0_off();

stat1_off();

stat2_off();

CCR = 0;

CCR = 16; //Enable RTC config

HOUR = 0;

MIN = 0;

SEC = 0;

CCR = 17; //Run RTC

while ( NotUseExternalPower() && (MIN < 5 ) )

{

AMR = 254;

ALSEC = SEC + 2;

if (ALSEC >= 60) ALSEC -= 60;

CIIR = 0x00; //disable counter ints

ILR = 3; //Reset anything in here before enabling the int

//IODIR0 &= 0xBFFFFFFF; //turn off the GPS

IOCLR0 = 0x40000000;

PCONP = 0x00000200; //shut down everything but the RTC

VICIntEnable |= 0x00002000;

ILR = 3;

PCON = 0x02; //power down

VICIntEnClr = 0x00002000;//clear the int

}

stat0_on();

stat1_on();

stat2_on();

PCONP = 0x00181FBE; //re-enable the chip peripherals

Initialize(); //start back up

setup_uart1(4800,0);

//VICIntEnClr = 0x00002000;

//IODIR0 |= 0x40000000; //turn on the GPS

IOSET0 = 0x40000000;

}

}