Controlling stepper motor speed with PWM (problem in code)

Discussion in 'Embedded Systems and Microcontrollers' started by theumer, Mar 29, 2012.

  1. theumer

    Thread Starter New Member

    Mar 29, 2012
    2
    0
    I've been writing a code to control the movements and speed of stepper motor but I have been facing some problem in the speed control. Step angle of the motor is 7.5 degrees and the angle movement is from -90 to 0 to +90. But the motor does not seem to operate properly. Here's the code: (location of probable flaw/guidance would be appreciated)

    Code ( (Unknown Language)):
    1.  
    2.  
    3. //////////////////   delay.h   ///////////////////
    4.  
    5. #ifndef    XTAL_FREQ
    6. #define    XTAL_FREQ    4MHZ        /* Crystal frequency in MHz */
    7. #endif
    8.  
    9. #define    MHZ    *1000L            /* number of kHz in a MHz */
    10. #define    KHZ    *1            /* number of kHz in a kHz */
    11.  
    12. #if    XTAL_FREQ >= 12MHZ
    13.  
    14. #define    DelayUs(x)    { unsigned char _dcnt; \
    15.               _dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
    16.               while(--_dcnt != 0) \
    17.                   continue; }
    18. #else
    19.  
    20. #define    DelayUs(x)    { unsigned char _dcnt; \
    21.               _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
    22.               while(--_dcnt != 0) \
    23.                   continue; }
    24. #endif
    25.  
    26. extern void DelayMs(unsigned char);
    27.  
    28. ///////////////////////////////////// delay.c ///////////////////
    29.  
    30.  
    31. #include    "delay.h"
    32.  
    33. void
    34. DelayMs(unsigned char cnt)
    35. {
    36. #if    XTAL_FREQ <= 2MHZ
    37.     do {
    38.         DelayUs(996);
    39.     } while(--cnt);
    40. #endif
    41.  
    42. #if    XTAL_FREQ > 2MHZ    
    43.     unsigned char    i;
    44.     do {
    45.         i = 4;
    46.         do {
    47.             DelayUs(250);
    48.         } while(--i);
    49.     } while(--cnt);
    50. #endif
    51. }
    52.  
    53. /////////////////////////////// test.c /////////////////////
    54.  
    55. #include <pic.h>
    56. #include "delay.h"
    57.  
    58. __CONFIG(WDTDIS & XT & WRTEN);
    59.  
    60. void main ()
    61. {
    62.     TRISB = 0xFF;
    63.     TRISC = 0x00;
    64.     while (1)
    65.     {
    66.         if(RB0==1)     //////////////// Clockwise Rotation /////////////
    67.         {
    68.             PORTC = 0x66;
    69.             DelayMs(200);
    70.             PORTC = 0xCC;
    71.             DelayMs(200);
    72.             PORTC = 0x99;
    73.             DelayMs(200);
    74.             PORTC = 0x33;
    75.             DelayMs(200);
    76.         }
    77.         if(RB1==1)    ///////////////// Counterclockwise Rotation ///////
    78.         {
    79.             PORTC = 0x66;
    80.             DelayMs(200);
    81.             PORTC = 0x33;
    82.             DelayMs(200);
    83.             PORTC = 0x99;
    84.             DelayMs(200);
    85.             PORTC = 0xCC;
    86.             DelayMs(200);
    87.         }
    88.         //if(RB0==0 & RB1==0)
    89.         //{
    90.         //    PORTC = 0x00;
    91.         //    DelayMs(200);
    92.         //}
    93.     }
    94. }
    95.  
    96. ////////////////////////////////////////////////
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You do know that Hi-Tech C have excellent built in delay functions __DELAY_MS and __DELAY_US. I would recommend using these functions. Remember to use the prior definition of preprocessor symbol _XTAL_FREQ in Hz like this.
    Code ( (Unknown Language)):
    1.  
    2. #define XTAL_FREQ 4000000
    3.  
     
  3. shortbus

    AAC Fanatic!

    Sep 30, 2009
    4,000
    1,512
    Don't know anything about 'code' but, step motors don't use PWM to rotate. The drivers only need a 'pulse'. Depending on the step setting(half step -etc) each pulse moves the shaft of the motor. Pulse length- PWM, has nothing to do with it.

    Example - for your 7.5° motor in full step mode - 360° / 7.5° = 48 pulses for one revolution. To change speed, you just change frequency of pulses. Most drivers step on the positive going pulse.
     
Loading...