CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC12F675 heating (mosfet, timer/PWM involved)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FJMSoft



Joined: 20 Oct 2013
Posts: 36

View user's profile Send private message

PIC12F675 heating (mosfet, timer/PWM involved)
PostPosted: Mon Dec 02, 2013 10:55 pm     Reply with quote

Hello there.

This is just my second project using CCS also PIC, first one was just a simple adjustable strobe light.
This project uses timer to generate 3 channels PWM.
The PWM will adjust the speed of 3 motors (small 5v fan) randomly.
Also lights on and off a laser accordingly.
The device is a simple laser show device (like party ones).

Normal expected behavior:
Turn on laser, do a random set of random motors speeds.
-Around 30sec-
Turn off laser, turn off motors.
-Around 45sec (15+30)-
Start again.

The problem:
My device is getting very crazy, it is performing very weird sometimes, probably crashing/halting (laser don't turn on, laser don't turn off, motors always at constant speed, don't do the breaks).
It starts performing weird sometimes soon as powered, sometimes a minute later, sometimes just work well.
Sometimes the uC gets very hot, I almost burned my finger on it!
I also discovered recently, if I touch pin 4 (GP3) uC gets crazy, generally starts heating too.

I already exchanged the uC and the supply.
I'm aware about the delay times being not precise because of the interruption.

I cant make it work normally so I'm asking help here.

uC is PIC12F675
Compiler is version 5.008

I have tried my best here, please don't be so critic, I'm not an expert.
English is not my first language also.
Sorry for any mistake.

Follows circuit and code.
(I forgot to put gnd in the uC in the circuit but of course it is connected)


Code:

#include <main.h>
#USE FAST_IO (A)

int8 Motor1V;
int8 Motor2V;
int8 Motor3V;
int8 PWMC; //PWM Counter
int8 OSB;
int8 LASERMODE=0;
int8 I;
int16 IL;
char random_byte;


char rand(void)   {
   char sum;
   
   sum = 0;
   
   // This calculates parity on the selected bits (mask = 0xb4).
   if(random_byte & 0x80)
      sum = 1;
   
   if(random_byte & 0x20)
      sum ^= 1;
   
   if(random_byte & 0x10)
      sum ^= 1;
   
   if(random_byte & 0x04)
      sum ^= 1;
   
   random_byte <<= 1;
   
   random_byte |= sum;
   
   return(random_byte);
}

void srand(char seed) {
   random_byte = seed;
}


#INT_EXT
void  EXT_isr(void)
{}

#INT_RTCC
void  RTCC_isr(void)
{
   If (Motor1V==PWMC) output_low(PIN_A4);
   If (Motor2V==PWMC) output_low(PIN_A5);
   If (Motor3V==PWMC) output_low(PIN_A0);
   
   PWMC++;
   If (PWMC==64){
     PWMC=0;
     
     If (Motor1V>0) output_high(PIN_A4);
     If (Motor2V>0) output_high(PIN_A5);
     If (Motor3V>0) output_high(PIN_A0);
   }
   
   set_timer0(200);
 
}

void main()
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_bit);      //256 us overflow

   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   
   set_tris_a(0b00000000);
   
   //Read last eeprom value and use it to generate a new seed
   srand(read_eeprom(0));
   write_eeprom(0,rand());
   
   while(TRUE)
   {
     
     switch (LASERMODE) {
       case 1://2 motor random ====================
              for (I=0; I<10; I++) {
                Motor1V=rand()/4;
                Motor2V=rand()/4;
                Motor3V=0;
                delay_ms(1500);
              }
              LASERMODE=255;
              break;
       
       case 2://3 motor random ====================
              for (I=0; I<10; I++) {
                Motor1V=rand()/4;
                Motor2V=rand()/4;
                Motor3V=rand()/4;
                delay_ms(1500);
              }
              LASERMODE=255;
              break;

       case 3://2,5 motor random ====================
              Motor3V=63;
              delay_ms(20);
              Motor3V=5;
             
              for (I=0; I<10; I++) {
                Motor1V=rand()/4;
                Motor2V=rand()/4;
                delay_ms(1500);
              }
              LASERMODE=255;
              break;
       
       case 4://2 motor fade ======================
              Motor1V=rand()/4;
              Motor2V=rand()/4;
              Motor3V=0;
              for (I=0; I<20; I++) {
                Motor1V+=rand()/64;
                If (Motor1V>63) Motor1V=5;
                Motor2V+=rand()/64;
                If (Motor2V>63) Motor2V=5;
                delay_ms(700);
              }
              LASERMODE=255;
              break;
       
       case 5://3 motor slow mode =================
              Motor1V=63;//initial acceleration
              Motor2V=63;
              Motor3V=63;
              delay_ms(20);
             
              for (I=0; I<10; I++) {
                Motor1V=(rand()/64)+5;
                Motor2V=(rand()/64)+5;
                Motor3V=(rand()/64)+5;
                delay_ms(1500);
              }
              LASERMODE=255;
              break;
       
       case 6://2 motor slow mode =================
              Motor1V=63;//initial acceleration
              Motor2V=63;
              Motor3V=0;
              delay_ms(20);
             
              for (I=0; I<10; I++) {
                Motor1V=(rand()/64)+5;
                Motor2V=(rand()/64)+5;
                delay_ms(1500);
              }
              LASERMODE=255;
              break;
             
       case 7://2,5 motor slow mode =================
              Motor1V=63;//initial acceleration
              Motor2V=63;
              Motor3V=63;
              delay_ms(20);
              Motor3V=5;
             
              for (I=0; I<10; I++) {
                Motor1V=(rand()/32)+5;
                Motor2V=(rand()/32)+5;
                delay_ms(1500);
              }
              LASERMODE=255;
              break;
       
       case 255://pause/restart/random mode set
               output_low(PIN_A1);
               
               Motor1V=63;
               Motor2V=0;
               Motor3V=0;      //Laser cooling help
               delay_ms(5000);
               
               Motor1V=0;
               delay_ms(10000);
               
               srand(rand());
               LASERMODE=rand()/32;
               output_high(PIN_A1);
               break;
       
       default: //mode not found
               LASERMODE=rand()/32;
               output_high(PIN_A1);
               break;
             
     }
   }

}


