Bipolar Stepper Motor Controlling using 16F877A

Thread Starter

Shyamal805

Joined Mar 20, 2012
27
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 -
Rich (BB code):
*******************************************************
#include<16F877A.h> 
#fuses HS,NOWDT
#use delay(clock=10000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)

void main()
{
 while(true){
  set_tris_a(0x06);
  set_tris_b(0x00);
  output_b(0x00);

  if(input(PIN_A1) == 0){
  int i;
  for(i=0; i<6; i++)
    {
     output_b(0x01);     //1 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }

     output_b(0x02);     //2 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }

     output_b(0x04);     //3 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }

     output_b(0x08);     //4 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }

    }
   for(i=0; i<6; i++)
    {
     output_b(0x04);      //3 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }
 
     output_b(0x02);      //2 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }

     output_b(0x01);     //1 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }

     output_b(0x08);     //4 No. PIN
        delay_ms(500);
     output_b(0x00);
     if(input(PIN_A2) == 0){
        goto stop;
     }
    }
    stop: continue;
    }    //end if
    }    //end while
}        //end main
*******************************************************
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 ......
 

Attachments

Last edited by a moderator:

nerdegutta

Joined Dec 15, 2009
2,676
I am using PIC16F877A for rotating bipolar stepper motor & controlling.
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.
 

ligo.george

Joined Feb 2, 2013
19
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

Code (take the algorithm in this code
Rich (BB code):
 do
  {
    PORTB = 0b00000001;
    Delay_ms(500);
    PORTB = 0b00000100;
    Delay_ms(500);
    PORTB = 0b00000010;
    Delay_ms(500);
    PORTB = 0b00001000;
    Delay_ms(500);
  }while(1);
This circuit diagram and code is taken from the following page..
Interfacing Stepper Motor with PIC Microcontroller - MikroC
 

ligo.george

Joined Feb 2, 2013
19
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
Rich (BB code):
unsigned char readEEPROM(unsigned char address)
{
  EEADR = address; //Address to be read
  EECON1.EEPGD = 0;//Selecting EEPROM Data Memory
  EECON1.RD = 1; //Initialise read cycle
  return EEDATA; //Returning data
}
To write data to eeprom
Rich (BB code):
void writeEEPROM(unsigned char address, unsigned char datas)
{
  unsigned char INTCON_SAVE;//To save INTCON register value
  EEADR = address; //Address to write
  EEDATA = datas; //Data to write
  EECON1.EEPGD = 0; //Selecting EEPROM Data Memory
  EECON1.WREN = 1; //Enable writing of EEPROM
  INTCON_SAVE=INTCON;//Backup INCON interupt register
  INTCON=0; //Diables the interrupt
  EECON2=0x55; //Required sequence for write to internal EEPROM
  EECON2=0xAA; //Required sequence for write to internal EEPROM
  EECON1.WR = 1; //Initialise write cycle
  INTCON = INTCON_SAVE;//Enables Interrupt
  EECON1.WREN = 0; //To disable write
  while(PIR2.EEIF == 0)//Checking for complition of write operation
  {
    asm nop; //do nothing
  }
  PIR2.EEIF = 0; //Clearing EEIF bit
}
Source :
Using Internal EEPROM PIC Microcontroller
 
Top