/*
	AO1.C

	Illustration of pulse width modulated (PWM) output from output channel 0
	(OUT_01).

	Attach a voltmeter to OUT_01 and observe the increase and decrease of the
	output signal.
*/

#use eziopk23.lib		//	pk2300 specific defns
#define OUT_CHAN 0		//	define output channel
								//	to be HC-01 (pin 21)
main() {
  auto unsigned dutyCycle;
  auto int sign;

  eioBrdInit(0);		//	initialize general I/O
  _eioSetupAO1st();	//	initialize PWM
  dutyCycle = 0;		//	duty cycle starts at 0
  sign = 1;				//	ramp up

  while (1) {			//	do this forever
    eioBrdAO(OUT_CHAN,dutyCycle); // change cycle
    if (_eioBrdAORf()==-1) break; // refresh OK?
    if (dutyCycle == 256) sign = -1;   // reverse
    else if (dutyCycle == 0) sign = 1; // reverse
    dutyCycle = dutyCycle + sign; // ramp
    hitwd();			//	hit watchdog
  }
  printf("AO refresh failed\n");
}
