/*

relay.c:

This program demonstrates how to control the relays built-in to the
PK25xx controller. You can control the frequency of toggling the
relays by adjusting the macros R0PERIOD and R1PERIOD. The half
periods are specified in milliseconds.

Note that the relays are treated as HV output lines in the eioBrdDO
function call. Channel 12 is relay 0, and channel 13 is relay 1.
This way, the application program can change pin
assignments easily without changing function calls.

*/

#define R0PERIOD 400		//	400ms as the HALF period for relay 0
#define R1PERIOD 420		//	420ms as the HALF period for relay 1
#define N_WATCHDOG 0		//	don't need any virtual watchdog
#define _VD_NOHITWD		//	don't need watchdog hit in the background

#use vdriver.lib			//	include vdriver for timing
#use eziopk25.lib			//	include eziopk25 for I/O drivers

main() {
	auto int r0State, r1State;	//	states of the relays
	auto unsigned long int r0Time, r1Time;	//	time marks
	VdInit();				//	initialize timer for timing purposes
	r0Time = r1Time = MS_TIMER;	//	mark the time
	r0State = r1State = 0;
	while (1) {				//	do this forever
		if (MS_TIMER - r0Time >= R0PERIOD) {	//	has half period expired?
			r0State = !r0State;		//	toggle state		
			eioBrdDO(12,r0State);	//	reflect the new state
			r0Time = MS_TIMER;		//	refresh time mark
		}

		if (MS_TIMER - r1Time >= R1PERIOD) {	//	has half period expired?
			r1State = !r1State;		//	toggle state
			eioBrdDO(13,r1State);	//	reflect the new state
			r1Time = MS_TIMER;		//	refresh time mark
		}	

		hitwd();				//	hit the watchdog
	}
}