Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! 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.

sathiieesh

Newbie level 5
Joined
Feb 5, 2010
Messages
8
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Chennai
Activity points
1,379
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;
 }
}

Compiler-MikroC
Clock-10MHz


Sathiesh Kumar.V
 

At first glance the code look OK although not very efficiently written.

What happens at the moment and what circuitry are you using to drive the motor? The PIC alone cannot supply enough current and you need drivers capable of sinking and sourcing current in order for the motor to be reversible.

You could test operation by connecting a bi-colour 2-pin LED with a series resistor across the PIC pins, you should get one color for open and the other for close. When that works, you can investigate the motor drive circuit.

Brian.
 

Ya i am using L293d motor driver ic in between PIC and Motor.... but the code is not working.... can i replace switch case statement by some other statement... if so what can i do...
 

As you have only one instruction in each case statement, you can change the port bits in the main block of code, you don't need the 'door_open' and 'door_close' functions at all.

I suspect the real problem is that you reset all the ports each time you try to change one bit. When a byte arrives from the UART, you write a fixed number to the ports so they can only have one bit at a time set. I suspect what you want to do is set and reset bits individually so you should use AND (&) to reset bits and OR (|) to set them. If you do this, more than one motor can operate at a time.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top