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