PWM Program for a PIC16F

Discussion in 'Embedded Systems and Microcontrollers' started by Gert, Mar 19, 2009.

  1. Gert

    Thread Starter New Member

    Mar 19, 2009
    6
    0
    Hi

    Can anyone please help me with a program for a PIC that gives PWM output?
    PIC16F***
    It can be any PIC16F I just want to see how to program PWM on a PIC and modify the program to learn from it. I would prefer it if the program is in C but assembly would also do.

    Please

    Gert
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Are you using CCS compiler for C?
     
  3. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
  4. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,698
    904
    Do you want to use the built in PWM module or do it entirely in software? What frequency do you want?

    John
     
  5. slight_return

    Member

    Mar 4, 2009
    12
    1
    If you think about it, a PWM (in logic) can be constructed with a simple binary counter (say 8-bit for example) plugged into an 8-bit comparator. The counter counts off the clock input and increments the output on a rising edge on the clock. (this changes the 8-bit 'Q' output). This feeds into the comparator which compares this value against some other value (say an input from a dip switch). Depending on the value of the dip switch input (see attached) the pulse width will change. Now I know this sounds like a lot of "logic stuff" for a software forum but if you think about it, it should be easy to mimic this in the PIC using some variable which represents your "virtual dipswitch" or reading a value from an input and creating what's in effect a counter, which counts off your clock input, and feeds into a simple loop that compares the "dipswitch variable" to the value of your count then toggles and holds your output value for a particular period.

    In the attached image is an 8-bit cascadable counter and an 8-bit comparator. Both are pretty easy to grasp and if you need help understanding what either does, the information is readily available on the web for both of these logic constructs.

    Hope this helps. If you need code, let me know. Hopefully this will stir the pot enough to get you going and is hopefully generic enough of an explanation to be easy to handle in assembly, C or C++.

    Slight_Return
     
    • PWM.PNG
      PWM.PNG
      File size:
      33.3 KB
      Views:
      111
  6. Gert

    Thread Starter New Member

    Mar 19, 2009
    6
    0
    It would be nice to have a code for the built in PWM module and the one entirely in software but either on is good enough. I will use a 16MHz clock speed. If possible in C please.
     
  7. Gert

    Thread Starter New Member

    Mar 19, 2009
    6
    0
    Can you please paste your code for the schematic if possible in C
     
  8. Gert

    Thread Starter New Member

    Mar 19, 2009
    6
    0
  9. Gert

    Thread Starter New Member

    Mar 19, 2009
    6
    0
    I uses a Boost C compiler but i think that the CCS compiler would also work.
     
  10. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
Loading...