Code:

#include <12F675.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES PUT                      //Power Up Timer
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOPROTECT

#use delay(internal=4000000)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 03, 2013 12:21 am     Reply with quote

Take a close-up, focused photo of your board and post it on the internet
and then post a link to it here. If you don't have a website, post it on a
free image hosting website such as http://imageshack.us and then post
a link to it.
FJMSoft



Joined: 20 Oct 2013
Posts: 36

View user's profile Send private message

PostPosted: Tue Dec 03, 2013 1:12 am     Reply with quote

Thanks for your answer.

The device still a prototype so it is in the universal/breadboard yet...
It is very ugly, I very ashamed to show it Sad

http://www.fjm.xpg.com.br/projectimg/SAM_2265.JPG
http://www.fjm.xpg.com.br/projectimg/SAM_2266.JPG
Ttelmah



Joined: 11 Mar 2010
Posts: 19454

View user's profile Send private message

PostPosted: Tue Dec 03, 2013 1:55 am     Reply with quote

First thing, clamp diodes.
Don't rely on the internal diodes in the FET structure to clamp flyback when you switch things off. They are very slow, typically have much lower current ratings than the device itself, and can give significant problems, unless specifically designed for this (some devices have properly structured separate diodes specifically built in - The IRLZ14 does).
Then look at the gate voltage of your FET, capacitance, and the drive capabilities of the PIC. For example, the 2N7000, at a Vgs of perhaps 4.4v (which is all the PIC can manage), is only rated for about 0.4A. How much are you drawing?. Then realise that driving the gate capacitance for a reasonably fast on/off time needs significant current. The gate charge curves show Id values from 115mA to 500mA. Can the PIC deliver this?. People get away with driving FET's like this for simple ON/OFF drives, at low rates, but when you start using PWM, you need to think about adding a separate gate driver....
Then should a pin on the PIC, be left floating?. Pin's should always be either driven, or clamped to something.

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9205
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Dec 03, 2013 6:16 am     Reply with quote

more stuff...
replace the FETs with just LEDs for test purposes.be sure to use current limiting resistors(typ. 330r).
the PIC should NEVER get HOT ! sounds like you're trying to suck a LOT of power from it( FET gate resistors are missing in diagram!)
try simple on/off/timed functions instead of 'random' events especially for testing.that way you can prove you've got solid reliable hardware

hth
jay
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Dec 03, 2013 3:12 pm     Reply with quote

Couple more things - in addition to the 100 uf capacitor on the supply, you need some good bypass caps ( .1 uf ceramic for example) across the power/gnd pins on the processor - as close to the processor as possible. The 100 uf will filter the low frequency stuff, but they don't take out high frequency components (which can drive logic circuits nuts). Also make sure exactly how your ground is set up - you want the grounds of the motors etc to ground back at the power supply not daisy chained off the ground on the breadboard with your processor on it - motors are notorious (well any inductive load) for creating all sorts of spikes and pulses and ground loops are just asking for problems. As others have indicated, the processor should NOT be getting hot. One mistake many new designers make is not understanding the reality of capacitors. The ideal capacitor graph would have you believe that the impedance goes down as the frequency goes up and you would thing a 100 uf cap would have very low impedance at high frequencies. The reality is electrolytic caps perform well at low to mid frequencies, but at high frequencies, due to various internal inductances, they act more like an open circuit. If you look at any good design, you will almost always find the filter caps in parallel with smaller ceramic caps to filter the high frequency components.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
FJMSoft



