Software PWM - Help Needed.

I have a led dimmer project with 3 switches 1 to increase intensity, 1 to decrease intensity and 1 to cycle through 5 fixed intensities. now based on the switch inputs i generate a pwm using the analogWrite() function. This works fine. Now the challenge is I have to port this project to PIC 12F675 and it does not have a hardware PWM option. so I need to write a software PWM nad i have been trying it out on arduino. Now how do i do this on the arduino? i know i can set it for one time execution by putting it inside an infinite loop. Now how do i create a software pwm that i can use?( just trigger it on and go ahead with other routines till there is some interrupt from the switches to modify the intensity?)



Hi Prakash,

I threw something together real quick, but haven’t had the chance to test it out yet. Test it and let me know how it works.

Connect a voltmeter to pin 13 on the board and ground. 2.5 volts is our target.

#define OUTPIN 13

void setup()
      pinMode (OUTPIN, OUTPUT);

int i = 0;

void loop()
      if(i % 2) //if you don't know what the % operator is: go here
            digitalWrite(OUTPIN, HIGH);
      digitalWrite(OUTPIN, LOW);

So everytime, the arbitrary counter “i” is evenly divisible by 2. I turn on the output, else the output is off.

Let me know how this works out. I understand that my explanation is horrid, but I’m hoping you understand…if not, then I’d be glad to explain.

Here’s some PWM code in C (actually from a PIC12f675-based project, but it should work fine on Arduino given appropriate implementation of getbright(), led_all_on(), and ledN_off…

   while (1) {  // forever
      for (level_delay = 50; level_delay != 0; level_delay--) {
         pwm1 = bright1;
         pwm2 = bright2;
         pwm3 = bright3;
       pwm4 = bright4;
       pwm5 = bright5;
         for (pwm_delay = 128; pwm_delay !=0; pwm_delay--) {
            * We have a random number in each PWM between 1 and 128
            * and we're going to have 128 cycles of delay.  So each
            * decremented output can only cross zero once, at which
            * time we toggle the output state.
             if (--pwm1 == 0) {
             if (--pwm2 == 0) {
             if (--pwm3 == 0) {
             if (--pwm4 == 0) {
             if (--pwm5 == 0) {
         } /* time for new PWM cycle */
      } /* time for new brightness */
   } /* while forever */

Hi west,

Is this the complete code? Can you post the complete code? the gled_all_on() and ledN_off() functions in specific. I’ve done the getbright() part but not sure of how exactly to turn on the leds. say for example, when i say analogWrite(11,200); pin 11 stays at 200 till i modify it

hi fusive,

getting 1.28v off that code…

able to generate maximum of 3.18v by varying the base value for the mod operator(i%15)

now my concern is how to trigger this on, forget about it and then interrupt it again to change it.i think i need some kind of a timer interrupt… not sure how that works