Bipolar Stepper Motor Controlling using 16F877A

Discussion in 'Embedded Systems and Microcontrollers' started by Shyamal805, Aug 4, 2013.

  1. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    Hi,
    I'm new in microcontroller programming. I am using PIC16F877A for rotating bipolar stepper motor & controlling. I have done one code using embeded C on MPLAB. I see that my code is working good. I also use Proteus Professional to see the output.

    The code is -
    Code ( (Unknown Language)):
    1.  
    2. *******************************************************
    3. #include<16F877A.h>
    4. #fuses HS,NOWDT
    5. #use delay(clock=10000000)
    6. #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
    7.  
    8. void main()
    9. {
    10.  while(true){
    11.   set_tris_a(0x06);
    12.   set_tris_b(0x00);
    13.   output_b(0x00);
    14.  
    15.   if(input(PIN_A1) == 0){
    16.   int i;
    17.   for(i=0; i<6; i++)
    18.     {
    19.      output_b(0x01);     //1 No. PIN
    20.         delay_ms(500);
    21.      output_b(0x00);
    22.      if(input(PIN_A2) == 0){
    23.         goto stop;
    24.      }
    25.  
    26.      output_b(0x02);     //2 No. PIN
    27.         delay_ms(500);
    28.      output_b(0x00);
    29.      if(input(PIN_A2) == 0){
    30.         goto stop;
    31.      }
    32.  
    33.      output_b(0x04);     //3 No. PIN
    34.         delay_ms(500);
    35.      output_b(0x00);
    36.      if(input(PIN_A2) == 0){
    37.         goto stop;
    38.      }
    39.  
    40.      output_b(0x08);     //4 No. PIN
    41.         delay_ms(500);
    42.      output_b(0x00);
    43.      if(input(PIN_A2) == 0){
    44.         goto stop;
    45.      }
    46.  
    47.     }
    48.    for(i=0; i<6; i++)
    49.     {
    50.      output_b(0x04);      //3 No. PIN
    51.         delay_ms(500);
    52.      output_b(0x00);
    53.      if(input(PIN_A2) == 0){
    54.         goto stop;
    55.      }
    56.  
    57.      output_b(0x02);      //2 No. PIN
    58.         delay_ms(500);
    59.      output_b(0x00);
    60.      if(input(PIN_A2) == 0){
    61.         goto stop;
    62.      }
    63.  
    64.      output_b(0x01);     //1 No. PIN
    65.         delay_ms(500);
    66.      output_b(0x00);
    67.      if(input(PIN_A2) == 0){
    68.         goto stop;
    69.      }
    70.  
    71.      output_b(0x08);     //4 No. PIN
    72.         delay_ms(500);
    73.      output_b(0x00);
    74.      if(input(PIN_A2) == 0){
    75.         goto stop;
    76.      }
    77.     }
    78.     stop: continue;
    79.     }    //end if
    80.     }    //end while
    81. }        //end main
    82. *******************************************************
    83.  
    Here, i use 2 input and 4 output. To see the 4 output use 4 LED.

    When input(PIN_A1) is ON then LED's are ON sequentially and when input(PIN_A2) is ON then the all LED's are OFF. It's ok.

    But again, when input(PIN_A1) is ON then LED's are ON sequentially from the beginning. It is my problem. I want to pause the program and starts again from the present states.

    I'm new in this sector that's why can't understand how can i control this. Please someone help me ......
     
    Last edited by a moderator: Aug 4, 2013
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    I think you need an H-bridge with a bipolar stepper motor. The uC don't source enough current to drive the motor, most likely...

    You also need some routine/function to register the position of the motor, if you want it to run, then stop, and then continue from the last position.
     
  3. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    I know but for this reason i use motor drive IC293.
     
  4. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    Please give me some more details about this. How can i do it ...
     
  5. ligo.george

    New Member

    Feb 2, 2013
    19
    2
    Here is the code for driving bipolar stepper motor.... and circuit diagram.. The code is written in mikroc.. so take its algorithm, you can simply convert it to your code..
    Circuit Diagram
    [​IMG]
    Code (take the algorithm in this code
    Code ( (Unknown Language)):
    1.  
    2.  do
    3.   {
    4.     PORTB = 0b00000001;
    5.     Delay_ms(500);
    6.     PORTB = 0b00000100;
    7.     Delay_ms(500);
    8.     PORTB = 0b00000010;
    9.     Delay_ms(500);
    10.     PORTB = 0b00001000;
    11.     Delay_ms(500);
    12.   }while(1);
    13.  
    This circuit diagram and code is taken from the following page..
    Interfacing Stepper Motor with PIC Microcontroller - MikroC
     
  6. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    ligo.george

    What i am say what are you telling....
     
  7. ligo.george

    New Member

    Feb 2, 2013
    19
    2
    Sorry, I haven't read your problem completely...
    Save the status of the step in the PIC's EEPROM... and reload it when you resume..
     
  8. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    Thanks,
    Doy you tell me how can i save the present step status in the Pic's EEPROM and reload it?
     
  9. ligo.george

    New Member

    Feb 2, 2013
    19
    2
    In mikroc there are built in functions for reading from and writing to eeprom.. use the following functions.. .. You may need to make some changes depending upon the compiler you are using..
    To read data from internal eeprom
    Code ( (Unknown Language)):
    1.  
    2. unsigned char readEEPROM(unsigned char address)
    3. {
    4.   EEADR = address; //Address to be read
    5.   EECON1.EEPGD = 0;//Selecting EEPROM Data Memory
    6.   EECON1.RD = 1; //Initialise read cycle
    7.   return EEDATA; //Returning data
    8. }
    9.  
    To write data to eeprom
    Code ( (Unknown Language)):
    1.  
    2. void writeEEPROM(unsigned char address, unsigned char datas)
    3. {
    4.   unsigned char INTCON_SAVE;//To save INTCON register value
    5.   EEADR = address; //Address to write
    6.   EEDATA = datas; //Data to write
    7.   EECON1.EEPGD = 0; //Selecting EEPROM Data Memory
    8.   EECON1.WREN = 1; //Enable writing of EEPROM
    9.   INTCON_SAVE=INTCON;//Backup INCON interupt register
    10.   INTCON=0; //Diables the interrupt
    11.   EECON2=0x55; //Required sequence for write to internal EEPROM
    12.   EECON2=0xAA; //Required sequence for write to internal EEPROM
    13.   EECON1.WR = 1; //Initialise write cycle
    14.   INTCON = INTCON_SAVE;//Enables Interrupt
    15.   EECON1.WREN = 0; //To disable write
    16.   while(PIR2.EEIF == 0)//Checking for complition of write operation
    17.   {
    18.     asm nop; //do nothing
    19.   }
    20.   PIR2.EEIF = 0; //Clearing EEIF bit
    21. }
    22.  
    Source :
    Using Internal EEPROM PIC Microcontroller
     
Loading...