SPWM_INT - Multiple Software PWM Question


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1

    Default SPWM_INT - Multiple Software PWM Question

    (I'm starting a new thread as none of the ones I searched which referenced 'SPWM_INT' seemed appropriate)

    I've implemented Darrel's excellent SPWM_INT include file together with ADCIN (reading a trim pot) to adjust the duty cycle for one pin (see my thread here). The problem is that (a) using 8-bit resolution (0-255) makes the LED brightness adjustment a little coarse, and (b) there's a delay in changing the duty cycle no matter how low I adjust the ADC_SAMPLEUS value. I'm willing to live with (b) but I'd like to move to 10-bit resolution to address (a).

    Is it just a matter of changing
    Code:
    DEFINE SPWM_RES 256
    to
    Code:
    DEFINE SPWM_RES 1024
    and then altering
    Code:
    DEFINE ADC_BITS 8
    to
    Code:
    DEFINE ADC_BITS 10
    and
    Code:
    ADCON1.7 = 1
    for right-justified values? Will that give me the finer control of LED brightness that I'm after?

    BTW, I'm using a PIC16F1825 with 16Mhz internal oscillator setting.

    Darrel's page (below) doesn't have the frequency range definitions like his earlier Multi-SPWM page, but I think this won't work with DC motors as the frequency needs to be greater than 16,000 Hz. Is that right?

    DT_INTS-14 (SPWM_INT - Multiple Software PWM)
    http://www.darreltaylor.com/DT_INTS-14/SPWM.html
    Last edited by RossWaddell; - 15th January 2013 at 20:14.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    (I guess I really have 2 issues here: finer LED brightness control and doing PWM for a motor (since I also want to 'fade in/out' the motor RPM by changing the duty cycle). If this should be a separate thread, please let me know)..

    For the motor application, I think I can configure the registers for the duty cycle (CCP1CON.4 & .5, CCPR1L) but I can't wrap my head around the frequency calculation (with 10-bit resolution at FOSC 32Mhz, I think I need PR2 = 255 with a timer prescale of 1 to get 31.25kHz judging from Table 24-5 in the PIC16F1825 datasheet). But what if I want to aim for 16-20kHz?

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    SPWM_INT has a maximum resolution of 8-bits (0-255), as detailed on the page you linked to.

    For using PWM with the CCP module, mister-e's PicMultiCalc is very useful for figuring out what frequencies and dutycycles you can get.
    http://www.picbasic.co.uk/forum/cont....-PICMultiCalc
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    Thank Darrel. I mis-read "PWM resolution is no longer limited to 8-bit (256)" on your web page to mean higher resolutions were possible - I guess you mean lower resolutions are now possible.

    I just grabbed Mr E's MultiCalc, but it doesn't seem to output results for a 1:1 prescaler (which I think would give me 10-bit resolution for 32Mhz & 16kHz). Also, when you say it will give me available duty cycles does that mean I can't just use 0-1023 (10-bit) or 0-255 (8-bit) when incrementing the CCPRIL & CCP1CON.4 & 5 registers?
    Last edited by RossWaddell; - 15th January 2013 at 23:53.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    No, it's not very often that you get a full 0-1023 resolution with hardware PWM.

    And you can't get 16Khz from Timer2 with a 1:1 prescaler, which is why PicMultiCalc didn't show it.
    With a 32Mhz OSC, the number of timer ticks required is ...

    32,000,000 / 4 / 16,000 = 500

    But Timer2 is only an 8-bit counter, so the max is 256 ticks till it overflows.
    The next available prescaler is 1:4. So divide 500 / 4 = 125
    Timer2 overflows at PR2 + 1, so subtract 1 to make it 124. This is the PR2 value for 16Khz.

    The maximum dutycycle value is (PR2 + 1) * 4 because of the CCPxCON.4,5 bits.
    (124 + 1) * 4 = 500
    So 500 is the "Always On" point (100%), and anything above that is 100% too.

    If your control value is 0-1023, it must be scaled to 0-500.

    500 is slightly less than a 9-bit value, which is 1-bit better than the 8-bit resolution of SPWM_INT.

    At 32kHz, you can get 0-1000 dutycycle, because a 1:1 Timer2 prescaler will give 32Khz when PR2 = 249.
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    If I switch to 16Mhz OSC I can get 10-bit resolution at 16kHz:

    Name:  Mr_E_PWM_Calc.PNG
Views: 1200
Size:  15.2 KB

    So here's the thing: if I set up my ADCIN to use 10-bit then presumably I can get 0-1023 values from my trim pot but since 1000 is the max duty cycle anything above that changes nothing. Will it cause a problem if I pass that in to the duty cycle registers (e.g. 1012) or do I need to set a programmatic limit at 1000? Same applies to 8-bit - max duty cycle is 250 but 8-bit resolution on ADCIN would give me 0-255, so anything greater than 250 does nothing.

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    Quote Originally Posted by Darrel Taylor View Post
    The maximum dutycycle value is (PR2 + 1) * 4 because of the CCPxCON.4,5 bits.
    (124 + 1) * 4 = 500
    So 500 is the "Always On" point (100%), and anything above that is 100% too.
    Is that calculation dependent somehow on the prescaler? I'd like to programmatically determine the max duty cycle available to me with PR2=249 and prescaler of 1:1 (16Mhz OSC, 16,000Hz freq). With Mister E's MultiCalc it shows me '1000' is the max duty cycle for these paramters, which also works with (PR2 + 1)*4 where PR2 = 249 but is that always the rule?

Similar Threads

  1. Darrel's Multiple Software PWM
    By passion1 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 15th October 2013, 15:21
  2. Replies: 1
    Last Post: - 4th March 2013, 21:57
  3. DT Multiple Software PWM and 18F2321 issue
    By microuser in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th December 2012, 08:23
  4. General Question About Multiple Interrupts
    By grzeh666 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 31st August 2008, 17:09
  5. Multiple HPWM question
    By Johan in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th October 2007, 13:00

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