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.

Problem with controlling DC motor using L293D and PIC167F887 [Beginner]

Status
Not open for further replies.

davewhite

Newbie level 3
Joined
Oct 2, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,313
Hi. I am a beginner in C programming and I am trying to make this project, where an Input of 111 would make M2 spin, while an input of 000 would turn M1.

I've made a simple circuit and a program. My problem is no matter what the input is, only M1 would rotate. I've been trying to figure this one out for hours now but I just can't make it work. Any tips or suggestion would really help. Thanks

Untitled.png

Here are the codes. I try to use both switch statement and if statement.

Code:
 void InitMain() {
TRISD = 0x00;
TRISB = 0xff;


Pwm1_Init(5000);
Pwm2_Init(5000);

}
int i= 0;
void main() {
InitMain();
Pwm1_Start();
Pwm2_Start();

  while (1) {
            //white = 1 black =0
  i = portb;
  switch(i)
  {
      case 0x00:PORTD =0xA;
        Pwm1_Set_Duty(200);
        Pwm2_Set_Duty(0);

         break;

      case 0x07:PORTD=0x0A;
        Pwm1_set_Duty(0);
        Pwm2_Set_Duty(200);
          
         break;
          
          }

    }


}



Code:
void InitMain() {
TRISD = 0x00;
TRISB = 0xFF;

Pwm1_Init(5000);
Pwm2_Init(5000);
    
}

void main() {
InitMain();
Pwm1_Start();
Pwm2_Start();
  while (1) {
            //white = 1 black =0

   
    if(PORTB.B0==0&&PORTB.B1==0&&PORTB.B2==0){
       
        portd=0x0A;
        Pwm1_Set_Duty(200);      
        Pwm2_Set_Duty(0);


        }
     else if(PORTB.B0==0&&PORTB.B1==1&&PORTB.B2==1) {          
        
        portd=0x0A;
        Pwm1_set_Duty(0);      
        Pwm2_Set_Duty(200);     
             }

     }



}
 

its not clear exactly what Pwmx_set-Duty() does. Presumably it toggles RC1 or RC2 for PWM output ? Show this code if you can - your problem is likely here.

But even so, a few points/ questions -

your case statement and your If() statements expect different values to be input for the two cases. 000 / 111 for case, and 000 / 110 for If.
If you use the switch case method, i suggest you also mask out the 3 usable bits, in case you get random values on the other open pins.
 
Good day Sir. Thanks for answering

its not clear exactly what Pwmx_set-Duty() does. Presumably it toggles RC1 or RC2 for PWM output ? Show this code if you can - your problem is likely here.


As what I have read from the mikroc manual, it sets PWM duty ratio and takes values from 0 to 255. So I assumed that it sets the speed of the dc motor?.


But even so, a few points/ questions -

your case statement and your If() statements expect different values to be input for the two cases. 000 / 111 for case, and 000 / 110 for If.
If you use the switch case method, i suggest you also mask out the 3 usable bits, in case you get random values on the other open pins.

If I am understanding your question right, that difference is from me trying to get a different output from the dc motor. My problem is, from those two statements (if & switch), no matter what changes I do in the logic gates the dc motor would not respond. It just sticks to the one action. Any changes in logic gates does not work. I've tried 111=motor on while with 000=motor off. But it just keeps on rotating.
 

I repeat ....

its not clear exactly what Pwmx_set-Duty() does. Presumably it toggles RC1 or RC2 for PWM output ? Show this code if you can - your problem is likely here.

Its is RC1 and RC2 which enable or disable the L293 bridges, and not your code, where PORTD is always 1010 for the input pins of L293.

Unless you check inside Pwmx_set-duty() routines, you will get nowhere. If this routine is not toggling RC1 and RC2, then your system will not work

Update: ok, i read the manual myself, and the PWM_ routines are supposed to toggle RC1 and RC2. So try also setting PORTC as output ,,, TRISC = 0
 
Last edited:
Some simple steps for you to debug.

1. make a single input instead of your three inputs, which will make the code really simple.
2.Set direction for port c.
3.Always enable both the motors(i didn't go through L293, so i don't know what logic input is need to be connected there).


if(PORTB.B0==True)
{

portd=0x0A;
Pwm1_Set_Duty(200);
Pwm2_Set_Duty(0);


}
else
{

portd=0x18;
Pwm1_set_Duty(0);
Pwm2_Set_Duty(200);
}
 
1. make a single input instead of your three inputs, which will make the code really simple.

excellent suggestion

2.Set direction for port c.
as mentioned by myself also. This is probably all you have to do to fix it

3.Always enable both the motors(i didn't go through L293, so i don't know what logic input is need to be connected there).

...... portd=0x18;

do not do this 0x18. It will disable both motors
 

Thanks for the help!

Seems like adding ANSELH=0; solved the problem. Declaring PortC as an output has no effect whatsoever.

UntitledGG.png
Code:
void  InitMain() {
TRISD = 0x00;
TRISB = 0xff;
TRISC=0;
Pwm1_Init(5000);
Pwm2_Init(5000);

}
int i=0;

void main() {
ANSELH = 0;

InitMain();
Pwm1_Start();
Pwm2_Start();

  while (1) {
i = portb;
  switch(i)
  {
      case 0x01:PORTD =0xA;
      portc=0x04;
        Pwm1_Set_Duty(200);
        Pwm2_Set_Duty(0);

        break;

      case 0x00:PORTD=0x0A;
      portc=0x02;
        Pwm1_set_Duty(0);
        Pwm2_Set_Duty(200);

         break;

          }

    }


}
 

Seems like adding ANSELH=0; solved the problem. Declaring PortC as an output has no effect whatsoever.

View attachment 85504
Code:
void  InitMain() {
TRISD = 0x00;
TRISB = 0xff;
TRISC=0;
Pwm1_Init(5000);
Pwm2_Init(5000);

}
int i=0;

void main() {
ANSELH = 0;

InitMain();
Pwm1_Start();
Pwm2_Start();

  while (1) {
i = portb;
  switch(i)
  {
      case 0x01:PORTD =0xA;
  [COLOR="#FF0000"]    portc=0x04;[/COLOR]
        Pwm1_Set_Duty(200);
        Pwm2_Set_Duty(0);

        break;

      case 0x00:PORTD=0x0A;
[COLOR="#FF0000"]      portc=0x02;[/COLOR]
        Pwm1_set_Duty(0);
        Pwm2_Set_Duty(200);

         break;

          }

    }


}

That just doesn't make sense. Though i notice you introduced 2 new lines in your code for RC1 and RC2 .. LoL !!
 

Hi Kripacharya,

When does the motor run? N3 and N4 need to be low?
What about EN1 and EN2?

Thanks in Advance
 

Hi Kripacharya,

When does the motor run? N3 and N4 need to be low?
What about EN1 and EN2?

Thanks in Advance

In1 & In2 are associated with EN1 (ENable pin, active high)
In3 & In4 are associated with EN2
Outputs are corresponding number to input.

In this case, In1 and In2 are set Low and High permanently. So whenever EN1 goes high, the full voltage is applied to motor, and when low then motor is in stop mode.

Now if the ENx pin is driven using a PWM signal, then the speed of the motor can be controlled. Thats it.
 
That just doesn't make sense. Though i notice you introduced 2 new lines in your code for RC1 and RC2 .. LoL !!

Even after removing those 2 lines, it still works fine. I have no idea why.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top