PWM Program for a PIC16F

Thread Starter

Gert

Joined Mar 19, 2009
6
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
 
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
 

Attachments

Thread Starter

Gert

Joined Mar 19, 2009
6
Do you want to use the built in PWM module or do it entirely in software? What frequency do you want?

John
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.
 

Thread Starter

Gert

Joined Mar 19, 2009
6
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
Can you please paste your code for the schematic if possible in C
 
Top