/*

RTCWAKE.C:

This program demonstrates how to set up the RTC to wake up
the power supply. Compile the program and switch the controller
to run-mode. Monitor Vcc and you should see it is turned on
every 5 seconds.

*/

#use eziolp31.lib

main() {
	auto struct tm t;
	auto char oldSec;
	auto int count;

	eioBrdInit(0);				//	initialize I/O
	
	rtcSwWIE(0);
	if (rtcChkWF()) {
		rtcSwWF(0);
		//	do wake-up specific logic here
	}

	rtcSwKSE(0);
	if (rtcChkKF()) {
		rtcSwKF(0);
		//	do kick start specific logic here
	}
	
	rtcInit();					//	initialize RTC

	eioBrdDO(24,1);			//	turn on LED
	
	count = 5000;
	while (--count) hitwd();
	
	tm_rd(&t);
	oldSec = t.tm_sec;
		
	//	set all alarm fields except second to don't care, you can
	//	set specific values in a field to indicate
	//	alarm "only when the value of RTC matches this
	//	value". This way, you can generate intervals at
	//	a second, a minute, an hour, a day or every
	//	month.

	t.tm_sec = (oldSec + 5) % 60;
	t.tm_min = 0xc0;
	t.tm_hour = 0xc0;
	t.tm_mday = 0xc0;

	rtcSetAlmTime(&t);
	
	lp31Shutdown(0,1);		//	enable wake feature, then power down
	
	//	the previous function never returns!
}