/*
	Illustration of pulse width modulated (PWM) output from output channel 0
	(OUT-00).

	Attach a voltmeter to OUT-00 and observe the increase and decrease of the
	output signal.
*/

#use eziopk25.lib			//	pk2500 specific defns
#define OUT_CHAN 0		//	define output channel
								//	to be OUT-00
main() {
  auto unsigned dutyCycle;
  auto int sign;
  auto long int i;

  eioBrdInit(0);		//	initialize general I/O
  _eioSetupAO1st();	//	initialize PWM
  dutyCycle = 4;		//	duty cycle starts at 4
  sign = 1;				//	ramp up initially
  i = 500;				//	delay count

  while (1) {										//	do this forever
    if (!i) {										//	if delay count == 0?
      eioBrdAO(OUT_CHAN,dutyCycle); 		// change duty cycle
      if (dutyCycle == 255) sign = -1;		// reverse and ends
      else if (dutyCycle == 4) sign = 1;	// reverse
      dutyCycle = dutyCycle + sign; 		// ramp
      i = 500;										//	initialize delay
    } else {
      --i;											//	decrement delay count
    }
    if (_eioBrdAORf()==-1) break; 			// refresh OK? if not break
    hitwd();			//	hit watchdog
  }
  printf("AO refresh failed\n");				//	the only reason to break
  														//	is failure to refresh
}
