4 Channel PWM output with PIC16F690

picI wanted to “pulse” four seperate LEDs on each of the four PIC16F690 PWM output channels. The correct mode to use is the enhanced PWM output – single output mode with pulse steering. Sounds hard, but it’s really not. Here is a link to the source: pic16f690_PWM_ex.c

The source code should be self-explanatory. To set up the correct mode you use:

/* use pulse steering with single output mode */
CCP1CON = 0b00001100;

To work out PR2 and T2CON you use this website: http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html

You set the each of the following registers to 1 to turn on that output port:

STRA (pin5), STRB (pin6), STRC (pin7), STRD (pin14).

And then you just make a loop that varies CCPR1L  from 0 to 0xff and back down to 0 again.

Easy as pie. Have a look at the source code for a more detailed explanation and a function for pulsing the LEDs. Leave a comment if you have any questions.

7 comments ↓

#1 Ryan on 04.15.09 at 9:20 am

Hi
How do you select and output individual pwm channels? i have 3 pots and i want to use them to control the colours the rgb led will output. I am quite stuck. If you could help it would be much appreciated.

Many thanks

#2 psichron on 04.16.09 at 6:41 am


Use registers STRA, STRB, STRC, STRD to turn on/off each of the 4 channels
STRA = pin5
STRB = pin6
STRC = pin7
STRD = pin14

So you can turn on/off the channel output on pin5 by setting STRA = 1 (on) or STRA=0 (off).

#3 Ryan on 04.16.09 at 6:47 am

Hi
I have tried this but it only dims or brightens the led, doesnt actually change the colour? The colour stays red??

Thanks

#4 psichron on 04.16.09 at 6:50 am

Do you have a link to the datasheet of the LED?
Because STRA, STB, etc is how you enable/disable output of the channels, so you have the same signal coming out of all the enabled channel pins.

#5 Ryan on 04.16.09 at 9:12 am

Is it possible to have different signals coming out of the 3 pwm pins? i.e pin5 – 100Hz, pin6 – 200Hz & pin7 – 500Hz or something like this?
Is this how the led colour changes or am i wrong?

Many thanks

#6 Ryan on 04.16.09 at 9:25 am

Unfortunately i dont have the datasheet but its the standard common annode 4 pin rgb led. Basically i want to manually control the colour output from the led with 3 pots.

Many thanks

#7 psichron on 04.16.09 at 12:36 pm

No, not with the 16F690’s PWM generator. You’ll have to use your own timing functions and switch the pins on/off manually.

Leave a Comment