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

Problem with stepper motor

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
spom



Joined: 12 Jul 2007
Posts: 32

View user's profile Send private message

Problem with stepper motor
PostPosted: Wed Apr 02, 2008 5:02 am     Reply with quote

Hi,
I have a problem with my stepper motor.
I use a PIC 18F2480, L293D as a driver and a motor which uses 4.6V and 0.8 A/Phase.
I use the same power supply both for the PIC and for the motor.
The problem is that the motor makes three or four steps and then the programm starts at the begin.
Why? Have I forgotten any component?
Thanks!
Code:
#include <18f2480.h>
#device *=16
#device ADC=10
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,BRGH1OK,stream=debug)
#include <input.c>

int16 seconds;      // A running seconds counter
int8 int_count;    // Number of interrupts left before a second has elapsed


#define INTS_PER_SECOND 76                   // (20000000/(4*256*256))
#int_rtcc                                    // This function is called every time
void clock_isr() {                           // the RTCC (timer0) overflows (255->0).
                                             // For this program this is apx 61 times
    if(--int_count==0) {                     // per second.
      ++seconds;
      int_count=INTS_PER_SECOND;
    }

}

#BYTE port_b = 0xf81
BYTE const POSITIONS[8] = {0b0101,
                           0b0001,
                           0b1001,
                           0b1000,
                           0b1010,
                           0b0010,
                           0b0110,
                           0b0100};

drive_stepper(BYTE speed, char dir, int16 steps) {
   static BYTE stepper_state = 0;
   BYTE i;

   for(i=0; i<steps; ++i) {
     delay_ms(speed);
     fprintf(debug,"i: %u;\n",i);
     set_tris_b(0xf0);
     port_b = POSITIONS[ stepper_state ];
     if(dir!='R')
       stepper_state=(stepper_state+1)&(sizeof(POSITIONS)-1);
     else
       stepper_state=(stepper_state-1)&(sizeof(POSITIONS)-1);
   }
}

main() {

   BYTE speed;
   int16 steps;
   char dir;

   while (TRUE)
   {
       speed = 1;
       dir = 'F';
       
       printf("\n\rSteps (hex): ");
       steps = gethex();

       drive_stepper(speed,dir,steps);
       delay_ms(1000);
       seconds = 0;
       while(seconds < 60)
       {
       ;
       }
   }
}
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: Problem with stepper motor
PostPosted: Wed Apr 02, 2008 6:10 am     Reply with quote

Maybe the motor spikes are not sufficiently decoupled and are getting back into the PIC and resetting it. An easy way to find out is to disconnect the motor and driver and run the PIC all by itself. You have an RS-232 debug of the stepping function that will tell you if it is working or being reset.

Robert Scott
Real-Time Specialties
Ttelmah
Guest







PostPosted: Wed Apr 02, 2008 7:15 am     Reply with quote

One other comment. Have you got the trap diodes installed?. The L293, doesn't include it's own trap diodes, and these must be added (they are shown on the example circuits).

Best Wishes
spom



Joined: 12 Jul 2007
Posts: 32

View user's profile Send private message

PostPosted: Wed Apr 02, 2008 7:34 am     Reply with quote

I use L293D. So I thought I don't need any more diodes as the diodes are installed in the driver, isn't it?
How can I better decoupling the motor spikes?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 02, 2008 8:59 am     Reply with quote

See this post about power supply connections to a motor.
http://www.ccsinfo.com/forum/viewtopic.php?t=32927&start=7&highlight=motor
Ttelmah
Guest







PostPosted: Wed Apr 02, 2008 10:24 am     Reply with quote

PCM programmer has pointed to a good page about the general connection layout.
The big question I have, is how is the Vs of the driver chip connected and decoupled, relative to the PIC?. The problem is that as you step, the energy in the coils, when disconnected, has to go somewhere. the effect will be to put significant spikes on the supply rail at the driver. If the capacitance on the rail is not enough to store this without the supply rail rising significantly, the result will be significant noise on this rail. Also, though the size of the capacitors matters for this, separately you have the question of their ESR (which is why it is common to see several slightly smaller capacitors, in some places where one large one would seem more obvious), and also how they react to high frequencies (most electrolytic capacitors behave quite poorly at HF).
So:
(not very good at ASCII art - fixed pitch font required...)
Code:

Vin ---+----------()()()----+----+---
       |           ()()     |    |
      ---                  ---  ---
      ___ At PIC           ___  ___  To Motor
       |                   | |   |
       |                   ---   |
       |                    |    |
0V -+--+                    +----+---
    |                       |
    ------------------------

Decouple the supply at the motor, with both a reasonably large electrolytic, and a small ceramic or polyester cap in parallel.
Make sure the ground connection for the PIC, is not 'picked off' along the run to the motor.
Decouple right close to the PIC with a ceramic or polyester cap close to the chip (I am assuming the supply already has decoupling present).
You might consider adding an inline inductor on the supply to the motor as shown.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Apr 02, 2008 12:37 pm     Reply with quote

One thing that I could think of is that the motor might not be in the same 'physical' position as the 'electrical' position of the PIC. Upon start up, the PIC has it's outputs in a default state. The motor will move to this state. Then, the PIC will (I assume) set the outputs to it's initialized state. The motor will need to move to this state.

Not knowing the exact details of your project, this is what comes to mind first.

Ronald
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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