Using alternate pins for PWM on Pic16F1825

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Using alternate pins for PWM on Pic16F1825

Post by Creative25 »

Hi.
I just wrote a program for using a Hardware PWM with also a software PWM
I get the strangest things with it.

If I use the software PWM only it works fine.
As soon as the Hardware PWM is enabled the software PWM becomes unstable.
I also have a ADC input on pin Number 7 for measuring voltage. When I pull the voltage down to zero then the Software and the hardware PWM are both stable.
As soon as a voltage enters that pin it does not work.
Also I would like to set the PWM to pin Number 9.
I think there is an error there are to pins named CCP4 ?
How do I configure the output of the Hardware PWM to Pin number 9?

Best Regards:
Uli
Attachments
Pic 16f1825PWM.fcf
(16.26 KiB) Downloaded 288 times

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Kenrix2 »

I am having trouble understanding your post and attached program. However, I can assure you that setting the PWM properties to CCP4 will indeed output a 512us period with a 20% on duty(102us) on pin 9(ccp4) exactly as in simulation. Attached is a program so that you can verify it in hardware.
Attachments
Pic16f1825PWMccp4test.fcf
(6.5 KiB) Downloaded 277 times

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Creative25 »

Hi Kernix
Thanks for your answer.
So I was certain this PWM was working.
The fault was on hardware. One of the vias was not connected, and therefore the Pic16F1825 did not have a proper ground.
So when the input to the ADC was 0 Volt it must have gotten ground through the ADC.
Since I fixed this Via everything works fine.

Hope the cip did not damaged when earth was actually provided by ADC pin.


My program was not complete. What I am trying to do is this. Have one hardware PWM for dimming a light.
Have one software PWM with two outputs for changing color of an LED indicating battery voltage.
So basically the software PWM has one color on at a time. When red is on then green is off. When green is on red is off.
By changing the duty cycle of one color both colors are changing. Everything is working now.

But it would be nice if I could do the color also with a hardware PWM
How can I make a complementary hardware PWM in Flowcode. So that if one pin is off, the other one is on?

Best Regards:
Uli

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Kenrix2 »

Not sure if this is what your looking for but, sometimes any response will give you an idea on how to solve your problem.
Connect the single PWM output to both led's so you can adjust the brightness on each but, one led's polarity is reversed. Then connect the other end of the leds to your current limiting resistor and finally to a port pin. If the port pin output is high the red led will light, if the the port pin output is low the green led will light and your PWM will still control the brightness of both.
Good luck.

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Creative25 »

Hi Kernix
What I actually want is to change color not brightness.
So I alternate between read and green very fast at least 200 Hz and changing the duty cycle to have the color I want.
If Red is 90% then green is 10% If red is 50% green is 50% etc. If red is on green is off if green is on red is off. In this way I can have many colors from green yellow orange to read. I other words I use the PWM to mix color. I have them connected in opposite polarity with a current set resistor. One pin of the pic is on the side where the current resistor is and the other where there is no resistor. So by switching one pin on and the other off I have one color when swapping the polarities around I get the other color. When both pins are off both LED's are off.
I already made A program for it with a software PWM that works quite well. I just think it would be neat to do it with a hardware PWM.

I understand your Idea but then I would have to combine a hardware PWM with some software for the complementary pin.
So that will probable more complex than a pure software PWM

Best Regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Benj »

Hello Uli,

You could do this using a single hardware PWM channel but it would require some additional external components.

e.g.

if you have an output which feeds the red LED. This signals is then also passed via a resistor into the base of a NPN transistor. The output of the transistor is pulled high by an external pullup which also acts to supply power to the green LED. Then one hardware channel controls both but means you cannot actually do any global brightness control or switch both the LEDs off.

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Creative25 »

Hi what I actually was thinking of is Half bridge mode.
I guess this will not work because both pins will have the same duty cycle,
so probably it will be best to stick to the Software Pwm in the Code a posted in the beginning.

I have another question.
How do change the PWM to 10 bit mode?
The data sheet says this is possible, but I do not find this option in flowcode.
Best regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Benj »

Hi Uli,

Thinking about it you wouldn't even need a transistor. You have a LED (LED2) connected with a resistor between VCC and the uC pin. You have the other LED (LED1) connected with a resistor between the uC pin and Ground. When the PWM pin is high LED 1 is lit, when the pin is low LED 2 is lit. The duty would be inversed e.g. if LED 1 is constantly on then LED 2 will be constantly off.

There is a setdutycycle10bit component macro or something similar which allows you to setup the PWM duty using a 10-bit value (0-1023).

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Creative25 »

Hi Benj.
Thanks for the answer.
I understand your Idea this will work for changing colour, but how would you turn OFF both LED's at the same time?

Ok I see the option set duty cycle 10 Bit
But the Periosdregister still can have a maximum count of 255 which according to my knowledge is still eight bits. Is that equal to a rollover value?
How does it actually change to 10 bit if the maximum value on the period register is 255?

Best Regards:
Uli

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using alternate pins for PWM on Pic16F1825

Post by Benj »

Hi Uli,

The period register basically equates to the 8-upper bits of the PWM count. the lower two bits of the period register cannot be altered.

When setting the duty using the 10 bit function you can change all ten bits. When setting the duty using the 8 bit function you can only change the upper 8 bits.

No options of switching off both LEDs unless you supply the VCC from another output pin :wink: but then your back up to 2 pins.

Post Reply