Joined: 20 Oct 2013
Posts: 36

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 1:01 am     Reply with quote

Firstly, thanks for all answers.

Ttelmah, friend, I dont understand what is a clamp diode, I tried to search about clamp diodes with mosfet but I didnt found anything that looks applicable here... probably because of my ignorance.
About the motors, I gave attention to this, chose 2N7000 because of the small size, low price and low current... the motor current is very low, I measured 110mA free running and 210mA in the worst case (motor locked), so 2N7000 is working with less than half capacity.
For sure uC cant give 115mA to 500mA... I dont know if this is a right procedure but I tried to measure the current in the gate (PWM in 50%), got nothing in DC, and around 0,003 in AC (another mosfets from new test).
About the pin floating, I thought it would not be a problem but agree that would be better it tied (better tied to gnd or vcc?).
But why GP3 makes uC crazy? Also, why GP2 dont do same even not tied too. (Maybe because GP3 is also reset when set? So it is more sensitive to noise?)

temtronic, It would be hard to exchange the mosfets for LEDs in that board so I exchanged the uC to a common breadboard, I hadnt put LEDs but problem also has not shown up (explained better below).
Yes, I know it should not heat AT ALL.
I really didnt put resistors in gate, I thought it wouldnt be necessary also would not know which resistor to use, but I gave a quick search which said to put 5 ohms to 100 ohms resistor in series, is this right?
Yes, I also made a simplier code (explained better below).

gpsmikey, I gave some tests with the 0.1uF capacitor, but after the new test (explained below) I hardly can make uC get hot again, anyways your capacitor tip is very valid, thank you!
I just didnt understood what you said about the ground planes, sorry :(


--The new test.
I have exchanged the uC to a common breadboard so it was easier to make better tests.
I have used exactly same uC (same for laser module). For mosfets I used new ones, same part but not the ones that was in the other board, also for motors.
I made a new code, much simplier and timed as temtronic sugested. I used same PWM routine, motor1 is constant 100% and motor2 at constant 50%. Main code turns laser on and off also motor 0% and 50% at 1sec rate.
I also desoldered and measured laser module and every motor current for a problem check, all ok.

When uC was in the breadboard I couldnt make it heat in any way!
I got it back to the other board, then I hardly could it heat, tried SEVERAL times but it got heat only 1 time.
Back to old code, same as new code, could put it to heat only 1 time.
I checked and rechecked the board several times.

More details:
Motors are around 110mA free and 210mA when locked.
Laser module is around 350~400mA
PWM is 144hz
Supply is a simple wall supply, rated 5v 2A.

Thank you again.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 2:09 am     Reply with quote

Quote:
I tried to search about clamp diodes with mosfet but I didnt found
anything that looks applicable here.

Go to the link below and read section 7 near the end and look at the schematic:
7. Inductive Switching Issues
http://www.coilgun.eclipse.co.uk/switching_devices.html
Ttelmah



Joined: 11 Mar 2010
Posts: 19454

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 3:05 am     Reply with quote

The PIC itself, has 'protection diodes' built in on it's pins. These though are 'last resort' devices, designed to stop the chip being destroyed. The one pin that doesn't have these, is GP3, because it has to be able to go up to Vpp, when the device is being programmed. Now that touching GP3 brings problems says several things:
1) There is a lot of energy floating around somewhere (look at the article PCM_programmer points to for one part of this - inductive switching). There is also a separate issue with a motor, of 'dynamo generation', because of it's mechanical inertia - lower voltage, but still has to go somewhere....
I always like to draw people's attention to the little flash guns that run off a few AAA cells at this point. These need several hundred volts across the tube, and a couple of thousand volts on the trigger wire, yet if you look at the circuit, it is just a transistor switching an inductor on/off, and a couple of diodes and a capacitor to use the voltage generated, instead of discharging it. It is almost unbelievably easy to generate damaging/dangerous voltages with an inductor, and a switching element!...

2) You need to provide your own protection to all the pins. Floating pins, are always an 'accident waiting to happen'. For the unused GP pins except GP3, set them as outputs and drive low/high. For GP3, ensure you add an external resistor to clamp it to a rail.

