I need some more explaination of what you are trying to do.
What is the range of possible values for temp?
You always send SEROUT 8 times so it appears that what you intend is to send the following 8 sequences of the command
- when temp=%00000001 you want to send SEROUT 8 times with the sequence of commands 150, 151, 151, 151, 151, 151, 151, 151
- when temp=%00000010 you want to send SEROUT 8 times with the sequence of commands 151, 150, 151, 151, 151, 151, 151, 151
- when temp=%00000100 you want to send SEROUT 8 times with the sequence of commands 151, 151, 150, 151, 151, 151, 151, 151
- when temp=%11111111 you want to send SEROUT 8 times with the sequence of commands 150, 150, 150, 150, 150, 150, 150, 150
- when temp=%10101010 you want to send SEROUT 8 times with the sequence of commands 150, 151, 150, 151, 150, 151, 150, 151
Is this correct?
If I understand your intention correcty then my first comment is that your eample code does not do what you intend:
Code:
bittest=1 ; point to bit 0
FOR loopcounter = 0 TO 7 ; Extract bit by bit
bitly = temp & bittest
IF bitly=1 THEN
... lines cut ...
bittest = bittest * 2 ; point to next bit
Your example code does not test bits 0-7. It sets bittest to the values 00000001, 00000010, 00000100, 00001000, 00010000, 00010000, 00100000, 01000000 & 10000000 and this means your test "temp & bittest" can only ever return 1 when bittest =1
- temp & bittest for bittest=00000001 can only be 0 or 1 and will send the sequence of commands 150, 151, 151, 151, 151, 151, 151, 151 for any value of temp that has bit0=1. i.e. for temp=00000001, 00000011, 11111111, 01010101, 0000111, etc
- temp & bittest for bittest=00000010 can only be 0 or 2 and so will always send a sequence of 8 x 151 commands for all values of temp
- temp & bittest for bittest=00000100 can only be 0 or 4 and so will always send a sequence of 8 x 151 commands for all values of temp
- etc
As your code is outputting serial commands using SEROUT you can test it in the PE simulator by replacing your SEROUT commands with SERTXD commands.
Try running this code. I've set temp to a value that has a mixture of 1's & 0's and for any value of temp you test that has bit0 set to 0 it always outputs 8 x 151 commands
Code:
temp=%10101010 ; temp is to be examined bit by bit
bittest=1 ; point to bit 0
FOR loopcounter = 0 TO 7 ; Extract bit by bit
SERTXD (#bittest, ": ")
bitly = temp & bittest
IF bitly=1 THEN
command = 150
ELSE
command = 151
ENDIF
SERTXD (#command, cr, lf)
bittest = bittest * 2 ; point to next bit
NEXT loopcounter
One way to correct this is to divide temp by bittest to move the current bit at position bittest into position 0 amd then always test bit positioin 0, like this:
Code:
READ char,temp ; temp is to be examined bit by bit
bittest=1 ; point to bit 0
FOR loopcounter = 0 TO 7 ; Extract bit by bit
bitly = temp / bittest & %0000001
IF bitly=1 THEN
command = 150
ELSE
command = 151
ENDIF
serout SIGIN,T2400,(command,Xtim,Ytim)
bittest = bittest * 2 ; point to next bit
NEXT loopcounter
Going back to your original question.
You can remove the loopcounter variable by using a DO LOOP and testing the value of bittest.
Note that this do loop will correctly repeat for bit7 after calculatiing bittest*2=1000000 but the final calculation bittest = bittest * 2 will overflow the value of the byte variable and set bittest to 0, so that is we it tests "UNTIl bittest=0".
bitly must be a byte variable for this DO LOOP to work.
Code:
READ char,temp ; temp is to be examined bit by bit
bittest=1 ; point to bit 0
DO ; Extract bit by bit
bitly = temp / bittest & %0000001
IF bitly=1 THEN
command = 150
ELSE
command = 151
ENDIF
serout SIGIN,T2400,(command,Xtim,Ytim)
bittest = bittest * 2 ; point to next bit
LOOP UNTIL bittest =0
You can remove the command variable by putting the SEROUT commands inside the IF, THEN, ELSE code:
A syntax check of this code reports exactly the same size as the DO LOOP code above.
Code:
READ char,temp ; temp is to be examined bit by bit
bittest=1 ; point to bit 0
DO ; Extract bit by bit
bitly = temp / bittest & %0000001
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bittest = bittest * 2 ; point to next bit
LOOP UNTIL bittest = 0
And if you have enough spare program space in the 14M2 then you can remove the bittest variable by "unrolling" the DO LOOP and coding it as 8 x If, THEN, ELSE commands with the bittest value replaced by a constant:
Code:
READ char,temp ; temp is to be examined bit by bit
bitly = temp & %0000001 ; Test bit0
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bitly = temp / 2 & %0000001 ; Test bit1
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bitly = temp / 4 & %0000001 ; Test bit2
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bitly = temp / 8 & %0000001 ; Test bit3
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bitly = temp / 16 & %0000001 ; Test bit4
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bitly = temp / 32 & %0000001 ; Test bit5
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bitly = temp / 64 & %0000001 ; Test bit6
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF
bitly = temp / 128 & %0000001 ; Test bit7
IF bitly=1 THEN
serout SIGIN,T2400,(150,Xtim,Ytim)
ELSE
serout SIGIN,T2400,(151,Xtim,Ytim)
ENDIF