Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

INT_TO_WORD function not working

Wiresplus
2024-01-26
2024-01-26
  • Wiresplus - 2024-01-26

    Hello,

    I need to take an INT input, and convert it to a WORD value.

    VAR
    DRV2Speed : WORD; //IO card takes a WORD where 16000 = full 10V output (equivalent to 50Hz)
    PumpFillSpeed : INT; //Users enter a value between 0 and 50
    END_VAR

    IF <conditions> THEN
    DRV2Speed :=INT_TO_WORD((PumpFillSpeed/50)*16000);
    END_IF </conditions>

    According to me this should work - but it stays at 0, no matter what.

    What am I doing wrong?

     
    πŸ‘
    1

    Last edit: Wiresplus 2024-01-26
  • ojz0r - 2024-01-26

    That calculation will not work the way you expect it to. If PumpFillSpeed is limitied to 0-50 then the only whole number would be 50/50 = 1 everything else is 0.0-0.9999... and since you are using word and integer only 50 would work since there is no representation for mantissa in integer values. Declare a temporary real variable and do the calculation and then do a REAL_TO_WORD in the DRV2Speed assignment.

     
  • Wiresplus - 2024-01-26

    Thankyou. worked fine.

     

    Last edit: Wiresplus 2024-01-26

Log in to post a comment.