Help with C+ program to drive Bipolar Stepper motor using PIC18F1320 Microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by zabumafu, Nov 20, 2013.

  1. zabumafu

    Thread Starter New Member

    Nov 20, 2013
    Hello I have very little experience and or instructions on how to program this however here is what I have. I am using a pic 18f1320 microcontroller and 2 H-bridges to drive a Mercury Stepper Motor (SM-42BYG011-25) in order for it to rotate one way with the one of the LED's lit on the microcontroller, stop and light a different LED, and then reverse its direction (clockwise to counterclock wise or v.v.) and keep it in a loop. If someone could help me through this that'd be amazing, ill probably be sticking around this forum now that I'm getting into electrical engineering.

    The datasheet for the pic18f1320 is below:
    PIC18F1220/1320 Data Sheet 18/20/28-Pin High ... - Microchip

    I know its suppose to be in 4 parts with the Coil Energization Sequence as follows
    (I believe this is the correctly matched pins:

    Coil Energization Sequence
    RA0: H-H-L-L-L
    RA1: L-L-H-H-L
    RA2: H-L-L-H-L
    RA3: L-H-H-L-L
    with H being high output and L being low and the last L for RA0,1,2,3 being L meaning off. (My professor gave me a circuit diagram for a PIC16F84A and it had this written on it and I am trying to match these RA0 pins to B2, B3 etc. on my Pic 18f1320. A picture of what he gave me and the microcontroller and 2 H bridges are below:

    I am using a PCW compiler program to compile the code in C+ then MPLAB to program the microcontroller. This is what I have thus far for one step but how do I stop it, reverse it, and loop it.
    *PLEASE place comments along the code so I know whats going on so I can edit delays etc.

    Here is the code I made thusfar and it compiled fine in PCW compiler program:


    Code ( (Unknown Language)):
    2. #include <18f1320.h> /*tell compiler we are using a 18f1320.h microcontroller */
    4. #use delay (clock=4000000)
    5. //LED lights
    6. #define GREEN_LED PIN_A3
    7. #define YELLOW_LED PIN_B0
    8. #define RED_LED PIN_B1
    9. //methods
    10. Main(){
    11. void part_1(void){
    12. Output_low(PIN_A3); //led green
    13. Output_low(PIN_B2);
    14. Output_high(PIN_B3);
    15. Output_high(PIN_A7);
    16. Output_low(PIN_A6);
    17. Delay_ms(2500);
    18. Output_high(PIN_A3);
    19. //turn off led
    20. }
    21. void part_2(void) {
    22. Output_low(PIN_B1); //led yellow
    23. Output_high(PIN_B3);
    24. Output_low(PIN_B2);
    25. Output_low(PIN_A7);
    26. Output_high(PIN_A6);
    27. Delay_ms(2500);
    28. Output_high(PIN_B1);
    29. }
    30. void part_3(void) {
    31. Output_low(PIN_B0); //led red
    32. Output_low(PIN_B3);
    33. Output_high(PIN_B2);
    34. Output_low(PIN_A7);
    35. Output_high(PIN_A6);
    36. Delay_ms(2500);
    37. Output_high(PIN_B0);
    38. }
    40. void part_4(void) {
    41. Output_low(PIN_B0); //led red
    42. Output_low(PIN_B3);
    43. Output_high(PIN_B2);
    44. Output_high(PIN_A7);
    45. Output_low(PIN_A6);
    46. Delay_ms(2500);
    47. Output_high(PIN_B0);
    48. }
    49. }
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    To reverse the motor you need to keep separate control of the "count".

    That count is the postion of the motor, and turning forward would be;
    obviously turning in reverse would be;

    I would split the code into two areas;
    1. count sequencing, ie count up 0123 or count down 3210
    2. motor control, this sets the pins for each possible state; 0,1,2,3
    zabumafu likes this.
  3. zabumafu

    Thread Starter New Member

    Nov 20, 2013
    Okay thank you, I'm gonna do my best to rewrite it as so and update my post with the new code to see if its better. I won't be able to test it in the lab until tomorrow.