/* C Listing 11.4 */

#include <hc11.h>	/* Include register definitions */
#include <stdio.h>
/* Declarations */
unsigned int OC2hi, OC2lo;	/* Duty cycle data */
unsigned int T_counts=0x7d0, per_on=50;


/* Prototypes */
void initoc2(void);
void rtoc2(void);
void cnts(void);

void main(void)
{
 	setbaud(BAUD9600);
	INTR_OFF();
	/* set up jump vector for buffalo */
	asm("	ldaa #$7e");
	asm("	staa $dc");
	asm("	ldd #_rtoc2");
	asm("	std $dd");
	OC2hi = 0x3e8;
	OC2lo = 0x3e8;
	initoc2();
	while(1)
	{
	 	 cnts();
		 printf("%x   %x\n",OC2hi,OC2lo); 
	}	  	/* Loop forever */
}

void initoc2(void)
/* Initialize OC2 */
{
	TOC2 = TCNT;
	TCTL1 = 0xC0;
	TFLG1 = 0x40;
	TMSK1 = 0x40;
	INTR_ON();
}

void rtoc2(void)
/* Interrupt Service Routine for OC2 PWM */
{
	if ((TOC2 & 0X40) == 0) 
		TOC2 = TOC2 + OC2lo;
	else
		TOC2 = TOC2 + OC2hi;
	TCTL1 = TCTL1 ^ 0x40;	/* invert OL2 to toggle next OC2 edge */
	TFLG1 = 0x40;	/* Clear flag OC2F */
	asm("	rti");	/* This is an interrupt service routine */
}


void cnts (void)
{
	OC2hi=T_counts * (float)per_on/100;
	OC2lo=T_counts - OC2hi;
}


