Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Controlling Motor using UART in PIC 16f877A

Status
Not open for further replies.

Sathiesh Kumar

New Member
Hi...
I would like to rotate the motor in forward and backward direction.Motor is connected to portD .By sending a Numbers(eg..1) from PC Hyperterminal it should activate the motor .. I have attached six motors in PORTD and PORTC.. I am attaching the sample code which i tested but it doesnt seem to work...
Check this code and kindly suggest any changes...

Code:
void door_open(unsigned char);
void door_close(unsigned char);
char gate_sel;

void main() {
  TRISD=0;
  UART1_Init(9600);              // Initialize UART module at 9600 bps
  Delay_ms(100);                 // Wait for UART module to stabilize

  while (1) {                    // Endless loop
   if (UART1_Data_Ready()) {     // If data is received,
     gate_sel = UART1_Read();     //   read the received data,
     delay_ms(500);
     switch (gate_sel)             //Input from PC Hyperterminal o select Gate
     {
     case 1: door_open(1);          //Funcion to Open the door1
             delay_ms(5000);
             door_close(1);          //Function to close the door1
             delay_ms(5000);
             break;
     case 2: door_open(2);           //Funcion to Open the door1
             delay_ms(5000);
             door_close(2);           //Function to close the door2
             delay_ms(5000);
             break;
     case 3: door_open(3);           //Funcion to Open the door3
             delay_ms(5000);
             door_close(3);          //Function to close the door3
             delay_ms(5000);
             break;
     case 4: door_open(4);           //Funcion to Open the door4
             delay_ms(5000);
             door_close(4);            //Function to close the door4
             delay_ms(5000);
             break;
     case 5: door_open(5);           //Funcion to Open the door5
             delay_ms(5000);
             door_close(5);           //Function to close the door5
             delay_ms(5000);
             break;
     case 6: door_open(6);              //Funcion to Open the door6
             delay_ms(50000);
             door_close(6);              //Function to close the door6
             delay_ms(50000);
             break;
     }
    }
  }
}
void door_open(unsigned char sel)
{
 switch (sel)
 {
 case 1: PORTD=0x02;            //Motor 1 Pos-D1 , Neg-D0
         break;
 case 2: PORTD=0x08;            //Motor 2 Pos-D3 , Neg-D2
         break;
 case 3: PORTD=0x20;            //Motor 3 Pos-D5 , Neg-D4
         break;
 case 4: PORTD=0x80;            //Motor 4 Pos-D7 , Neg-D6
         break;
 case 5: PORTC=0x02;            //Motor 5 Pos-C1 , Neg-C0
         break;
 case 6: PORTC=0x08;            //Motor 6 Pos-C3 , Neg-C2
         break;
 }
}
void door_close(unsigned char sel)
{
 switch (sel)
 {
 case 1: PORTD=0x01;            //Motor 1 Pos-D0 , Neg-D1
         break;
 case 2: PORTD=0x04;            //Motor 2 Pos-D2 , Neg-D3
         break;
 case 3: PORTD=0x10;            //Motor 3 Pos-D4 , Neg-D5
         break;
 case 4: PORTD=0x40;            //Motor 4 Pos-D6 , Neg-D7
         break;
 case 5: PORTC=0x01;            //Motor 5 Pos-C0 , Neg-C1
         break;
 case 6: PORTC=0x04;            //Motor 6 Pos-C2 , Neg-C3
         break;
 }
}


Sathiesh Kumar.V
 
It won't work because the pins of an MCU cannot provide enough current to run a motor. They can supply enough voltage but not enough current. To drive high power devices you need to use the MCU to control things like relays, transistors, amplifiers, or motor drives.

You need to put extra circuitry between the motor and MCU. Like an H-bridge (which is the basic circuit for a bidirectional motor driver).
 
Last edited:
i have used L293d driver motor ic to connect PIC with Motor... whether the logic behind the program is right... whether the switch case statement work...
 
Have you tried putting a multi meter across the pins you expect to go high and low when you send the UART commands to see if they actually do?

You could also check your case statements by outputting to the UART a string such like "DOOR_OPEN Case 1" at each case, so that for case 1 and "DOOR_OPEN Case 2" for case 2 etc.

Wilksey
 
I can't see anything wrong with the code except that there are no config or include lines.

Mike.
 
i got the output... i am attaching the code which works good...

Code:
void door_open(unsigned char);
void door_close(unsigned char);
char gate_sel;

void main() {
  TRISD=0;
  TRISC=0x80;
  UART1_Init(9600);              // Initialize UART module at 9600 bps
  Delay_ms(100);                 // Wait for UART module to stabilize

  while (1) {                    // Endless loop
   if (UART1_Data_Ready()) {     // If data is received,
     gate_sel = UART1_Read();     //   read the received data,
     delay_ms(500);
     switch (gate_sel)             //Input from PC Hyperterminal o select Gate
     {
     case 0x31: door_open(1);          //Funcion to Open the door1
             delay_ms(5000);
             door_close(1);          //Function to close the door1
             delay_ms(5000);
             break;
     case 0x32: door_open(2);           //Funcion to Open the door1
             delay_ms(5000);
             door_close(2);           //Function to close the door2
             delay_ms(5000);
             break;
     case 0x33: door_open(3);           //Funcion to Open the door3
             delay_ms(5000);
             door_close(3);          //Function to close the door3
             delay_ms(5000);
             break;
     case 0x34: door_open(4);           //Funcion to Open the door4
             delay_ms(5000);
             door_close(4);            //Function to close the door4
             delay_ms(5000);
             break;
     case 0x35: door_open(5);           //Funcion to Open the door5
             delay_ms(5000);
             door_close(5);           //Function to close the door5
             delay_ms(5000);
             break;
     case 0x36: door_open(6);              //Funcion to Open the door6
             delay_ms(5000);
             door_close(6);              //Function to close the door6
             delay_ms(5000);
             break;
     }
    }
  }
}
void door_open(unsigned char sel)
{
 switch (sel)
 {
 case 1: PORTD=0x02;            //Motor 1 Pos-D1 , Neg-D0
         break;
 case 2: PORTD=0x08;            //Motor 2 Pos-D3 , Neg-D2
         break;
 case 3: PORTD=0x20;            //Motor 3 Pos-D5 , Neg-D4
         break;
 case 4: PORTD=0x80;            //Motor 4 Pos-D7 , Neg-D6
         break;
 case 5: PORTC=0x02;            //Motor 5 Pos-C1 , Neg-C0
         break;
 case 6: PORTC=0x08;            //Motor 6 Pos-C3 , Neg-C2
         break;
 }
}
void door_close(unsigned char sel)
{
 switch (sel)
 {
 case 1: PORTD=0x01;            //Motor 1 Pos-D0 , Neg-D1
         break;
 case 2: PORTD=0x04;            //Motor 2 Pos-D2 , Neg-D3
         break;
 case 3: PORTD=0x10;            //Motor 3 Pos-D4 , Neg-D5
         break;
 case 4: PORTD=0x40;            //Motor 4 Pos-D6 , Neg-D7
         break;
 case 5: PORTC=0x01;            //Motor 5 Pos-C0 , Neg-C1
         break;
 case 6: PORTC=0x04;            //Motor 6 Pos-C2 , Neg-C3
         break;
 }
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top