manual hardware PWM setup on 16F1824


Closed Thread
Results 1 to 24 of 24
  1. #1

    Default manual hardware PWM setup on 16F1824

    ok, here is what i want...
    OSC = 32MHz
    PWM bits = 8
    PWM frequency: as low as possible but grater than 200Hz
    PWM to use TMR6

    ok, so, most of it i have done and figured out by scouring the datasheet for the last week.

    However, the fun bit is the PWM bits... nowhere does it tell you how to set it to 8 bits not 10...
    How is this done?

    Also, TMR2 or 4 or 6 are capable of a 1:64 prescaler, but the PWM section referes to the maximum prescaler of 1:16.... why can't i use 1:64?

    Also, be aware i'm using all 4 PWMs including the two enhanced mode PWM's in standard mode with default port assignments.

    Also, i can't quite understand the 'formulas' in the datasheet for calculating PWM values.

    Any help appreciated.
    Thanks

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Did you already used PicMultiCalc for PWM on previous PIC before?

    I haven't open the datasheet for this specific one, but I think the PWM formulas shouldn't be that different from others. There's quite a few thread about that in here (try http://www.picbasic.co.uk/forum/show...ht=duty&page=2 ). Fortunately you have other PIC on hand for testing purpose?

    I would start with a single output and then, play with related registers to add some outputs.
    Last edited by mister_e; - 28th January 2011 at 01:10.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    that doesn't help...
    I looked through that thread, first off your website seems to be on strike, so couldn't get that calculator application thing...
    Secondly... i compared the PWM section of the 16F1824 datasheet witht the 18F4550 PWM section of the datasheet. The differences were number of PWM's and clock selection...

    Other than that the sections on duty cycle and resolutions were almost the same, so it's not chip specific...

    How do i make the PWM's 8 bit not 10 bit?

  4. #4
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Here is the location for Steve's Multi-Calc. It is quite helpful.

    http://www.picbasic.co.uk/forum/cont....-PICMultiCalc

    If you wanted 8 bit PWM using the hardware, you could shift your 8 bits to change it to 10, matching the hardware.

    something like this:
    Code:
    DUTY1 var word
     
    DUTY1 = 255                    'put in some 8 bit value
    DUTY1 = DUTY1 <<2          'shift your 8 bits to 10 bits
    CCP1CON.5=DUTY1.1       ' load CCP1 duty value            
     CCP1CON.4=DUTY1.0       '      with Duty1 
    CCPR1L=DUTY1>>2
    Last edited by ScaleRobotics; - 29th January 2011 at 01:53.

  5. #5
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Not a perfect solution

    This is not a perfect solution, but you could just leave the 2 LSB's set to 0, then just use 8 bits for the MSB's. This will allow you to set PWm to zero, but not 100%
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  6. #6


    Did you find this post helpful? Yes | No

    Default

    or... i could just times the 8 bit duty by 4...

    but still, why does the datasheet, even the one for the 18F4550 'indicates' a variable bit resolution...

    TABLE 24-5: EXAMPLE PWM FREQUENCIES AND RESOLUTIONS (FOSC = 32 MHz)
    Code:
    PWM Frequency                1.95 kHz 7.81 kHz 31.25 kHz 125 kHz 250 kHz 333.3 kHz
    Timer Prescale (1, 4, 16)   16          4           1              1          1           1
    PRx Value                         0xFF      0xFF       0xFF         0x3F     0x1F      0x17
    Maximum Resolution (bits) 10         10           10            8          7           6.6
    Thats a copy of the table from the datasheet... as best i can get it over
    So, saying their is maximum resolution that is less than what the physical hardware can do indicates that you can change the resolution...

    So, where do you change it?
    HOW do you then only use 8 bits as indicated?
    I've now read through 3 'guides' but they have not told me what i need to know...
    However...
    One of them indicated that PR was the period register used for setting the time lenght for each cycle...
    However this can't possibly be true since that would mean the chip was built back to front... It would be more logical to set the frequency and then set the number of bits....

    ???????

  7. #7
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Good luck operating it at 6.58 bits

    Name:  10bits.PNG
