Program to generate pulse width modulation on PA6/OC2 using TOC1 to control OC2







/*
 * Program to generate pulse width modulation
 * on PA6/OC2
 */
#include <hc11.h>

#define DUTY_CYCLE 0x750
#define PERIOD     0x1000

#define TRUE       1 

main()
{
	/* Set up OC1 to bring OC2 high, enable int */
	OC1M = OC1M | 0x40;
	OC1D = OC1D | 0x40;
	TOC1_JMP = JMP_OP_CODE;
	TOC1_VEC = toc1_isr;
	TFLG1 = 0x80;
	TMSK1 = TMSK1 | 0x80;

	/* Set up OC2 to bring OC2 low, no int */
	TCTL1 = (TCTL1 | 0x80) & ~0x40;

	enable();

	while (TRUE) ;
}

#pragma interrupt_handler toc1_isr
void toc1_isr(void)
{
	TOC2 = TOC1 + DUTY_CYCLE;
	TOC1 = TOC1 + PERIOD;
	TFLG1 = 0x80;
}