Is my C code to control stepper motor ok?

Discussion in 'Programmer's Corner' started by Guinness, Apr 12, 2010.

  1. Guinness

    Thread Starter Member

    Dec 31, 2009
    81
    1
    Hi, this is the first real program I have done that isn't just a copy of someone else's. As such I think I have got everything in there I need, but as I only started learning C just over a week ago, I expect a few things to be wrong. If you could have a look and point out any errors I have made please.

    The hardware is ok, as I had a different thread to check that out, so I have only included it as refference to check with the C code. I only mention that so it doesn't get confussed as a duplicate thread.

    Code ( (Unknown Language)):
    1. /* The program is designed to make a 200 step Unipolar Stepper Motor turn
    2.  360 degrees once every second*/
    3.  
    4. #include<pic.h>
    5. __CONFIG(XT & WDTDIS & UNPROTECT & MCLREN);
    6. void pause (unsigned short usvalue);
    7. void msecbase (void);
    8. void step (unsigned char phase);
    9. main()
    10. {
    11.       unsigned char counter = 0;
    12.      
    13.       PORTB = 0x00;
    14.       TRISB = 0x00;
    15.      
    16.       while(1==1)
    17.       {
    18.          while(counter<=200)
    19.          {
    20.               step;          //Calls Step Function
    21.               pause(5);      //Calls pause Function which pauses for 5 milli Sec
    22.               counter++;     //increment counter by 1
    23.              
    24.          }    
    25.            
    26.          counter = 0;      //reset counter so while loop can start again
    27.          
    28.       }
    29. }
    30. void pause (unsigned short usvalue)//Calls the 1mS delay function usvalue times
    31. {
    32.      unsigned short x;
    33.      
    34.      for(x=0; x<=usvalue; x++)
    35.      {
    36.               msecbase();
    37.      }
    38.      
    39. }
    40. void msecbase (void)     //This function delays for about 1mS
    41. {
    42.      OPTION = 0x00000001;//sets the prescaler to 1:4
    43.      TMR0 = 0xD;         //presets TMR0 overflow
    44.      while(!T0IF);       //stay untill TMR0 overflow flag equals 1
    45.      T0IF = 0;           //clears the TMR0 overflow flag
    46. }
    47.  
    48. /* The step function is designed to activate step 1 when it is first called.
    49.    When it is called the second time, the phase variable would have been
    50.    incremented to 2, which activates step 2. The same for step 3, but at the
    51.    end of step 4, the phase variable resets to 1, starting the proccess over.*/
    52. void step (unsigned char phase)
    53. {
    54.      unsigned char phase = 1;
    55.      
    56.      switch (phase)
    57.      {
    58.             case 1:
    59.                  PORTB = 160;//RB7 + RB5 go high
    60.                  phase++;    //increments phase to 2
    61.                  break;
    62.             case 2:
    63.                  PORTB = 96;//RB6 + RB5 go high
    64.                  phase++;   //increments phase to 3
    65.                  break;
    66.             case 3:
    67.                  PORTB = 80;//RB6 + RB4 go high
    68.                  phase++;   //increments phase to 4
    69.                  break;
    70.             case 4:
    71.                  PORTB = 144;//RB7 + RB4 go high
    72.                  phase = 1;  //resets phase to 1
    73.                  break;
    74.                  
    75.      }
    76.      
    77. }
    78.  
     
  2. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    Can you please post what compiler you are using, as each has it's own extensions and none-standard instructions / functions for the internal peripherals.

    The only bit of the code that stands out on a quick glance is the function prototype declarations:

    Code ( (Unknown Language)):
    1. void pause (unsigned short usvalue);
    2. void msecbase (void);
    3. void step (unsigned char phase);
    The declarations should not have the actual variables included, just the variable types, like
    Code ( (Unknown Language)):
    1. void pause (unsigned short);
    2. void msecbase (void);
    3. void step (unsigned char);

    The big question is what happens when you try to compile it?
    Happy programming!
     
  3. Guinness

    Thread Starter Member

    Dec 31, 2009
    81
    1
    Its the Hi-Tech lite Compiler that I got with the pic Kit 2 package.

    It wouldn't compile at first, but I done what you said and deleted the variable names from the prototype functions. That got rid of the old failed message, so cheers for that one :)

    I did get a new failed messege about the MCLREN in the CONFIG line at the beginning, I deleted that and it compiled fine then.

    But does that mean I wont be able to use the MCLR pin on the PIC to reset it?
    Or is it turned on by default, and the option is to leave it on or put MCLRDIS to turn it off?
     
  4. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    Does the 16F84 have any alternate function on the MCLR pin?
    It won't need that option unless MCLR can be configures as an I/O pin.

    If you have the PICKIT 2 then you would be better off using a chip that supports the ICD interface, you can then single step through the program and watch the memory & registers to see what is happening from within MPLab.

    It's a brilliant system for debugging.
     
  5. Guinness

    Thread Starter Member

    Dec 31, 2009
    81
    1
    Oh right, will remember that as its only a MCLR pin. I done my first few little programs on the 16F690, so just assumed all PICs had to have it configured.

    I got the starter Kit instead of the Debugging Kit. Is it just a case of making a demo board with different connections, or is there special software that needs to be used with MPLAB to use the debugging system?

    It does sound very usefull, been able to see what the program is doing before putting it in practice and finding out there is a mistake.

    edit: never mind my questions about the degugging kit, I have looked into it now and understand what its all about and with what PIC's I can and can't use it with. Cheers for the info, just have a information overload now in regards to what PIC to get!!!
     
    Last edited: Apr 15, 2010
Loading...