Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC 'stalls' when motors run

Status
Not open for further replies.

ChriX

Member
I'm driving a couple of small motors via a L298 from a 16F876 (not via PWM... yet).

I setup a simple loop, where alternate LEDs flash, which works fine, perfectly in time. Then I set one of the input lines to the L298 high, and added the enable for this channel to go on and off at the same time as one of the LEDs.

Some times the motors will go on and off a few times (although the timing seems very erratic), but more often than not, the PIC appears to stop executing when the motors are on, so the motor is left running. I can't work out what would be causing this. I have measured the supply to the PIC when the motor is running, and it is still normal. I am running from 4 AA cells, with a MAX682 providing power to the logic circuits and PIC, with the motors driven directly from the battery, but I get the same problem when running the lot from a 5V lab supply.

Could the motor noise be disrupting my crystal? This is the only thing I can think of, hopefully you will have some ideas!

Thanks,
Chris





C4&5 = LEDs. C1 = one of the enable pins on the L298.

Code:
LOOP_BEGIN
  bsf PORTC,5  			
  bcf PORTC,4
  bcf PORTC,1
  DELAY_MILLI 250		
  bcf PORTC,5
  bsf PORTC,4	
  bsf PORTC,1
  DELAY_MILLI 250
  goto LOOP_BEGIN
 
I would suggest the noise is reseting the PIC?, for a start do you have supression components across the motor?, these are usually found to be absolutely essential in radio control models.

Try posting an exact circuit of what you are currently using, making sure you show EVERY component.
 
I didn't think it was resetting as I set one LED to come on and go off before the motors come on, so I thought I would've noticed it. The motors are definatley doing something though as when I unplug them the program runs fine, plug them back and it stops. If I run it with the motors attached and then unplug it does not continue.

Anyway, I have a small ceramic cap on each motor, I forget what the value is though, and I can never understand the markings on these - what values should I be looking at? The gearbox/motors are here. I have 100nF across the power lines, 4 diodes around each motor (2 on each side, one from ground and one to +v).

Thanks for your help Nigel (again). Will get hold of my schematic and post it tomorrow as it's on a different pc.
 
one easy check is have an LED blink only when the PIC first starts up (whether it be powering on, or a reset)... then it will be quite obvious if it is resetting on you. but from the sounds of your troubles, I would guess that that is the problem too.
 
ChriX said:
Code:
LOOP_BEGIN
  bsf PORTC,5  			
  bcf PORTC,4
  bcf PORTC,1
  DELAY_MILLI 250		
  bcf PORTC,5
  bsf PORTC,4	
  bsf PORTC,1
  DELAY_MILLI 250
  goto LOOP_BEGIN

You are changing several port C pins in sucession and these are read-modify-write type instructions. In PIC, the second instruction is read and decode while the first is being executed. Somewhere I saw on the Internet that this might cause problem. To see if this is really the case, try the following code and if the motor behaves correctly, then you have found the answer. It involves using a shadow memory location PORTC_TEMP which you have to define early in the asm code.

Code:
LOOP_BEGIN
  bsf PORTC_TEMP,5    ;operate on a shadow RAM location  			
  bcf PORTC_TEMP,4
  bcf PORTC_TEMP,1

  MOVF PORTC_TEMP,W  ;move value into W
  MOVWF PORTC        ;send to port C

  DELAY_MILLI 250		
  bcf PORTC_TEMP,5
  bsf PORTC_TEMP,4	
  bsf PORTC_TEMP,1

  MOVF PORTC_TEMP,W
  MOVWF PORTC

  DELAY_MILLI 250
  goto LOOP_BEGIN
 
eblc: would doing movlw b'00000001' (or whatever) and then movwf onto the port have the same effect as what you posted? I tried that and it made no difference.

evandude: tried that, changed my code to LED on, pause, LED off, pause, motor+LED on, pause, loop. It still gets stuck with the LED on and the motor on.

Anyway here is a bodge of a circuit diagram - couldn't get to the comptuer that my Proteus schematic is on.

**broken link removed**
 
In my previous post I asked for the 'EXACT' circuit, and 'EVERY' component - there are (presumably?) many of them not included?. An obvious part if the power supply?, where does it come from?.
 
I mentioned that the power supply is 4x AA batteries in my first post, the supply to the motors comes straight from the batteries, and the supply to the PIC from a MAX682, I didn't think it important to show the regulator, but have added it now.
 
