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,604


    Did you find this post helpful? Yes | No

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

    Hi,
    I don't really follow your coding but you have a main loop which constantly (I mean thousands of times per second) keeps setting the dutycycle to some value. When the interrupt fires, decide which way to turn (if any) and set the dutycycle, don't change it the main loop. BTW, does 10Hz really work? Also, in your previous code I got the impression that the sensors were analog (you used ADCIN to read them) but now they are digital?

    /Henrik.

    EDIT: One more thing, you don't seem to reload the timer value in the ISR.
    Since the timer is freerunning and generating an interrupt on overflow, with a prescaler of 8 and a 4MHz x-tal the interrupt frequency of 4000000/4/8/65536=1.9Hz, I think you need to run faster than that.
    Last edited by HenrikOlsson; - 29th September 2012 at 10:51.

  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, thanks you for your comments..I guess I have to rearrange my codes.
    don't change it the main loop.
    Does this mean I have to remove this in the main loop..
    Code:
    ; Start rolling
    HPWM 1 ,Gear[i],10
    HPWM 2 ,Gear[i],10
    and place it in a sub program?

    I did remove the analog reading to digital because I thought it will better...don't you think so?

    Regarding the timer, should this be the value T1CON = %00100001 ?
    I really appreciate for answering my questions..not really all clear to me at this time but I hope I will learn some if not all.
    BTW do you think it is best to have a fix speed instead of changing from time to time?

    regards,
    tacbanon
    Last edited by tacbanon; - 29th September 2012 at 11:27.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

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

    Hi,
    HPWM works in the background, once you tell it to output a certain dutycycle with a certain frequency it does that in hardware. There's no need to continously execute the HPWM commands like your doing in your main loop.

    If you change the prescaler to 1:1 you'll get an interrupt every 65536us or around 15Hz, I'd try that as a starting point. If you need faster interrupt rate than that you need to preload the TMR1 register in the ISR so that it doesn't start from 0. If you load with 55536 there will be 10000 cycles (or 10000us in this case) untill the next interrupt.

    I have no idea if analog or digital sensor is best. I don't even know what sensors you're using....

    If you have two sensors, one on "each side" of the line then I think two speeds for each motor should do. Run both motor at the lower speed to go straight forward, increase the speed of one motor to turn. (Or do the oposite if you want).

  4. #4
    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, thanks...I will try your suggestions regarding the timer/interrupt implementation...but I still don't get what you meant HPWM working on the background...my question probably is how I can change the speed parameter if I needed?

    speedR var byte
    speedL var byte
    speedR = 150
    speedL = 150
    pause 2000
    HPWM 1 ,speedR,10 ' Right Wheel
    HPWM 2 ,speedL,10 ' Left wheel

    Mainloop:

    speedR = 100 ' new value of speed
    speedL = 150 '

    Goto Mainloop

    regards,
    tacbanon

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

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

    Hi,
    To change the speed you to execute the HPWM command. What I'm "objecting" against is this:
    Code:
    Main:
    HPWM.....
    HPWM.....
    Goto Main
    When you do it this your trying to update the PWM generator thousands of time per second. What happens when you update it faster then the frequency it's outputting? I don't know the answer.

    In the ISR, determine what duty cycles to send to each motor and execute the HPWM command there. That way you'll update the speed once each interrupt instead of thousands of times per second.

    /Henrik.

  6. #6
    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..(Solved)

    Hi Henrik...many thanks for the kindly explaination...appreciate it very much...got it now.

    regards,
    tacbanon
    Last edited by tacbanon; - 1st October 2012 at 10:17.

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