Here’s a demonstration of the PWM module on the PIC16F88. We’ll take the input from a rotary pot connected to the A/D described in this tutorial and use it to set the PWM connected to an LED. Varying the duty cycle on the PWM will rapidly switch the LED on and off and create the effect of changing the brightness. The PWM output is connected between the current limiting resistor and the LED to pull the LED voltage to ground. This is a better configuration than driving the LED from the PIC PWM output because it avoids the PIC acting as a current source.
Here’s the circuit:
The A/D conversion clock, TAD should be a minimum of 1.6 us, so it’s set to 8 * FOSC. The code to initialize the A/D follows:
Completion of the A/D will trigger an interrupt with the
PIR1bits.ADIF bit set.
process_ad will be called in response to the interrupt, which will call
set_pwm to write the A/D conversion result to
CCP1CON, the PWM output connected to the LED.
Here’s the code: