I'm trying to set 16 individual port pin outputs, here named DO0~DO15, according to the bits in a 16-bit unsigned int.
My method is to take the 16-bit variable, bitwise-AND it with a successively moving '1' bit, and set port pins according to the result. I believe the result should only return a 1 or a 0, equating to a HIGH or LOW respectively.
The method seems to work for the lower 8 bits, but the upper 8 bits always return zeros. The port lines are fine otherwise. Have I misunderstood something?
Some proof that the upper 8 bits are NOT all zero would be useful.
Indeed; well, I was incrementing the variable by 1 and then calling the function, so you'll have to believe my separately testing that it was reaching 0xFFFF and rolling back over!
I've changed the method now, and the following works perfectly instead:
Yes, and strictly speaking it's only supposed to take a parameter of either LOW (or 0) and HIGH (or 1). Though it does the same as boolean here, with anything non-zero being treated as 1. The problem is that it probably truncates it to 8 bits before doing that.
BTW. Did you know that you can write to the 8 bit ports in parallel if you wish (so only two writes instead of 16).
Confirmed. It definitely truncates it to byte before testing for zero/non zero. For "int x" parameter digitalWrite outputs LOW for x=0 and HIGH for x = 1 ... 255. However it writes LOW for x = 256, 512, 1024 etc.