Page 1 of 1

### PIC18F452 and L298

Posted: Sat Oct 09, 2010 5:35 pm
Hi,

I am confused while trying to understand the code you've provided in L298 DC Motor Control: Software.

L298's operating instructions are as follows:

Code: Select all

`             |      Inputs          |    Function------------------------------------|--------------------------Ven = H      | C = H ; D = L        |    Forward             | C = L ; D = H        |    Reverse             | C = D                |    Fast Motor Stop------------------------------------|--------------------------Ven = L      | C = X ; D = X        |    Free Running Motor Stop`

As I understood, in your code,

Code: Select all

`    Delay100TCYx(speed_pos);    PORTBbits.RB1 = 1;-------------------\                                          > Forward mode    PORTBbits.RB2 = 0;-------------------/    Delay100TCYx(speed_neg);    PORTBbits.RB1 = 0;-------------------\                                          > Reverse mode    PORTBbits.RB2 = 1;-------------------/`

What I understood was, this code will make the motor to move forward and reverse within 0.5 seconds.

Regards,
/Lakmal

Posted: Sat Oct 09, 2010 7:21 pm
The two values, speed_pos and speed_neg do not control the direction the motor moves. Speed_Pos and Speed_Neg control how long each state occurs.

These are the 3 basic PWM pulses that you will use to control a motor using an L298:

With a PWM input of 50% on and 50% off the motor is stopped and does not move.
So, when Speed_Pos = Speed_Neg the motor is still.

Changing this to 90% On // 10% Off will turn the motor one direction almost as fast as it can go.

Changing this to 10% On // 90% Off will turn the motor in the other direction almost as fast as it can go.

Again as an example. If Speed_Pos = 25 and Speed_Neg = 25 you will have a duty cycle of 50% on // 50% off because the delays are the same.

If you change it to Speed_Pos = 45 and Speed_Neg = 5, the duty cycle will now be: 90% on // 10% off, this turns the motor full speed forward.

Hope this helps.

Posted: Mon Oct 11, 2010 6:09 am
DC motor speed is controlled by the applied voltage. If the motor is rated 12V, 1000 RPM and if we apply 6V its

RPM would be roughly half. i.e. 500.

By using PWM, we make the motor to "see" the average voltage.

80% duty cycle: 80% ON and 20% OFF. graphically:

Let's take T = 2 seconds

Code: Select all

`12v  ----------------0v                   ----     <--- 1.6 s ----><.4>`

Code: Select all

`Average voltage = 12 * (80/100) = 9.6VRPM             = 1000 * (80/100) = 800 RPM`

i.e. if you apply PWM signal with 80% duty cycle, RPM would be 800.

According to your code, motor would "forward" 1.6 seconds and "reverse" 0.4 seconds. As I understand this is bad. You may,

According to L298 datasheet,

Code: Select all

`C=H, D=L => ForawardC=L, D=H => Reverse`

Therefore, I think you should (to move forward)

Code: Select all

`Set Input3 [of L298] = 1;Set Input4           = 0;Apply the PWM signal to 'Enable' pin`

This will effectively rotate your motor in forward direction and

Code: Select all

`Set Input3 [of L298] = 0;Set Input4           = 1;Apply the PWM signal to 'Enable' pin`

for reverse.

Posted: Mon Oct 11, 2010 8:25 am
I wrote a C code that will operate the motor as:
0->100->0...
At duty_cycle = 0, it will change the direction

Code: Select all

`#include <pic18.h>#include <18f2550.h>#include "delay.h"#pragma config FOSC = INTOSC_EC#pragma config WDT = OFF#pragma config LVP = OFF#define Enable LATB4#define C LATB6#define D LATB7void delay (unsigned int interval){for ( unsigned int i=0; i!=interval; i++ );}void main(){   // internal oscillator set to 8 MHz   IRCF2 = 1; IRCF1 = 1; IRCF0 = 1;      TRISB = 0; PORTB = 0;      //float freq = 2; // in KHz   const unsigned int step = 5;   float duty_cycle = 0;   unsigned int direction = 1;      Enable = 0;     C = 1; D = 0; // forward configuration       while(1){      for (unsigned int i=0; i<200; i++){         if (duty_cycle!=0){            Enable = 1;            delay(duty_cycle); // = 0.5 uS * duty_cycle         }         if (duty_cycle!=100){            Enable = 0;            delay(100-duty_cycle);         }      }        if (direction == 1) {         duty_cycle += step;         if (duty_cycle == 100){            direction = 0;         }      }      if( direction == 0) {         duty_cycle -= step;         if (duty_cycle == 0) {            // forward-reverse control            if (C == 0){C = 1;}else{C = 0;}            if (D == 0){D = 1;}else{D = 0;}            direction = 1;         }      }   }}`

Posted: Mon Oct 11, 2010 9:23 pm
It seems you already have a good feeling for how this works. However I am worried about your last statement....

denujith wrote:

Code: Select all

`Set Input3 [of L298] = 1;Set Input4           = 0;Apply the PWM signal to 'Enable' pin`

This will effectively rotate your motor in forward direction and

Code: Select all

`Set Input3 [of L298] = 0;Set Input4           = 1;Apply the PWM signal to 'Enable' pin`

for reverse.

This will work, however the way I did it was different.

The PWM signal was put on the Input (1) with a Differential of that same PWM signal put on the Input (2). The enable pin was held stable.

Maybe that will clear up the differences in how we're using the L298.

Posted: Tue Oct 12, 2010 3:51 am
Thank you a lot for having a lengthier discussion over this. Anyway, I am trying to understand the way, how you control the Motor.

Thank you again!

### Re: PIC18F452 and L298

Posted: Tue Feb 03, 2015 10:48 am
That's correct. Making sure they add to 50 keeps the proper ratio for setting the PWM frequency. If you look closer in the code, the speed_pos and speed_neg values are put into delay functions. This is what creates the high spots and low spots and for how long in the PWM signal.
____________
Usman