/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... decode signals from a quadrature encoder.
 */

#include <stdio.h>
#include <target.h>

#define POS	((short)Intern_qenval)		// QDEC pos count

/*
 * The QEN(coder) - which is a QDE(coder) in reality - is
 * a position counter, which counts up or down, depending
 * on two signals, usually called A and B from an encoder
 * used in motor control.
 *
 * Connect A, B from the encoder to S1, S2 (=P4.0, P4.1).
 */
int
main(void)
{
	int last = 0x8000;			// last pos

	Intern_qencon  = 1;			// enable QEN
	Intern_gp4con |= 0x11;			//        S1, S2

	/*
	 * Read the position and display changes.
	 */
	while (1) if (POS != last) printf("Pos: %d\n", last = POS);
}
