0

I am using Connected Components Workbench V12 with a Micro850 PLC

I am trying to take an input from a barcode scanner and convert the scanned integer input to a 8-bit binary number to use each bit as a boolean to trigger outputs on the PLC.

I am expecting the FOR loop to get the remainder for the input/2 and repeat for the remaining bits. for example I input 21 and expect a 0,0,0,1,0,1,0,1 but only get a 1 on the 8th bit

Here's a Screenshot of the Variable Monitor

out_Complete := in_Enable;
IF in_Enable THEN
    B[8]:= ANY_TO_DINT(in_Integer_Input);   //convert input to DINT
    Ba[8]:= ANY_TO_DINT(in_Integer_Input);
    
    FOR X := 1 TO 8 BY 1 DO
        B[X]:= MOD(Ba[X],2);                //get remainder for [x] bit
        Ba[X]:= B[X] / 2;                   //divide by 2
        OutputBit[X]:= ANY_TO_BOOL(B[X]);   //set output bit 
    END_FOR;
END_IF;

1 Answers1

2

Ba[X] is always zero, except for index 8, because that is the one you set.

Another way to check the bits would be try bit access. Or bit shifting combined with AND, something like this

FOR X := 0 TO 7 BY 1 DO
   B[X]:= SHR(in_Integer_Input, X) AND 1;
END_FOR;
Roald
  • 2,459
  • 16
  • 43