SPWM_INT - Multiple Software PWM Question


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1


    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?

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


    Did you find this post helpful? Yes | No

    Default

    The prescaler has already been factored in when calculating the PR2 value.

    The (PR2+1)*4 formula holds true in all cases for the maximum dutycycle.
    DT

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    Thanks Darrel - I'll add that formula into the next round of changes once I get the basic PWM via registers sorted out.

    Part of this thread was how to set PWM frequency/duty cycle via registers so I can use CCP3 & CCP4 on the PIC16F1825 (CCP1 & CCP2 are ECCP's). I have code to control DC motors via a SN754410 motor driver and while it works OK with pbp's HPWM I have to use CCP1 & CCP2 and changing the duty cycle in a loop (the motors have to spin up to the desired speed slowly) means that the HPWM command is changing more than just the duty cycle. So, wanting to learn how to do this via registers I came up with this simple test code:

    Code:
    DEFINE OSC 16               ; Set oscillator 16Mhz
    
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    #CONFIG
       __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
       __config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF
    #ENDCONFIG
    
    OSCCON   = %01111000        ' 16MHz internal osc
    
    pause 100                   ' As advised by Darrel Taylor for EEPROM issue
    
    ANSELA   = 0                ; Digital only for all PortA pins
    ANSELC   = 0                ; Diginal only for all PortC pins
    
    TRISA = 0
    TRISC = 0
    
    MOTOR_1_DIR    VAR PORTC.0   ' Alias PORTC.0 as "MOTOR_1_DIR"
    MOTOR_1_PWM    VAR PORTA.2   ' Alias PORTA.2 as "MOTOR_1_PWM"
    
    ' ***************************************************************
    ' Set up registers for PWM on CCP3 & 4
    ' ***************************************************************
    
    CCP3CON = %00001100         ; Use CCP3 in PWM mode                             
    T2CON   = %00000101         ; Timer2 on with 1:4 prescaler
    PR2     = 62                ; For 16Mhz OSC the desired output of 15,873Hz is
                                ; achieved with this PR2 value (8-bit resolution
                                ; with 1:4 prescaler)
    
    i        VAR BYTE
    
    DutyVar3 VAR BYTE
     
    ;LOW MOTOR_1_DIR
    ;DutyVar3 = 0
    
    HIGH MOTOR_1_DIR
    DutyVar3 = 250
     
    FOR i = 0 to 250
    ;    DutyVar3 = DutyVar3 + 1
        DutyVar3 = DutyVar3 - 1
    
        CCP3CON.4 = DutyVar3.0
        CCP3CON.5 = DutyVar3.1
        CCPR3L = DutyVar3 >> 2
        
        pause 33
    NEXT i
                                
    Main:
    
        GOTO Main
    For clockwise rotation of the DC motor, I use:

    Code:
    LOW MOTOR_1_DIR
    DutyVar3 = 0
    and inside the loop:

    Code:
    DutyVar3 = DutyVar3 + 1
    This works! The motor spins CW slowly up to 250 (max duty cycle with prescaler of 1:4, according to Mister E's MultiCalc).

    The problem is when I want to spin the motor CCW. For that, I use:

    Code:
    HIGH MOTOR_1_DIR
    DutyVar3 = 250
    and inside the loop:

    Code:
    DutyVar3 = DutyVar3 - 1
    This almost works: the motor spins CCW slowly up to max speed but then when the loop is over (and presumably the motor is spinning its fastest) it just stops. Am I using the wrong register settings when I want to count backwards from the maximum (with the SN754110, if the motor direction pin is HIGH then the motor speed starts at the opposite end of the duty cycle range).

  4. #4
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    172


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    You may need to limit the decrement counter so that it never goes below 0
    Code:
    If DutyVar3 > 0 then
        DutyVar3 = DutyVar3 -1
    else
        DutyVar3 = 0
    endif
    Cheers
    Barry
    VK2XBP

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    Quote Originally Posted by Aussie Barry View Post
    You may need to limit the decrement counter so that it never goes below 0
    Code:
    If DutyVar3 > 0 then
        DutyVar3 = DutyVar3 -1
    else
        DutyVar3 = 0
    endif
    Cheers
    Barry
    VK2XBP
    Ah! You think DutyVar3 is ending with -1? I can hook up my serial LCD and see for sure, but that looks to be what's happening. Thanks Barry!

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: SPWM_INT - Multiple Software PWM Question

    That was it. I should have learned by now to go over my loop code in more detail.

    Any ideas on combining SPWM and blinking LEDs?

Similar Threads

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