Simple PWM Ouptut

​Setup timer 2 or 3 to be the timebase.  The PR value used for the timer will be the total PWM period:

	//----- SETUP TIMER 2 -----
	//Used for: PWM
	OpenTimer2((T2_ON | T2_IDLE_CON | T2_GATE_OFF | T2_PS_1_1 | T2_SOURCE_INT), 5000);

Turn on the PWM output.  The OCxRS value will be the duty cycle of the PWM signal (e.g. if set to 50% of the timers PR value you'll get a 50:50 PWM signal):

	//Used for: Piezo
	OpenOC2((OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE), 2500, 0);		// Config, OCxRS(the PWM duity cycle to use), OCxR

(Note that the OCxR value gets loaded from the OCxRS when the timer next rolls over).

Set a new PWM duty cycle:

	OC2RS = 1250;



We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.