L298N Motor Driver using pwm code not working..


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: L298N Motor Driver using pwm code not working..

    That code looks just wrong.
    You set four outputs on PortD while the schematic shows PortC.
    Then you seems to missing the actual PWM statement, and if you meant to PWM the Enable pins you have those too on the wrong pins.

    I'm surprised if that code even compiles.

    /Henrik.

  2. #2
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: L298N Motor Driver using pwm code not working..

    Hi Henrik,
    My bad, uploaded the wrong image(must be very tired last night)
    [ATTACH=CONFIG]6686[/ATTACH
    Thanks for the replying...I will try it today and post here my output.

    and if you meant to PWM the Enable pins you have those too on the wrong pins.
    ..aren't pin5 and pin6 the PWM pins on JP6?


    /tacbanon
    Attached Images Attached Images  
    Last edited by tacbanon; - 27th September 2012 at 01:45.

  3. #3
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: L298N Motor Driver using pwm code not working..

    Hi, kindly check if this should be the right pins assignment.
    PortD.0 to input3 of l298
    PortD.1 to input4 of l28
    PortC.0 to enable of l298
    by henrik: I'm surprised if that code even compiles.
    Really sorry..next time I will double check before uploading.
    Code:
    PWM PORTD.0, 127,100
    PWM PORTD.1, 127,100
    PortC.0 = 1

    Hope I'm close...

    /tacbanon

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: L298N Motor Driver using pwm code not working..

    Concentrate on one bridge and motor to begin with, when you have that working apply the same procedure to the second bridge/motor.

    Bridge 1 is controlled In1, In2 and ENA - in your case (according to your latest schematic) PortD.0, PortD.1 and PortC.4 respectively.
    To drive the motor forward you drive PortD.0 high, PortD.1 low and apply the PWM signal to PortC.4
    Code:
    PortD.0 = 1
    PortD.1 = 0
    
    Main:
    For i = 0 to 200              ' Ramp up.
       PWM PortC.4, i, 10
    NEXT
    
    For i = 200 to 0 Step -1  ' Ramp down.
       PWM PortC.4, i, 10
    NEXT
    
    Pause 500
    
    Goto Main
    According to the photo of the board there are a jumper for each bridge which says Remove jumper for PWM - did you do that? If you didn't the EN-pins are tied high and will short your PWM signal to the boards Vcc.

    /Henrik.

  5. #5
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: L298N Motor Driver using pwm code not working..

    Thank you Henrik for saving me hours of time just to make it ...I got it finally! I will try to add the other bridge, then post here my result.

    regards,
    tacbanon

  6. #6
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Imrproving motor control in line follower application.

    Hi, I did manage to add the other motor, right now I made as a line tracker robot . Not very well on the curves yet and a little slow..hope you can look onto my codes and might able to improve the performance..
    Name:  LineFollower1.png
Views: 2782
Size:  751.7 KB
    Code:
    DEFINE osc 4         
    ANSEL = %00000111 ' Make AN0-AN7 Analog
    ANSELH= %00000000 ' Make AN8-AN13 digital
    
    TRISA  = %00111111
    ADCON1 = %00001001 
    TRISC = 0
    TRISD = 0
    TRISB = 0
    
    ;-------------Variables
    senCenter var byte
    senRight  var byte
    senLeft   var byte 
    
    i var word
    i = 0
    PORTD.0 = 1 ' In1 
    PORTD.1 = 0 ' In2
    PORTB.0 = 1 ' In3 
    PORTB.1 = 0 ' In4
    
    ; PORTC.4 PWM Signal  
    
    loop1:
       Adcin 0, senCenter
       Adcin 1, senRight
       Adcin 2, senLeft
       ;Serout2 PORTD.2, 84, [$1B,$45,Dec senCenter," ", DEc senRight," ",Dec senLeft]
       ;------------------------Select options seen by the sensors
       IF senCenter > 150 and senRight > 150  then 
           Gosub movExtRight
        Endif
       
       IF senCenter > 150 and senLeft > 150  then 
           Gosub movExtLeft
        Endif
       
        IF senCenter > 150 then 
           ;Serout2 PORTD.2, 84, [$1B,$45]       
           ;Serout2 PORTD.2, 84, [$D,"     Center"]
           gosub movForward
        Endif
        IF senRight > 150 then        
           ;Serout2 PORTD.2, 84, [$1B,$45]
           ;Serout2 PORTD.2, 84, [$D,"Rigth"]
           gosub movRight
        Endif
        IF senLeft > 150 then        
           ;Serout2 PORTD.2, 84, [$1B,$45] 
           ;Serout2 PORTD.2, 84, [$D,"           Left"]
           gosub movLeft
        Endif
       ;------------------------
       
       ;gosub movForward
    
     
    GOTO loop1               ' Do it forever
    
        END
    
    movForward: '  forward
    PORTD.0 = 1 ' In1 
    PORTD.1 = 0 ' In2
    PORTB.0 = 1 ' In3 
    PORTB.1 = 0 ' In4
    PWM PORTC.4 ,200,10 
    PWM PORTC.5 ,200,10
    return
    
    movExtRight: ' turn more to the right
    PWM PORTC.4 ,50,10 
    PWM PORTC.5 ,200,10
    return
    
    movRight: '  short right
    PWM PORTC.4 ,100,10 
    PWM PORTC.5 ,200,10
    return
    
    movLeft: '   short left
    PWM PORTC.4 ,200,10 
    PWM PORTC.5 ,100,10
    return
    
    movExtLeft: ' turn more to the left
    PWM PORTC.4 ,200,10 
    PWM PORTC.5 ,50,10
    return
    
    senLost: ' Getback no line detected
    PORTD.0 = 0 ' In1 
    PORTD.1 = 1 ' In2
    PORTB.0 = 0 ' In3 
    PORTB.1 = 1 ' In4
    PWM PORTC.4 ,200,5 
    PWM PORTC.5 ,200,5
    pause 50
    return
    Thanks in advance,
    tacbanon

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: L298N Motor Driver using pwm code not working..

    Since you're using the 16F887 which has two CCP-modules the first thing I'd do is the rewire the thing so that the EN-pins of the L298N are connected to PortC.1 and PortC.2 and use HPWM instead.
    That way the PWM generation is handled in the background, by the CCP-modules (instead of tying up the processor) and you only need to update it when you want to change the speed. Your program gets a lot of time to do other things.

    Then I'd probably set up a timer interrupt to trip every 25ms or something. In the ISR I'd read the sensors and set the PWM dutycycles accordingly.

Similar Threads

  1. Replies: 2
    Last Post: - 7th August 2012, 16:16
  2. Motor driver problem w/ HPWM
    By Amoque in forum General
    Replies: 2
    Last Post: - 21st May 2011, 20:51
  3. L293 motor driver options?
    By Ron Marcus in forum Off Topic
    Replies: 0
    Last Post: - 5th December 2006, 15:39
  4. Stepping motor Driver
    By mesamune80 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 8th August 2006, 04:36
  5. Driver code for 14 segment LED display
    By Durward Searcy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th December 2004, 20:08

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts