50Hz PWM signal with 18F4431

Author
steve50j
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2009/10/03 09:01:07
  • Location: 0
  • Status: offline
2009/10/03 09:15:15 (permalink)
0

50Hz PWM signal with 18F4431

Hi I wanted to know if it is possible to generate a 50 hz PWM signal with a duty cycle of 5-10% using PWM module  of PIC 18f4431(20Mhz oscillator).
I want to drive four Brushless motor ESC's with this PIC but I am struggling to setup the pwm module.

If someone can give me an example code in assembly I would really appreciate it.
#1

14 Replies Related Threads

    tunelabguy
    Super Member
    • Total Posts : 1712
    • Reward points : 0
    • Joined: 2005/04/03 08:30:19
    • Location: Hopkins, MN USA
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/03 09:57:36 (permalink)
    0
    If you are just driving brushless motors, why do you need such a low PWM frequency? Normally people try to use the highest frequency they can to minimize current ripple.


    Robert Scott
    Hopkins, MN
     
    #2
    steve50j
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2009/10/03 09:01:07
    • Location: 0
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/20 13:18:07 (permalink)
    0
    Hi, thanx for your reply. I was a bit busy this month so I didn't work much on my project.
    Yes I know the 50hz is a bit low but i need between 50 and 150hz to drive a ESC for brushless motors. Or at least thats what im told the freq of esc are.

    But at the moment I'm struggeling to even setup my PWM channels.
    any help is welcome.
    #3
    atferrari
    Super Member
    • Total Posts : 1379
    • Reward points : 0
    • Joined: 2004/07/08 13:09:24
    • Location: Buenos Aires - Argentina
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/20 15:08:38 (permalink)
    0

    ORIGINAL: steve50j
    But at the moment I'm struggeling to even setup my PWM channels.
    any help is welcome.


    Any help is quite an ample thing. Could you narrow the problem and show code, duly formatted and commented?

    Have played with PWM of that micro and seems quite simple to get it up and running.

    Agustín Tomás

    In theory, there is no difference between theory and practice. In practice, however, there is.

    http://cablemodem.fibertel.com.ar/atferrari/
    #4
    tunelabguy
    Super Member
    • Total Posts : 1712
    • Reward points : 0
    • Joined: 2005/04/03 08:30:19
    • Location: Hopkins, MN USA
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/21 04:49:15 (permalink)
    0
    ORIGINAL: steve50j
    ... i need between 50 and 150hz to drive a ESC for brushless motors...

    OK, I see what is going on. You are trying to generate the standard radio control signals that normally come from an R/C receiver and go to an R/C servo, or in this case, an Electronic Speed Control.

    These signals are not really PWM signals. The determinative parameter is the pulse width. The frequency is not that important, except that it must be, as you said, between 50 and 150 Hz.

    If you could get the hardware PWM of the PIC to produce this low frequency, then it would work fine for you. But for the usual PIC oscillator speeds, you will find this just isn't possible. The frequency is determined by the period of Timer 2, and Timer 2 just can't be made to cycle that slow unless you use a very slow PIC oscillator.

    One alternative is to use the Output Compare mode of a CCP module (not the PWM mode) to produce the timed pulse of 0.7msec to 1.7msec. You can use the same CCP module to measure out the needed 20 msec. between pulses. This involves writing an interrupt routine to repond to each compare event and take appropriate action.


    Robert Scott
    Hopkins, MN
     
    #5
    steve50j
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2009/10/03 09:01:07
    • Location: 0
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/21 09:34:03 (permalink)
    0
    Hi Robert

    Yes that is exactly what I'm going for. The only problem is that I need to drive four motors simultaneously
    and update their speeds at a very fast rate (It is for a Quad rotor helicopter). So I thought the 18F4431's
    PWM function should work perfectly.
    Also in the datasheet it says that it should be able to generate such a low freq
    but as you said it doesn't seem that simple.

    Is it possible to use the ccp module for such an application ( I've never used it before)
    or do you perhaps have any other suggestions.  

    Thanx for your explanation of the esc its gonna help alot.
    #6
    tunelabguy
    Super Member
    • Total Posts : 1712
    • Reward points : 0
    • Joined: 2005/04/03 08:30:19
    • Location: Hopkins, MN USA
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/21 11:42:12 (permalink)
    0

    ORIGINAL: steve50j
    ... in the datasheet it says that it should be able to generate such a low freq..

    Look at the block diagram of the PWM mode in the datasheet. It says TMR2 is an 8-bit timer that resets when it matches PR2. And it is clocked by Fosc/4 divided by the Timer 2 prescaler, which can be 1, 4, or 16. So the longest Timer 2 period you can get is 256 * 16 * 4 * Tosc. Or, in frequency terms, the slowest Timer 2 frequency you can get is:

    Fosc / 4 / 16 / 256.

    So if your 18F4431 is running with a 20 MHz oscillator, then the slowest Timer 2 reset frequency you can get is 20 MHz / 16384 = 1220.7 Hz (much faster than the 50 Hz you wanted). Now you could reach 50 Hz if you lowered your PIC oscillator frequency to 819 kHz. But I doubt you want all your code to run that slow. So that's it. PWM mode is out.

    Now if you need to create 4 separte channels of R/C output, you could do that in software. Use the CCP compare mode just to generate interrupts at the right time, and then in your interrupt routine you can set or clear the correct output bits. There is a little bit of latency jitter that will result from this approach, but I think it will be small enough not to bother you when generating pulses from 0.7 msec to 1.7 msec. And you can generate these signals sequentially so that you are only dealing with one channel at a time.


    Robert Scott
    Hopkins, MN
     
    #7
    jtemples
    Super Member
    • Total Posts : 11082
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/21 11:57:20 (permalink)
    0

    ORIGINAL: tunelabguy

    the slowest Timer 2 frequency you can get is:

    Fosc / 4 / 16 / 256.

    The 18Fxxx1 parts have the "high performance PWM" which has a dedicated 12-bit timer and a prescaler of up to 64:1, so you can divide Fosc by up to 1 meg.
    #8
    steve50j
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2009/10/03 09:01:07
    • Location: 0
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/21 14:44:07 (permalink)
    0
    Great I'm gonna look into the ccp module and study it a bit. Thanx for the advice!
    #9
    jtemples
    Super Member
    • Total Posts : 11082
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/10/21 14:51:50 (permalink)
    0

    ORIGINAL: steve50j

    Great I'm gonna look into the ccp module and study it a bit.

    The "high performance PWM" is not part of the CCP. The CCP provides the "low performance PWM".
    #10
    muhzd
    Super Member
    • Total Posts : 861
    • Reward points : 0
    • Joined: 2008/05/15 10:43:19
    • Location: Malaysia
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/11/17 11:20:29 (permalink)
    0
    Another thing, you might want to have a look at the pulses under a scope too to confirm your calculations.
    post edited by muhzd - 2009/11/17 11:25:50
    #11
    steve50j
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2009/10/03 09:01:07
    • Location: 0
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/11/30 12:00:23 (permalink)
    0
    Hi. I have decided for now that I'm going to use a 18F4431 at a low clock freq to achieve the 50Hz pwm and
    then update the duty cycle via another chip which will do my fast calculations.
    But my problem remains, I cant get the pwm generators to work. Please have a look at this code and tell me
    what I'm missing or doing wrong.
    Also I'm using oshonsoft to test the pwm. It is for this specific chip and it has a Oscilloscope feature, but I'm not sure if it really works. Has anyone ever tested pwm with oshonsoft or is there another way. (I don't own a real oscilloscope).

    Plz have a look at the attached file.
    Thanx!
    Steve
    #12
    jtemples
    Super Member
    • Total Posts : 11082
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/11/30 12:17:14 (permalink)
    0

    ORIGINAL: steve50j

    Hi. I have decided for now that I'm going to use a 18F4431 at a low clock freq to achieve the 50Hz pwm

    You do not need to use a low clock frequency with the power control PWM. I already pointed this out above.

    But my problem remains, I cant get the pwm generators to work. Please have a look at this code and tell me
    what I'm missing or doing wrong.

    I've not used that PWM, but it looks like your duty cycle is greater than your period, which will result in no PWM activity.

    Also I'm using oshonsoft to test the pwm. It is for this specific chip and it has a Oscilloscope feature, but I'm not sure if it really works. Has anyone ever tested pwm with oshonsoft or is there another way. (I don't own a real oscilloscope).

    I would have more confidence in the MPLAB simulator.
    #13
    steve50j
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2009/10/03 09:01:07
    • Location: 0
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/12/10 12:08:33 (permalink)
    0
    Hi again. Thanx for all your help so far. I finally got the PWM going at 50Hz with a 40MHz crystal, but I've only simulated it so far.

    I have another question. This is my first 18F series that I'm using and I'm really confused about the LAT and PORT
    thing. As far as I understand, you write to a LAT and read a port but when my LATA,1 is high why is PORTA,1 still low.
    If I read PORTA,1 its gonna give me a wrong value when the pin is actually high on LATA,1???

    It's a bit confusing, if someone can please clarify that.

    Thanx a Lot
    Steve
    #14
    MBedder
    Circuit breaker
    • Total Posts : 6699
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: 50Hz PWM signal with 18F4431 2009/12/10 13:19:07 (permalink)
    0
    To get the port reads working properly you have to disable shared analog functions of that port.
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5