C program to rotate 2 motors in clock wise and anti-clock wise direction using PIC 16f877a Micro controller.

C program to rotate 2 motors in clock wise and anti-clock wise direction using PIC 16f877a Micro controller.


 Note: The Microcontroller cannot drive the motor if they are connected directly to the ports. in practical scenario, we need to use L293d/ULN2003A motor drive.

 
C program:

 #include <htc.h>
__CONFIG(0x3F3A);
#define LP RC0
#define LN RC1
#define RP RC2
#define RN RC3

void go_front();
void go_back();
void turn_left();
void turn_right();
void stop_motors();

void main()
{       
TRISC=0;
while(1)
{
go_front();
delay();      
go_back();
delay();
turn_left();
delay();      
turn_right();
delay();      
stop_motors();
delay();      
}
}
  
delay()
{
int i,j;
for(i=0;i<1000;i++)
for(j=0;j<500;j++);
}       

void go_front()
{
RC0=1;
RC1=0;
RC2=1;
RC3=0;
}

void go_back()
{
RC0=0;
RC1=1;
RC2=0;
RC3=1;
}       

void turn_left()
{
RC0=0;
RC1=0;
RC2=0;
RC3=1;
}       

void turn_right()
{
RC0=0;
RC1=0;
RC2=1;
RC3=0;
}       

void stop_motors()
{
RC0=0;
RC1=0;
RC2=0;
RC3=0;
  
}       

Proteus Simulated design: 

< click on the image for enlarged view >



Video Screen Capture