Power Control PWM, HELP needed


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    11

    Unhappy Power Control PWM, HELP needed

    I'm falling apart and this post might be my last hope.. I can't find anyway to understand this
    I'm trying to generate a sinewave for a 3-phase inverter and I'm not asking for a written code
    using 18f4431 , i read the datasheet part of Power control pwm
    I also read Bruce's example,yet I can't get what are PTPER,PTMR used for and how are they related to duty cycle ?
    how pdc0l and pdc0h work ?
    for example how can I use a lookup table of these entries "0, 25, 50, 74, 98, 120, 142, 162, 180, 197, 212, 225, 236, 244, 250, 254, 255, 254, 250, 244, 236, 225, 212, 197, 180, 162, 142, 120, 98, 74, 50, 25"
    to generate half sine wave ?
    I apologize for my bad English but I am desperate and need help .
    thanks in advance

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,605


    Did you find this post helpful? Yes | No

    Default Re: Power Control PWM, HELP needed

    Hi,
    PTMR is the actual timer providing the timebase for the 4 PWM modules. You shouldn't need to read or write those register at all. PTPER is the period register, it is what controls the period, or frequency, of the PWM signal. Think of it this way, the timer counts from 0 and up, when it hits the value you write to PTPER it start over. Each time it start over a new cycle begins. The dutycycle is set by writing to the PDCx register for the specific channel.

    There is a relationship between the PTPER and the actual dutycycle. Think of it, if the timer starts over at a value 50 then the maximum dutycycle value will be 50. If the timer starts over at 500 then the maximum dutycycle value is 500 so if you change PTPER you'll change the PWM frequency AND the dutycycle.

    Bruce's example really does cover it quite well. If you can't get it going post your code and we'll take a look.

    /Henrik.

  3. #3
    Join Date
    Jun 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: Power Control PWM, HELP needed

    Hello henrik
    so if I put 0150 in PTPER and pdc0l= 00 , pd0h = 01 timer will count from 0 till it reaches 150 then what happens each time it reaches that value ? and what also happens during counting from 0-50 ?
    does that mean it will send a pulse of 100 duty cycle (out of 255)(39%) to pwm0 each timer reaches 150 ?
    I mean if i want to generate pulses of duty cycles of these entries in the this sequence to pwm0
    0, 25, 50, 74, 98, 120, 142, 162, 180, 197, 212, 225, 236, 244, 250, 254, 255, 254, 250, 244, 236, 225, 212, 197, 180, 162, 142, 120, 98, 74, 50, 25"
    what should I do ? and also what does it mean to make a pulse of 500 duty cycle ? i thought 255 = 100%
    thanks a lot for your help

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


    Did you find this post helpful? Yes | No

    Default Re: Power Control PWM, HELP needed

    Hi,
    OK, lots of questions....Lets take a step back and look at how the PWM module works - basically.
    There's a timer (PTMR), a period register (PTPER) - these are common for all "channels". Then there are a 4 dutycycle registers and comparators.

    The timer starts at 0 and counts up until its value equals that of the period register, it then starts over at 0 and it all repeats. If you load the period register with the value 150 the timer will count from 0 to 150 and then start over at 0, count to 150 and so on. If the timer is driven with a frequency of 1MHz it will count to 150 in 151us so the PWM frequency 6622Hz.

    When the timer starts at 0 the PWM output is set. As the timer counts upwards its value is compared to that of the dutycycle register. When the two are equal the PWM output is reset.

    So, if you load the period register with 150 and the dutycycle register with 50 you'll get a dutycycle of ~33% because the PWM period is divided into 151 steps. If you now change the value of period register to 499 the PWM frequency will change because it now takes 500us (instead of 151) to reach its destination and the dutycycle will change to 10% because there's now 500 steps in the cycle compared to 151 before. (This again asumes a 1MHz frequency being fed to the timer and is only meant to describe how the PWM generation works).

    So, as you probably can see, the absolute value of the dutycyle register at which you'll get 100% dutycycle is equal to that of that of the period register.

    This is basically how it works.

    /Henrik.

  5. #5
    Join Date
    Jun 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: Power Control PWM, HELP needed

    That explained A LOT !
    now it all makes sense
    thanks a lot for your help this couldn't be explained any better

  6. #6
    Join Date
    Jun 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: Power Control PWM, HELP needed

    but shouldn't it be "Fosc/4" if I'm using (1:1) prescale ? (1MHz/4 in our case ) which gives PWM frequency 1655Hz ?

  7. #7
    Join Date
    Jun 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: Power Control PWM, HELP needed

    update:
    Hi again,
    for Lookup2 it didn't compile
    "Expected '['
    Bad expression " but it's ok anyway since I already filled the array manually, but I can't find out how to use interrupt in my case or if there is any other solution for my problem

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,605


    Did you find this post helpful? Yes | No

    Default Re: Power Control PWM, HELP needed

    Hi,
    You are overthinking this. Why would want a lookuptable and then populate an array with the data from the lookuptable?
    EITHER use an array for your data OR use a lookup table - I sugest a lookup table. Use the value from the lookupo table and load it to the dutycycle registers - don't populate an array with it's just wasting RAM. Get it working with something like 16 values, then move on the full 100 or whatever.

    Then use three indexes, one for each phase to retreive the correct dutycycle value for each phase from the lookup table, just as you're doing with the arrays currently.

    I know I've covered all these questions in other threads on the exact same topic. 3-phase power inverters seems to be a popular beginners project for some reason. Do a bit of searching on the forum and you should be able to find example code for 3 phase sine PWM.

    /Henrik.

Similar Threads

  1. Power advice needed
    By bearpawz in forum Schematics
    Replies: 4
    Last Post: - 29th October 2010, 21:17
  2. CCP vs power control PWM
    By luminas in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 4th September 2008, 03:09
  3. Help needed - MOSFET driven by PWM
    By bcd in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 2nd April 2008, 05:02
  4. Servo control / UART / SPI help needed
    By Blackhawk in forum mel PIC BASIC
    Replies: 10
    Last Post: - 10th November 2006, 03:40
  5. Replies: 5
    Last Post: - 23rd February 2005, 17:35

Members who have read this thread : 1

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