![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
PIC18F46K20 PWM not working |
Posted: Sat Jan 24, 2015 8:57 am |
|
|
Hi everyone!
I'm building a speed regulator for a fan on my desk, with a pic and a display.
For the speed, I want to use PWM over a transistor.
So, I built this circuit (ignore the display and all the switch...), and (after testing the display, that works) I tried to figure out how I can do the PWM... I never done that...
5 minutes on google, and I wrote this:
Code: | #include <18F46K20.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
main()
{
output_low(PIN_C2); // Set CCP1 output low
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 124, 1); // 500 Hz
set_pwm2_duty(62); // 50% duty cycle on pin C1
while(1); // Prevent PIC from going to sleep
} |
and the led is not powered...
WHY?
help pls |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19370
|
|
Posted: Sat Jan 24, 2015 9:11 am |
|
|
You seem to have some pin confusion.
You are half setting up CCP1, then trying to use CCP2....
You haven't setup CCP2 as a PWM.
Also CCP2, needs to be told which pin it is to use (CCP2C1 fuse).
You need to decide which CCP you are using, and set the right one up. |
|
![](templates/subSilver/images/spacer.gif) |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
|
Posted: Sat Jan 24, 2015 9:27 am |
|
|
Ttelmah wrote: | You seem to have some pin confusion.
You are half setting up CCP1, then trying to use CCP2....
You haven't setup CCP2 as a PWM.
Also CCP2, needs to be told which pin it is to use (CCP2C1 fuse).
You need to decide which CCP you are using, and set the right one up. |
so, now I've got this:
Code: | #include <18F46K20.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, CCP2C1,
#use delay(clock = 4000000)
main()
{
output_low(PIN_C1);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 124, 1);
set_pwm2_duty(62);
while(1);
} |
and the led still not lighting up... |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19370
|
|
Posted: Sat Jan 24, 2015 9:34 am |
|
|
So, have you first verified the chip actually works?.
Flashed an LED just using output_high/output_low?.
MCLR pulled high?.
What current limiting resistor have you got on the LED?. |
|
![](templates/subSilver/images/spacer.gif) |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
|
Posted: Sat Jan 24, 2015 9:37 am |
|
|
Ttelmah wrote: | So, have you first verified the chip actually works?.
Flashed an LED just using output_high/output_low?.
MCLR pulled high?.
What current limiting resistor have you got on the LED?. |
the lcd works, and if turn on the led with output_high it works.
mclr is pulled up on a 10k resistor, and I removed the resistor on the led. |
|
![](templates/subSilver/images/spacer.gif) |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
|
Posted: Sat Jan 24, 2015 10:01 am |
|
|
Code: | #include <18F46K20.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP,
#use delay(clock = 4000000)
main()
{
setup_oscillator(OSC_8MHZ);
output_c(0);
setup_timer_2(T2_DIV_BY_16, 124, 1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(32);
while(1);
} |
THAT WORKS!
now, I have a rotary encoder on the board, for changing the duty... How can I use it?
And, what's the number in "set_pwm1_duty()" for 100% duty? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19370
|
|
Posted: Sat Jan 24, 2015 10:19 am |
|
|
The reason it is failing, is you are overloading the PIC.
No current limiting resistor = PIC port shorted to LED forward voltage max. Result overload. As you turn up the duty, the overload gets bad enough to stop the PIC running.
I wondered if this was what was happening.....
You need to limit the current to the LED to no more than about 20mA max. So if (for instance) you have a LED with a quoted Vf of 2.2v, and are driving from a PIC running off 3.3v, so the pin will want to go up to about 2.6v, you need a resistor of about 20R. The figure needed will depend on the Vf of your LED, and the supply you are using for your chip.
The number for set_pwm_duty, depends whether you feed it with an int16, or an int8. Currently int8, so PR2+1 (125) = 100%. If you feed it with an int16, multiply this by 4, and subtract 1. |
|
![](templates/subSilver/images/spacer.gif) |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
|
Posted: Sat Jan 24, 2015 10:59 am |
|
|
Ttelmah wrote: | The reason it is failing, is you are overloading the PIC.
No current limiting resistor = PIC port shorted to LED forward voltage max. Result overload. As you turn up the duty, the overload gets bad enough to stop the PIC running.
I wondered if this was what was happening.....
. |
if I set the duty to 125 it works.
I just added "output_c(0); " and it works. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19370
|
|
Posted: Sat Jan 24, 2015 1:04 pm |
|
|
However you are almost certainly overloading the PIC. You need to actually work out what current is driven from the pin in this state. 25mA is the max rating of any pin and there is also a maximum total rating on the chip.
One of the key things in design is to always keep inside _all_ ratings for the chip. |
|
![](templates/subSilver/images/spacer.gif) |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
|
Posted: Sun Jan 25, 2015 7:16 am |
|
|
on the pwm output I've connected a transistor, and removed the led.
The pwm signal enables the transistor on a 5v line, so I get a 5v pwm signal on the output.
on the program, if I set C2 (pwm) high I should have constant 5v on the output, and that work, but if I hook up an usb fan (5v 500mA, and the power supply is 5v 2A) the voltage drops at 2v and the fan doesn't spin.
why? the transistor is a bc327 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19370
|
|
Posted: Sun Jan 25, 2015 8:31 am |
|
|
Post a schematic of what you are doing. Either put a picture up on a site (free or otherwise), and post a link to this, or do an ASCII text schematic. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Sun Jan 25, 2015 8:53 am |
|
|
Without seeing your schematic this is what I think.....
Had a quick look at the transistor datasheet,,
First it's a PNP device, so a 'high' from the PIC will turn it off. the inverse of what I think you really want..and how the PWM is normally thought of..
2nd, at 500ma Ic, hfe is about 40, so you need +-12ma of current from the PIC to drive the fan (500ma). Might be possible, though I doubt it..... PIC will 'overload' try to 'selfprotect'( ie...fail...)
Normally these days you use a 'logic level FET' to drive fans. PIC I/O pin to gate, FET source to ground, drain tied to fan -ve, fan+ to +5V.
You should 'breadboard' the 'fan drive circuit' without a PIC , just use 0 and +5 as the 'control' input. Measure currents/voltages, etc. to see how it works.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
|
![](templates/subSilver/images/spacer.gif) |
Ellest96
Joined: 15 Nov 2013 Posts: 17
|
|
Posted: Sun Jan 25, 2015 9:58 am |
|
|
temtronic wrote: |
Normally these days you use a 'logic level FET' to drive fans. PIC I/O pin to gate, FET source to ground, drain tied to fan -ve, fan+ to +5V.
|
I know, but I don't have one of them, can I use a transistor? |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Sun Jan 25, 2015 10:47 am |
|
|
Yes BUT you MUST use a resistor to connect PIC to base of transistor !!
I'd suggest a 470r or 1K to start.
Typically you use an NPN transistor, with emitter connected to ground, 1K resistor from base to PIC I/O pin, collector to fan -ve lead, fan +ve lead to +5 volts.
This is all basic Electronics101 , tons of info on the web, be sure to get/read the datasheet for the NPN transistor you've chosen.
NOTE!!! I had a look at your schematic.
NOTE!!!! The 2N706 will NOT work for you!!! It is only rated for 50ma of collector current. Your fan needs 500 ma. You'll need to choose another transistor. It should be 'ok' for the backlight LED drive but NEEDS a base resistor(say 470r ).
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|