Electronic – Variable PWM for a PIC18F4550

cmicrocontrollerpwm

I would like to create a variable duty cycle PWM on a PIC18F4550 microcontroller to control the gate of a MOSFET in a project I am doing.

I am new to microcontrollers but already have code that can generate a PWM signal. I would like help figuring out how to do two things.

  1. How can I change the duty cycle step by step (i.e. 10%,20%,30%….100%), increasing the duty cycle each period of the PWM.
  2. How can I change the duty cycle of the PWM based upon the press of an external switch.

I am programming in the C language and would appreciate some example code.

Best Answer

To step through through various duty cycles, such as incrementing 10% each period, you need to set up an interrupt on the Timer Overflow. In that interrupt services routine, you modify the PWM duty cycle register to it's new value.

To update the duty cycle based on a switch, you do the same thing. Set up an interrupt on the input pin connected to the switch and again update the duty cycle registers during that interrupt service routine.

Here is a Microchip Tutorial on Timer interrupts including some C code. And here is the Microchip Application Note on the Capture/Compare/PWM peripheral. The examples are in Assembly. But you say you've already set up the PWMs so you should already be familiar with which registers you need to modify to change the duty cycle.

Related Topic