Wednesday, July 31, 2019

Use PWM_PERIOD_HZ/PWM_DUTY_FRACTION to generate PWM for TI CC26xx/CC13xx devices.


You can replace the following mainThread function call in pwmled2 example to use PWM_PERIOD_HZ/PWM_DUTY_FRACTION to generate PWM for TI CC26xx/CC13xx devices.



void *mainThread(void *arg0)
{
    /* Period and duty in microseconds */
    uint16_t   pwmPeriod = 1000; //1K Hz PWM
    uint16_t   pwmDutyRatio = 0; //Init PWM Duty Ratio
    uint16_t   pwmDutyRatioInc = 5; //PWM Duty Ratio increase every time
    uint16_t   pwmDutyRatioMax= 50; //Max PWM Duty Ratio

    pwmDutyRatio = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * pwmDutyRatio) / 100);

    /* Sleep time in microseconds */
    uint32_t   time = 50000;
    PWM_Handle pwm1 = NULL;
    PWM_Handle pwm2 = NULL;
    PWM_Params params;

    /* Call driver init functions. */
    PWM_init();

    PWM_Params_init(&params);
    params.dutyUnits = PWM_DUTY_FRACTION;
    params.dutyValue = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * pwmDutyRatio) / 100);
    params.periodUnits =  PWM_PERIOD_HZ;
    params.periodValue = pwmPeriod;
    pwm1 = PWM_open(Board_PWM0, &params);
    if (pwm1 == NULL) {
        /* Board_PWM0 did not open */
        while (1);
    }

    PWM_start(pwm1);

    pwm2 = PWM_open(Board_PWM1, &params);
    if (pwm2 == NULL) {
        /* Board_PWM0 did not open */
        while (1);
    }

    PWM_start(pwm2);

    /* Loop forever incrementing the PWM duty */
    while (1) {
        PWM_setDuty(pwm1, (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * pwmDutyRatio) / 100));

        PWM_setDuty(pwm2, (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * pwmDutyRatio) / 100));

        pwmDutyRatio = (pwmDutyRatio + pwmDutyRatioInc);

        if (pwmDutyRatio == pwmDutyRatioMax || (!pwmDutyRatio)) {
            pwmDutyRatioInc = - pwmDutyRatioInc;
        }

        usleep(time);
    }
}

No comments:

Post a Comment