0

I am trying to blink the yellow and green LEDs on my CC32xx board. To do that I need to edit some code that was presented to me. I need to set duty for PWM1 to 90%, set duty for PWM2 to 10%, set the sleep(1) function so it sleeps for 1 second, set duty for PWM1 to 0%, set duty for PWM2 to 90%, and create my code in a loop.

So far, I have set PWM1 to 90% of the period and PWM2 to 10% of the period. And changed the value of variable time to 1000000 ms, which is 1 second. How do you change value of PWM1 to 0% and PWM2 to 90% of period after the sleep function executes? Here is my code:

/*
 *  ======== pwmled2.c ========
 */
/* For usleep() */
#include <unistd.h>
#include <stddef.h>

/* Driver Header files */
#include <ti/drivers/PWM.h>

/* Driver configuration */
#include "ti_drivers_config.h"

/*
 *  ======== mainThread ========
 *  Task periodically increments the PWM duty for the on board LED.
 */
void *mainThread(void *arg0)
{
    /* Period and duty in microseconds */
    uint16_t   pwmPeriod = 3000;
    uint16_t   duty = 0;
    uint16_t   dutyInc = 100;

    /* Sleep time in microseconds */
    uint32_t   time = 1000000;
    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_US;
    params.dutyValue = 0;
    params.periodUnits = PWM_PERIOD_US;
    params.periodValue = pwmPeriod;
    pwm1 = PWM_open(CONFIG_PWM_0, &params);
    if (pwm1 == NULL) {
        /* CONFIG_PWM_0 did not open */
        while (1);
    }

    PWM_start(pwm1);

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

    PWM_start(pwm2);

    /* Loop forever incrementing the PWM duty */
    while (1) {
        PWM_setDuty(pwm1, 2700);

        PWM_setDuty(pwm2, 300);

        duty = (duty + dutyInc);

        if (duty == pwmPeriod || (!duty)) {
            dutyInc = - dutyInc;
        }

        usleep(time);
    }
}
2
  • To change the duty-cycle to 0, you set the count-control register (CCR) value to the max value of the timer period (e.g. the rollover value). The duty-cycle is the percentage of time that power is being applied. So if your timer has 16-bit precision (max 32767) and you have a 50% duty-cycle (CCR = 16384) the power is off from 0 - 16383 and on from 16384 - 32767. To set the duty-cycle to 0 set CCR = 32767 so there is no on period. Conversely for 100% duty-cycle, set CCR = 0. Commented Jan 12, 2023 at 4:04
  • For you, you seem to have a timer period of pwmPeriod = 3000; so to set a 90% PWM, you would need to set CCR = 300 so the PWM power is on for 90% of the 3000 tick period. The CCR value seems to be your params.dutyValue. Commented Jan 12, 2023 at 4:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.