/* C Listing 11.4 */

#include <hc11.h>	/* Include register definitions */
#include <stdio.h>

/* Declarations */
unsigned int long OC2hi, OC2lo;	/* Duty cycle data */
unsigned int long T_counts=0x7d0, per_on=75;


/* Prototypes */
void initoc2(void);
void rtoc2(void);
void calc(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");

	initoc2();
//    printf("%lx  %lx\n",OC2hi,OC2lo);

	while(1)
    {
		  calc();
    }		  /* 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 ((PORTA & 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 calc(void)
{	
	OC2hi=(T_counts * per_on)/100;
	OC2lo=T_counts - OC2hi;
}


