/*

STDBY.C:

This program demonstrates how to set up the RTC to generate
periodic interrupts. In addition, the program also enters
standby mode after each RTC interrupt. The program blinks
the LED at about 1Hz.

If you run this program using F9, Dynamic C will lose
communication as soon as the controller enters standby mode.
However, the LED should keep blinking.

To find out how much power is saved, you can comment out the
line that toggles the LED (or change the line to turn the
LED off, outport(BIT_LED,0)). Then observe the current draw
when the program runs. Contrast this current consumption with
the the current consumption when sysStandby is commented out.

*/

#use eziolp31.lib

unsigned count;

#JUMP_VEC RST38_VEC int0ISR

interrupt reti void int0ISR() {
	auto int flags;
	flags = rtcRdRegC();					//	reading register C clears the periodic
												//	interrupt flag (to release IRQ)
	if (flags & 0x40) {
		++count;								//	to indicate we've been here...
		outport(BIT_LED,(count & 0x80) ? 1 : 0);	//	comment out this line to
																//	measure current consumption
	}
}

main() {
	rtcInit();

	//	clear all interrupt enable
	rtcSwAIE(0);
	rtcSwKSE(0);
	rtcSwPIE(0);
	rtcSwUIE(0);
	rtcSwRIE(0);
	rtcSwWIE(0);

	//	clear all interrupt status flags
	rtcClrIRQ();

	rtcSetPIRate(0x01);			//	set up for 256Hz

	rtcIRQ(1);						//	enable INT0 to receive RTC IRQ

	rtcSwPIE(1);					//	now enable interrupt

	outport(BIT_LED,0);			//	turn off the LED initially
	
	while (1) {
		sysStandby();				//	comment out this line to compare
										//	current consumption
		hitwd();
	}
}