/*

RTCALM.C:

This program demonstrates how to set up the RTC to generate
alarm interrupts. Compile the program and run with F9.
Then add the watch expression "count" to the watch list.
Type control-U repeatedly to see the value change. The
variable count should be incremented once per second.

*/

#use eziolp31.lib

unsigned count;

#JUMP_VEC RST38_VEC int0ISR

interrupt reti void int0ISR() {
	auto flags;
	
	flags = rtcRdRegC();					//	reading register C clears the alarm
												//	flag (to release IRQ)
	if (flags & 0x20) {
		++count;								//	to indicate we've been here...
	}
}

main() {
	auto struct tm t;
	
	rtcInit();

	//	set all alarm fields 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 = 0xc0;
	t.tm_min = 0xc0;
	t.tm_hour = 0xc0;
	t.tm_mday = 0xc0;

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

	//	clear all interrupt status flags
	rtcClrIRQ();

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

	rtcSwAIE(1);					//	now enable interrupt (alarm)

	while (1) {
		runwatch();					//	update the "count" watch expression
		hitwd();
	}
}