PIC16F628A serial controlled PWM generator

Thread Starter

Markd77

Joined Sep 7, 2009
2,806
Thought I'd share this in case it is useful to someone. It should be able to generate PWM from 244 Hz to 1MHz with up to 10 bit resolution on the PWM.
Just program the PIC, wire it up to a MAX232 or similar and connect to your serial port. Output is on pin 9 and MCLR needs holding high with a resistor.
Check the PIC16F628A datasheet for formulas to calculate the actual frequency and duty ratio.
If you just press enter, it keeps the last value and goes to the next option.

 

Attachments

Last edited:

Thread Starter

Markd77

Joined Sep 7, 2009
2,806
Not the best frequency counter in the world, but OK for free (assuming you've got a PIC programmer, a MAX232 or ST232, and a PIC16F628A.)
Measured duty cycle is 0-255, rather than % because that is more useful to me. Frequency is in Hz (just a count for 1/4 second so only 4Hz accuracy which also means low frequencies can't be measured accurately or at all).
The duty cycle is calculated by sampling the input 16384 times during the quarter second so won't be very accurate especially at some frequencies.
Realterm works fine for this, I haven't tested anything else.
Pins RB6 and RB5 are connected together and used as the input.
It seems to work fine up to about 100000Hz but seems to have problems above that (or it might work better if it wasn't on a breadboard).

<ed> I've soldered it up on stripboard and now (unsurprisingly) it works a bit better. Frequency works fine with a 50% duty cycle up to 1MHz (I haven't got anything faster handy to test). In theory it should go up to 16MHz. Duty cycle measurements are not good at high frequencies but seem to be good below 100kHz. </ed>

In summary, if you haven't got a frequency counter, this is better than nothing.

Everything else works as before.


 

Attachments

Last edited:
Top