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 CCS Technical Support

Stepper motor --> whats wrong with this code

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



Joined: 17 Sep 2009
Posts: 16

View user's profile Send private message

Stepper motor --> whats wrong with this code
PostPosted: Sun Sep 26, 2010 8:49 am     Reply with quote

Hi.

I can't run the stepper motor.
If I check voltage before i the ULN driver I get no outputs.
So there must be error in the program but I can't find out what.

This is the link for schematics:
http://tinypic.com/view.php?pic=rkrevn&s=7

There are two micro switches, depending on the position, motor should run forward or backward.

This is the full program:
Code:

//--------------------------------------------------------------------------
// motor.h

#include <16F628A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
//#FUSES RESERVED               //Used to set the reserved FUSE bits

#use delay(internal=4M)

//---------------------------------------------------------------------------
#include "motor.h"

#define debounce 3            // debounce for mmicroswitch

#define start_delay 150       // delay for motor start in ms
                           
#define speed 0             // higher==slower

#define pause_1 1500       // pause in position 1 in milisecunds
#define pause_2 500        // pause in position 2 in milisecunds

#define FOUR_PHASE TRUE    // number of windings in the motor
                           // TRUE - 4
                           // FALSE - 2
                       
#define direction 1        // CV0
                           // CCV

#ifdef FOUR_PHASE

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

unsigned char my_flags = 0;
// 0 - detection on input 1 (pin A 0) - stop in forward
// 1 - detection on input 1 (pin A 1) - stop in backward
// 2 - motor is running

unsigned char debounce_timer = 0;
unsigned long motor_pause = 0;

#int_RTCC               
void  RTCC_isr(void) // timer0 interrupt each 1 ms.
{
   unsigned char tmp;
   
   if (motor_pause--);
   
   tmp = (~input_a()) & 0x03;    // port status send to tmp
   
   if (debounce_timer)          // is timer activated
   { 
      debounce_timer--;
   }
   else
   {
      switch (tmp)
      {
         case 0x01 : // detected input 1
                     if (my_flags & 0x01)
                     {
                        my_flags &= 0xFA;       // debaunce OK,stop the motor, delete flag
                        motor_pause = pause_1;    // set pause 1
                     }
                        else
                     {
                        my_flags = 0x01;        // debaunce started,set flag 
                        debounce_timer = debounce;
                     }
                     break;
         case 0x02 : // detected input 2
                     if (my_flags & 0x02)
                     {
                        my_flags &= 0xF9;   
                        motor_pause = pause_2;
                     }
                        else
                     {
                        my_flags = 0x01;   
                        debounce_timer = debounce;
                     }
                     break;
         case 0x00 : my_flags &= 0xFC; // kill all flags
                     break;
         default   : my_flags &= 0xF8; // error, stop the motor
                     break;
      }
   }
}

void drive_stepper(void)
{
   static BYTE stepper_state = 0;
   BYTE i;

   while (my_flags & 0x04)
   {
      //delay_ms(speed); // for max speed
      output_b (POSITIONS[ stepper_state ]);
      if(direction) // if forward
      {
         stepper_state=(stepper_state+1)&(sizeof(POSITIONS)-1);
      }
      else
         stepper_state=(stepper_state-1)&(sizeof(POSITIONS)-1);
   }
}

void main()
{

   set_tris_a(0x0F); // PORTA 0 - 3 inputs for triggers
   set_tris_b(0x00); // PORTB 0 - 3 inputs for motor.                   
   
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   
   delay_ms(500);

   for(;;)
   {
      if (!(my_flags & 0x04)) // if the motor is stopped
      {
         if(!motor_pause) // if the pause if over
         {
            debounce_timer = start_delay; // run delay to ignore inputs on motor start
            drive_stepper(); // run motor       
         }
      }
   }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Sun Sep 26, 2010 10:00 am     Reply with quote

Obvious first thing, you have 'MCLR' enabled, but nothing pulling the MCLR pin high in the schematic. The PIC won't run....
Fix this, and you should then see something. You'll probably have to add the delay back in the motor drive loop, or the pulses will be much too fast for the motor.

Best Wishes
W_Schauberg



Joined: 17 Sep 2009
Posts: 16

View user's profile Send private message

PostPosted: Wed Sep 29, 2010 12:32 am     Reply with quote

I removed the MCLR from the code, but I still can't get no outputs.
I can add some LED's and set some other outputs ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Wed Sep 29, 2010 1:49 am     Reply with quote

Removing MCLR, still leaves it enabled. It is the default setting. You need to have 'NOMCLR', or add the pull-up to the pin.
Also, you need the delay in the driver loop. Fastest that most steppers can 'start' with simple drives like this is probably around 500Hz to 1KHz, if only lightly loaded. So you need some delay in this loop as it is currently used (delay_ms(1) will probably work).

Best Wishes
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