This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

integer operation result is out of range

Hi,

I'm trying to do some define and encountered warning. How to resolve tis warning?

#define K_TIMER_PERIOD   1000
#define K_PWM_DUTY_10   ((K_TIMER_PERIOD*10)/100)
#define K_PWM_DUTY_90   ((K_TIMER_PERIOD*90)/100)

uint16_t tempPwmDC = 0;

// Initialize Timer
void PWM_Init(void)
{
 
 tempPwmDC = K_PWM_DUTY_99;    <= integer operation result is out of range
 
 gPWMParam.timerPeriod = K_TIMER_PERIOD;
 gPWMParam.dutyCycle = tempPwmDC;


 Timer_A_outputPWM(TIMER_A0_BASE, &gPWMParam);
}

  • Hi Ferlyn,

    The most secure way to assign value of K_PWM_DUTY_99 macro to tempPwmDC variable is to change the type of tempPwmDC from uint16_t to uint32_t.

    An other disturbing thing is that I see definition of K_PWM_DUTY_10 and K_PWM_DUTY_90 but the K_PWM_DUTY_99 is not defined in the listed code example.

    BR
    Tsvetolin Shulev

  • Hi,

    uint32_t tempPwmDC = 0;
    // Initialize Timer
    void PWM_Init(void)
    {

    tempPwmDC = K_PWM_DUTY_10;
    tempPwmDC = ((K_TIMER_PERIOD*90)/100); => integer operation result is out of range, same warning and result is 244 instead of 900??

    I'm sorry, should be K_PWM_DUTY_90, typo error.

    Regards,Ferlyn
  • Ferlyn,

    This behaviour seems strange. Are you included <inttypes.h> or <stdint.h> header files where the uint32_t type is defined?
    If the issue still exists try to cast K_PWM_DUTY_90 macro to uint32_t:
    tempPwmDC = (uint32_t)K_PWM_DUTY_90;

    An other attempt for workaround is to use unsigned int instead of uint32_t.

    BR
    Tsvetolin Shulev