/*
	MG2RTAD2.C

	Sample program to read and write the RTC of the BL15xx/MicroG2 and perform
	A/D conversion at the same time. The background is doing AD conversion
	while the hightask (executes every 25ms) polls the RTC.
*/

#define RUNKERNEL 1
#use srtk.lib
#use vdriver.lib

void showtime(struct tm thistime);
void loadtime(struct tm *thistime, int hour, int min, int sec,
					int mon, int mday,int year, int wday);
protected int data0, data1, data2, data3;

struct tm thistime;
struct tm thattime;

int pollrtc() {
		tm_rd(&thistime);
}

#funcchain _srtk_hightask pollrtc

main()
{
	int oldsec;

	
	VdInit();
	init_srtkernel();
	init_kernel();
	
	oldsec = 0;
	tm_rd(&thistime);
	showtime(thistime);
	if(mktime(&thistime)==0L)
	{
		printf("Real Time Clock has not been running.\n");
		printf("Loading new time of 8 a.m. on 4-1-95.\n");
		loadtime(&thistime, 8, 0, 0, 4, 1, 95, 6);
		tm_wr(&thistime);
	}
	
	for(;;)
	{
		hitwd();
		runwatch();
		data0 = mg2adc_read(0);   // read A/D channel 0
		data1 = mg2adc_read(1);   // read A/D channel 1
		data2 = mg2adc_read(2);   // read A/D channel 2
		data3 = mg2adc_read(3);   // read A/D channel 3   
		if(oldsec != thistime.tm_sec)
		{
			showtime(thistime);
			oldsec = thistime.tm_sec;
		printf("\nData for the ADC channels 0-3 !!!\n");
		printf("chan 0 >> %6d\n", data0);
		printf("chan 1 >> %6d\n", data1);
		printf("chan 2 >> %6d\n", data2);
		printf("chan 3 >> %6d\n", data3);
		}
	}
}

void showtime(struct tm thistime)
{
	printf("%02d:%02d:%02d  %02d-%02d-%02d-%1d\n",
				thistime.tm_hour,
				thistime.tm_min,
				thistime.tm_sec,
				thistime.tm_mon,
				thistime.tm_mday,
				thistime.tm_year,
				thistime.tm_wday);
}

void loadtime(struct tm *thistime, int hour, int min, int sec,
					int mon, int mday,int year, int wday)
{
	thistime->tm_hour = hour;
	thistime->tm_min = min;
	thistime->tm_sec = sec;
	thistime->tm_mon = mon;
	thistime->tm_mday = mday;
	thistime->tm_year = year;
	thistime->tm_wday = wday;
}