ChriX said:
I mentioned that the power supply is 4x AA batteries in my first post, the supply to the motors comes straight from the batteries, and the supply to the PIC from a MAX682, I didn't think it important to show the regulator, but have added it now.

Its VITALLY important, which is why I asked for EVERYTHING to be shown.

Assuming this now shows 'everything'?, a glaring omission is decoupling capacitors! - you require some good sized electrolytics across the supply, near the driver IC, and across the battery. There appears to be no reset connection to the PIC?, if left floating this will cause huge amounts of problems. Expecting a reliable regulated 5V off a 6V battery supply seems ambitiously hopeful?, particularly when the 6V batteries feed motors.
 
Well I thought that was everything, but I have actually tied MCLR high, it's just not in my scrawl.

I chose the MAX chip because it was supposed to be able to provide 5v from a range of input voltages from 2.7-5.5, meaning I could use a 4 cell NiMH pack reliably, or not in this case, seeing as i'm currently using Alkalines and getting problems. Also tested it on a lab psu and the same problems.

What values do you suggest for de-coupling?
 
ChriX said:
What values do you suggest for de-coupling?

To some extent the bigger the better?, as it's those that provide the surge current for the motor, I'd certainly be looking at least at 470uF.

Is the regulator chip capable of handling large sudden dips in the incoming supply?.

One thing you might try, is temporarily using a completely seperate supply for the micro.
 
I added some decoupling capacitors to no avail, but then decided to rebuild the circuit without the Maxim chip, and everything worked fine! So I can only assume that the MAX chip was the problem! I don't know whether I had a dud or not, but now I have the chance I think I will go with a more conventional method.

So I need a different way of getting a solid 5V. Would you say 8 AA cells and a LM7805 regulator would be a better way of doing it? I was considering a PP3 but the rechargables I can find don't have anywhere near the capacity i'm looking for - ~300mAh vs 2500mAh with the AAs. I'll have to increase to something like a 4R on the L298 though as my motors are only meant for 3-6V.

Thanks to everyone for their suggestions anyway, i'm glad to have found the problem after being at it for hours.
 
The usual way is to use a 9V battery and 78L05 (or low dropout regulator if you're Audioguru!) to feed the micro, plus your four AA's feeding the motor. It prevents all sorts of potential problems!.
 
ChriX said:
I added some decoupling capacitors to no avail, but then decided to rebuild the circuit without the Maxim chip, and everything worked fine! So I can only assume that the MAX chip was the problem! I don't know whether I had a dud or not, but now I have the chance I think I will go with a more conventional method.

The maxim chip was a charge pump. Noise is what you can expect from that, especially when you didn't put those decoupling caps? I think big decoupler caps on both side of the max chip would have fixed the problem.

-My thoughts
 
I did say in the post you quoted that I added some and it didn't make any difference. Maxim only specify 1u and 2.2u, but I tried these and much larger ones as Nigel suggested. I don't see how noise could cause my PSU to make the noises that it was as well, it was a noise like you get when you short it out by accident.
 
Decoupling capacitor to the power line...

Dear ChriX,

I'm wondering who teach you to apply the 100nF capacitor across the VDD and Vss of the microcontroller. yet my question is How NEAR is the capacitor being applied to the legs that connects those two pins? In my point of view, since your circuit works fine with the LEDs before motors are connected, that means the algorithms may not be the main problem. however, it might due to the power trip because of the sudden huge power drawn during the motor start-up from stalled condition.

From my experience dealing with motor driving using the Microccontroller Unit (MCU), I always applied a 0.01 capacitor (condensor) across the VDD and Vss pin DIRECTLY (direct soldered onto) to the LEGs of the MCU. this was done to protect sudden voltage drop (due to motor extreme current drawn) which causes the MCU to mulfunction.

I hope this will help you...
 
Hiya Chrix,
The problem may be the way you are implemting the bits for the stepper motor. Just by using bsf and bcf could and probably will cause an error writing to the port. By far the best way is use a table for the bits. In another post where I helped out a newbie I posted some simple code to step a unipolar motor every 200 milli seconds. Take a look at that thread and modify the code and see if it works. Also check out the easy steppin book or website as they have all the code in the books available for download, the code isn't commented although it shouldn't be hard to work it out.

Hope this helps

Cheers Bryan :D
 
i think that's the back emf from motors which is disturbing your Micro-controller. Try to Ground, mettalic body of motor and add 10uf 35v tantulum capacitor in power supply near micro-controller.

Regards.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top