/*
	TSTAIN.C

	Test the analog inputs.

	This example times the _eioBrdAI() function, then reads the
	number of counts and displays them on the stdio screen twenty
	times.
*/

#use eziopk24.lib

void setupPRT0() {
	auto char tmp;

	tmp = inport(TCR);
	tmp &= ~(0x11);
	outport(TCR, tmp);
	outport(TMDR0L,0);
	outport(TMDR0H,0);
}

void startPRT0() {
	outport(TCR, inport(TCR) | 0x1);
}

unsigned int stopPRT0() {
	auto int res;

	res = inport(TMDR0L);
	res += inport(TMDR0H)*256;
	return (~res) + 1;
}

float tickToUs(unsigned int ticks) {
	return (float)ticks*20 / sysclock() / 1.2e-3;
}

float tickToMs(unsigned int ticks) {
	return (float)ticks*20 / sysclock() / 1.2;
}

main() {
	unsigned int u1, u2;
	float tmpF;
	
	eioBrdInit(0);
	setupPRT0();
	startPRT0();
	u1 = stopPRT0();
	setupPRT0();
	startPRT0();
	_eioBrdAI(0);
	u2 = stopPRT0();
	printf("%u ticks\n",u2 - u1);
	printf("%6.2f us\n",tickToUs(u2-u1));
	for (u1 = 20; --u1;) {
		printf("%u\n",_eioBrdAI(3));
		hitwd();
	}
}