Connect with us

Need lower PWM frequency on Microchip PIC!

Discussion in 'Microcontrollers, Programming and IoT' started by xxJaRxx, Oct 11, 2012.

  1. xxJaRxx

    xxJaRxx

    34
    0
    Oct 16, 2011
    Hi Guys,

    I'm looking for a bit of advice on how to get a lower frequency (higher period) out of my micro.

    With my PIC18LF46K80 I am looking to control an ESC which in turn controls a brushless motor.
    The ESC is controlled via PWM, with min and max duty cycles of 1ms and 2ms respectively that the ESC can take as inputs.

    My issue is; from my calculations my micro, with a 40MHz clock, can offer a minimum of a 2.44kHz PWM frequency and therefore a maximum period of 409.6us.

    My PWM frequency really needs to be about 250Hz with a period of 4000us (4ms).

    Is there anything fancy I can do with post-scalers to get this? Or as a last resort any additional hardware I can use?

    Cheers
    James
     
    Last edited: Oct 11, 2012
  2. rob_croxford

    rob_croxford

    262
    0
    Aug 3, 2010
    You need to select a PIC that has a variety of internal oscillators (many of them do). Then in software you can selet which oscillator to use. For instance the HS oscillator maybe 40Mhz but the LS oscillator could be 32Khz. 32KHz will be low enough for you to attain 250Hz PWM. But this really relies on micro selection. Also lowering the internal oscillator will affect the timing of anything else the microe is doing so it is not very efficient if you are wanting to add extras to your controller as opposed to just generating PWM.

    Another method is to use timers and interupts to generate PWM using software (NOT using the CPP peripheral already on the Micro). There is alot of information on how to do this on the internet just do a google search for PWM software generation.

    Why are u wishing to run the motor at such a low frequency? At this frequency the motor will be quite noisey as it is within the audioable range. When i have run DC motors i have generally used a switching frequency of 16Khz or above.

    Cheers
     
  3. gorgon

    gorgon

    603
    23
    Jun 6, 2011
    It's a bit confusing. A duty cycle of 1ms, or 2ms, does not say anything about the PWM period. A duty cycle is normally in % of a a total period.

    The base input frequency of a PWM generator, together with the resolution of your duty cycle will give you the period and the frequency, or your PWM period. If you want a resolution of 8bits for your regulation(duty cycle), your input frequency needs to be 256x the PWM frequency.

    If you want a regulation pulse between 1 and 2ms, it sounds like a standard RC-servo output, with a repetition period of between 12 and 20ms.

    TOK ;)
     
  4. xxJaRxx

    xxJaRxx

    34
    0
    Oct 16, 2011
    Hi Rob, thanks for the reply. My PIC does have a variable internal oscillator, the lowest frequency available is 31.25kHz. I will set it to this today and see if I can get the ESCs & motors working, but I don't think the clock speed will be quick enough for my project because as you suggested, I will be doing other things as well as controlling motors via PWM.

    Timers and interrupts sound like an interesting method, I will look into it, but due to the fact that I need to individually control 4 ESCs/motors this way, it might be a bit unreliable?

    I will not be running the motor itself at this frequency, due to it being a brushless motor, the ESC will be controlling the motor itself, the PWM signal I am after is just the input to the ESC (which I have been told needs to have a min and max duty cycle of 1 and 2ms).

    Hi Gorgon, thanks for the response! My thought behind having a PWM period of 4ms was that if my min and max duty cycles (or regulation pulses) were 1 and 2ms, I would have greater sensitivity in that range than if my period was 20ms or more. Not sure if it is correct to assume this though!
     
  5. rob_croxford

    rob_croxford

    262
    0
    Aug 3, 2010
    Hi sorry i just brushed over your question and got strait to the PWM generation bit :p

    Well in that case you will actually be better off generating the PWM in software. Will you need four seperate PWM outputs?? Will you duty cycle be variable or will it be constant? Will you require feedback to ensure output follows required output? What topologie are you using?

    If you are planning on doing anything else with this micro (as u mentioned above) i would certainly not be lowering the internal oscillator. Software PWM will be just as effective as using the ccp module as it is using the same timers and oscillators that that uses so it will be no more or no less effective assuming your programming skills are up to scratch.

    There are a variety of methods of generating this signal in software but we will need to know a little more of what else your pic will be doing to guide you. Also what software/languages??
     
  6. rob_croxford

    rob_croxford

    262
    0
    Aug 3, 2010
    Also are u planning on using the generic ESC settings? so 1ms motor off, 1.5ms motor at 50% and 2ms motor at 100%?
     
  7. xxJaRxx

    xxJaRxx

    34
    0
    Oct 16, 2011
    Hi Rob, sounds like you know your stuff!

    Quick background into my project; I am designing and building a Quadcopter UAV from scratch. So, yes I will need four separate PWM outputs, with a duty cycle varying between 1ms and 2ms. I don't think I will need any feedback. Not sure what a topologie is or what I am using ha.

    The micro will also need to take input from an IMU (gyroscope and accelerometer) and then adjust the PWM to the ESCs accordingly to ensure the quadcopter stays level. This bit I think I have sorted though (IMU input & processing).

    I will be using a PIC18LF46K80 (which I have at the moment) and programming in C in MPLAB.

    Yes I am planning on keeping the ESC standard for now as I have never worked with them before!

    Cheers
    James
     
  8. rob_croxford

    rob_croxford

    262
    0
    Aug 3, 2010
    sounds like a nice project.

    By topology i meant how are you driving your motors? Half-bridge/ H-bridge? in other words what directions do you need the motors to go in - forward/reverse/breaking?

    Your feedback will be what you use to adjust the flight of your UAV. So your feedback will be two signals - one for speed (accelerometer) and one for possition (gyro). You will then need to compare this signal to a set point (how fast you want to be traveling/what possition you want to be in) and your controller varys the PWM output to ensure the output meets the input conditions. Look up PID controllers paying most of your attention to the P (proportional) and I (integral) terms.

    My suggestion would be to get a single motor running first to prove the concept and the controller. Unfortunatly you will not be able to use a single PWM output for the motors as each motor will need to be controlled indipendantly to sustaion reasonable flight.

    What may be a better idea (certainly one i would considder) is scrapping the ESC and developing the motor controller yourself. This will give you a better scope of control. To do this you will need to research different motor driving topologies and pick one that will suit your application.
     
  9. rob_croxford

    rob_croxford

    262
    0
    Aug 3, 2010
    P.S. is the quadcopter similar to the avengers idea? ;)
     
  10. xxJaRxx

    xxJaRxx

    34
    0
    Oct 16, 2011
    No half-bridge or H-bridge as I only need the motors to go in one direction. My aim at the moment is just to get the quadcopter up in the air, hovering steady and level, I am not going to start thinking about moving it in any directions yet.

    I know I will need 4 PWM outputs so I can control the ESCs independently. I do not have enough time to develop the ESC myself, I think the ESCs I have will do the job I just need to figure out how to get the PWM frequency right down.

    I managed to get a motor working earlier :) I lowered the Fosc of my micro right down to 31.25kHz and managed to get it working. I will investigate timers and interrupts to see if I can keep my Fosc high and still get the output signal I need.

    Unfortunately it will not be like the quadcopter from The Avengers ha!
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-