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

Status
Not open for further replies.

#### davewhite

##### Newbie level 3
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

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);
}

}

}

#### kripacharya

##### Banned
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.

davewhite

### davewhite

Points: 2

#### davewhite

##### Newbie level 3
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.

#### kripacharya

##### Banned
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:
davewhite

### davewhite

Points: 2

#### anzilkhan

##### Junior Member level 1
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);
}

davewhite

### davewhite

Points: 2

#### kripacharya

##### Banned
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

#### davewhite

##### Newbie level 3
Thanks for the help!

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

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;

}

}

}

#### kripacharya

##### Banned
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 !!

#### anzilkhan

##### Junior Member level 1
Hi Kripacharya,

When does the motor run? N3 and N4 need to be low?

#### kripacharya

##### Banned
Hi Kripacharya,

When does the motor run? N3 and N4 need to be low?

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.

anzilkhan

### anzilkhan

Points: 2

#### davewhite

##### Newbie level 3
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.