Using the ADuC7020/21/22/24/25/26/27/28/29 parts, how do I configure the duty cycle of the PWM?

Using the ADuC7020/21/22/24/25/26/27/28/29 parts, how do I configure the duty cycle of the PWM?

  • 0
    •  Analog Employees 
    on Jul 7, 2010 5:52 PM

    The following simple C file shows how to configure the PWM0H PWM output for a 5.5kHz output frequency.

    The function "SetPwmDutyCycle(unsigned char ucDutyCycle)" sets the duty cycle.

    The PWMCH0 register is loaded with a signed value and this supports duty cycles of less than 50%

    #define PWM_DAT0VALUE           0x1000  /* set PWM freq to 5.5KHz */
    #define PWM_DAT1VALUE           0x15    /* set dead time to 1us */

    #include<aduc7026.h>

    void SetPwmDutyCycle(unsigned char ucDutyCycle);


    int main(void)

    {

    GP4DAT = 0xffff0000;     // CHECKING IRQ FUNCTION
    GP3CON = 0x11111111;   // Enable the PWM outputs to the GPIO
    GP0CON = 0x110;     // Enable the PWM outputs to the GPIO
    GP2CON = 0x11111111;   // Enable the PWM outputs to the GPIO
    POWKEY1 = 0x01;        // Set core frequency to 41.78Mhz
    POWCON = 0x00;
    POWKEY2 = 0xF4;

       // Setup the PWM
       PWMCON = 0xF;        // Ext ASYNC enabled
       PWMDAT0 = 0x1000;      // Period register 182uS
       PWMDAT1 = 0x15;        // 1us Dead time

      
       SetPwmDutyCycle(30);    // Call Function to configure duty cycle
                // Example gives a 30% duty cycle
     
       while (1)
       {
       }

    }
    /*******************************************************************************
    Function: SetPwmDutyCycle
    Description: setup PWMH duty cycle
    Input : ucDutyCycle: PWMH Duty cycle: unit 1%
    The value passed should be between 1-100 representing duty cycle in %
                        
    *******************************************************************************/
    void SetPwmDutyCycle(unsigned char ucDutyCycle)
    {
        PWMCH0 = PWM_DAT1VALUE + (PWM_DAT0VALUE * ((signed int)ucDutyCycle - 50)) / 100;
        return;
    }