Views: 1307
Size:  20.4 KB

    That's its maximum resolution at that PWM frequency. It's still a 10 bit pwm.

  8. #8
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    You've never used 6.58 bits? The trick is what to do with the little bit (1.42) left over all the time
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  9. #9
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by cncmachineguy View Post
    You've never used 6.58 bits? The trick is what to do with the little bit (1.42) left over all the time
    That's easy, I just sweep them under the rug.

  10. #10
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    I would try that but my 2.3 kids will eat them
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  11. #11
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default

    but still, why does the datasheet, even the one for the 18F4550 'indicates' a variable bit resolution...
    It's not variable in the sense that you can change it, it changes as function, or relation, between the clock frequency and PWM frequency.

    If you had a 1MHz clock (Fosc/4=1Mhz) and wanted a 1Mhz PWM frequency you'd get two different dutycycles, 0% and 100% -1 bit resolution (not very useful). If you lower the PWM frequency to 100kHz you'd have 10 different dutycycles available. How many bits does the value 10 occupy? Well, you can't fit it in 3 bits and 4 is more than you actually need. Using the formula in the datasheet tells us that it's 3.32 bits. Obviosuly there are no such thing as partial bits and you'd need 4 bits to actually be able to use all 10 dutycycles but writing anything "above" 10 to these four bits will result in a dutycycle of 100%.

    Now, if the PWM module gives you a higher resolution than you want you can obviously "scale up" your low resolution value to fit whatever number of bits of resolution there is, as have been shown earlier.

    /Henrik.

  12. #12


    Did you find this post helpful? Yes | No

    Default

    right...

    So, if TMR6 is setup to feed the PWM's... and 'overflows' @ 80KHz...
    If the PWM's were 8 bit then i'd expect a cycle frequency of 312.5Hz...
    If the PWM's were 10bit then i'd expect a cycle frequency of 78.125Hz...

    Where on earth are these stupid decimals coming from?
    Who on earth made a PWM system so complicated?
    and which idiot approved their design?

    If the hardware is static 10bit... why the hell would the datasheet even suggest any other resolution? I don't even understand how you couldn't use 10bit resolution on hardware PWM...

    I really don't understand how these PWMs work... They are backward and alien to me...

    So, can someone please explain in English how to setup the PWMs (ie how the hell you get a different bit resolution)?

    Thanks
    Last edited by comwarrior; - 30th January 2011 at 21:56.

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Look at the related registers... it's timer based, then look which feature are in Timer... YUP you got it... prescaller OSC/2,4,8,16... hence the decimals.

    Use the PICMulticalc, set a low PWM frequency see the resolution... set it way higher... see the resolution.

    Conclusion?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    comwarrior,
    Henrik and others are trying to help you understand. There is no need to get upset.

    I am asking you to keep an eye on your language. This is a "family" type forum.
    Dave
    Always wear safety glasses while programming.

  15. #15
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    I really don't understand how these PWMs work... They are backward and alien to me...
    So, can someone please explain in English how to setup the PWMs
    I found this helpful, maybe you will as well. It is mostly about the hardware, so it is a good example to understand it better. It was done by a company that makes a C compiler.

    http://www.mikroe.com/eng/chapters/v...ntroller/#c3v7
    Last edited by ScaleRobotics; - 31st January 2011 at 01:13.

  16. #16


    Did you find this post helpful? Yes | No

    Default

    No one is getting my point... I'm experienced with hardware PWM chips... and I've never seen a setup like what pic's seem to have...
    I'm very aware that the pwm cycle frequency comes from a timer... I believe i have demonstrated this knowledge...

    It's one thing to say 'yes this is what will happen'... what i want to know is why?
    WHY would the resolution change with different input frequencies?
    If the hardware is 10bit resolution then it should always be 10 bit regardless of input frequency...

  17. #17
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    You might get more joy about this over at microchip forum. Seems you are looking for a legimate answer to why did you make it this way question.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  18. #18
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by comwarrior View Post
    ....It's one thing to say 'yes this is what will happen'... what i want to know is why?
    WHY would the resolution change with different input frequencies?
    If the hardware is 10bit resolution then it should always be 10 bit regardless of input frequency...
    Henrik answered your questions very succinctly in post #11.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  19. #19


    Did you find this post helpful? Yes | No

    Default

    rmteo,
    No, post 11 doesn't answer the question... it again does not tell me why...

    If i wanted 1 MHz pwm frequency and the pwm hardware is 10 bit then i'd have to give the PWM 'chip' a 1024MHz clock frequency...

    If i give the PWM 'chip' a 1MHz clock and the resolution is 10bit then i would expect the pwm frequency to be (1,000,000/1024) = 976.5625Hz

    Changing the bits based on clock input frequency (i believe your saying) is stupid...
    Thats like building a car that will only turn left when the engine RPM is above 6,000 RPM...

    If this 'variable' bit resolution is because the PWM hardware in the micro-controller can't keep up above certain speeds then it should be specified as rated upto a certain frequency...
    This is how all but one PWM chips that I've previously programmed have worked... the one exception was a royal pain in the ass!

    As it is, the frequency that i want is supported at 10 bits (apparently)... while my duty is 8 bit, it's just a few cpu cycles to times it by 4 and then do the PWM 'update'...

    scalerobotics

    I found this helpful, maybe you will as well. It is mostly about the hardware, so it is a good example to understand it better. It was done by a company that makes a C compiler.

    http://www.mikroe.com/eng/chapters/v...ntroller/#c3v7
    oky, so two lots of PR registers... one for 'pules duration' aka duty... the other for 'pulse period' apparently thats frequency... the PWM output frequency should be defined by the input clock divided by the resolution...

  20. #20
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by comwarrior View Post
    Changing the bits based on clock input frequency (i believe your saying) is stupid...
    Thats like building a car that will only turn left when the engine RPM is above 6,000 RPM...
    Why are you so worried about the resolution it is going to have at that high frequency? What is your application? I thought you didn't need more than 8 bit resolution anyway!!!

  21. #21
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    No where is it stated that the PWM hardware is 10-bit (it doesn't make sense anyway). The duty cycle is set by loading a 10-bit register (comprising CCPxCON5:4 and CCPRxL).
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  22. #22
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    The key is, it produces UP TO 10-bit resolution output.

    15.4 PWM Mode
    In Pulse-Width Modulation (PWM) mode, the CCPx pin
    produces up to a 10-bit resolution PWM output.

  23. #23
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    PWM resolution refers to the duty cycle. Not the PWM frequency. The value you load into PR2 sets the period for the frequency you want.

    Say you want a 125kHz frequency. The period of 125kHz is 1/125,000 = 8uS. Now say you're using the 32MHz internal oscillator.

    With a prescaler of 1 the timer will increment every 125nS or 1/8MHz.

    With the timer incrementing every 125nS, it takes 64 instruction cycles before the timer is = PR2.

    When it's = the value in PR2, the timer is automatically reset, and the PWM output pin is toggled high.

    You can see this in Figure 24-3 in the data sheet.

    The duty cycle is set by loading CCPR1L and CCP1CON bits 5 and 4. CCPR1L is an 8-bit register and the 2-bits in CCP1CON are the 2 least significant bits of the up to 10-bit duty cycle.

    When the timer = the value in CCPR1L, and the lower 2-bits = the 2-bit value in CCP1CON bits 5 and 4, the PWM output pin is forced low.

    Figure 24-4 and note 1 explain how the timer is concatenated to form a 10-bit counter used to compare the timer count to the 10-bit duty cycles registers.

    The higher the frequency, the lower the resolution of the duty cycle, since there's less time in the period for more steps to adjust the duty cycle.

    It looks confusing at first, but if you spend a few minutes reading the data sheet and have a calculator handy to plug-in the values shown in tables 24-5, 6 and 7, to see how they work out, it gets a lot clearer.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  24. #24
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default


    <IMG SRC="http://gobnf.org/i/jg/gwb_approved.gif">
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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