3) General rule, is you want to keep voltages and current like this _away_ from the PIC, so as well as adding your own protection at the PIC, you want to be snubbing at the motor. The clamp diode shown at the end of the article PCM_programmer pointed to. For your application, this is probably all that is needed.

You really do need to get some form of scope, if dealing with electronics. Working without one is a bit like trying to assemble a 'flat pack' kitchen cabinet, without the screwdriver and allen keys. Slightly 'old fashioned' analog scopes are almost 'throw away' items at things like radio swap-meets, and well worth looking at. Or see if you can borrow one. Be surprised at just what voltages are on the junction between the FET and motor if you can get one....

Now, the FET input, when driven by a changing voltage has a lot of capacitance. As such high currents are needed to drive it quickly. The PIC is not capable of delivering such currents. You can though limit the current, by adding a series resistor between the PIC and the FET, which then avoids overloading the PIC, and slows slightly the speed you actually switch the FET. Most circuits using a micro to drive a FET like this should show such resistors. If however you want to drive the FET fast, then you need to consider a driver capable of doing this. However for your application this is probably not necessary.

When a FET is taken 'overvoltage' (by 1'), the first path current flows is from the drain to the gate. Hence into the PIC outputs, where the clamp diodes in the PIC, seem to be just about saving the chip. However it is probably pretty borderline (hence the heat.....). Whenever you switch off the motor, this is happening....

Best Wishes
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 8:54 am     Reply with quote

My comment about grounds is based on the fact that while in the ideal world, a wire has no inductance and 0 resistance, in the real world, wires have both resistance and inductance. If you run "GND" from the supply to your control board then run from the control board to the ground on the motors, you will find that the "GND" side jumps all over the place when you start switching heavy loads like the motors. Those spikes will cause all sorts of strange things to happen. You need to have a ground from the supply to the control board. You need another ground that runs from the supply to the gnd on the motors to help eliminate that issue. Inductive loads (motors, relays etc.) generate all sorts of pulses etc. which is why the discussion on snubber diodes etc.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Ttelmah



Joined: 11 Mar 2010
Posts: 19454

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 9:24 am     Reply with quote

Very Very true....
FJMSoft



Joined: 20 Oct 2013
Posts: 36

View user's profile Send private message

PostPosted: Thu Apr 03, 2014 3:03 pm     Reply with quote

Hey guys, I found the problem!

The capacitor pins was a bit rusty so it didnt soldered very well.
This explains why circuit worked well in the breadboard but not in the "veroboard".
Also explains why sometimes the device worked well after been shaked.

Now device works EVERY time it is powered, no miss!

But, still me a doubt... could the lack of a capacitor make a uC heat and burn?
Looks yes but, why?


Thank you everyone.
Ttelmah



Joined: 11 Mar 2010
Posts: 19454

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 1:41 am     Reply with quote

This goes back almost certainly to clamp diodes....

If you don't have any provided by _you_ external to the PIC then any flyback or inductive spike generated, gets clamped by the internal diodes in the PIC _to the supply rail_. What happens then, depends on whether the internal diodes can cope, and how the supply rail responds to having extra power dumped into it. If the amount of capacitance on the rail is small (what will happen if your smoothing capacitor is not making good contact), then the supply voltage will rise significantly. Result 'overvoltage at PIC'. Release of magic smoke...
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 4:20 pm     Reply with quote

FJMSoft wrote:
Hey guys, I found the problem!

The capacitor pins was a bit rusty so it didnt soldered very well.
This explains why circuit worked well in the breadboard but not in the "veroboard".
Also explains why sometimes the device worked well after been shaked.

Now device works EVERY time it is powered, no miss!

But, still me a doubt... could the lack of a capacitor make a uC heat and burn?
Looks yes but, why?


Thank you everyone.

Clamp diodes or no clamp diodes, your PIC supply rail will swing around violently as you switch loads on and off without a decent size cap across the rails.
The inductance of the leads from your power supply to the PIC stores the energy to do this.
When the voltage goes high, the PIC's internal circuits break down, and the device gets hot!

Mike
.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 4:39 pm     Reply with quote

One trick I have used assuming your mosfet has a series resistor driving the gate is to add a little bit of capacitance from the gate to ground. The capacitance slows down the rise/fall time of the gate and slows down the turn-on/off time of the inductive load which significantly lessens the spikes created. The trick is to slow it down enough to mostly get rid of the transients, but not so much that the mosfet overheats since it is now spending longer in the linear region. At high frequencies, this does not work very well, but if you are using something like 400hz or simply turning something on/off periodically, this can make a big difference. There are two important parts of dealing with transients - the first is to minimize them to start with, then the second part is to deal with the ones that remain.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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