Hi Joe,
It's a lot simpler than it might look at first glance.
Index = (PORTA >> 2) & $0F ' Read DIP switch AND mask result
By shifting the value read in from the 8-bit porta register right by 2, we're just shifting the 4-bits of interest into the lower 4-bits of the Index variable. Next we AND the intermediate result with %00001111 so the Index variable can never be larger than 15.
We do this because the lookup table has 16 entries. 0 to 15.
LOOKUP2 Index,[200,300,400,500,600,700,800,900,1000,2000,3000,400 0,5000,6000,7000,8000],X
If Index = 0 then 200 is placed in X. If Index = 15 then X = 8000.
If you used RA0 to RA3 for the switches you wouldn't need to shift right by 2 since the switches would be connected to the lower 4-bits of the port. You would still want to AND the result with %00001111 however just to make sure the result in Index never exceeded 15 since the lookup table only has 16 entries. 0 to 15.
You could of course use portb or any other port with at least 4 inputs for your switches.
Say you already had the lower 4-bits of portb being used for something else, and you added 4 switches to RB4 to RB7.
You could do something like Index = (PORTB >> 4) & $0F for the same effect we had above with the lookup table by limiting the result to
0 - 15.
If you use RB0 to RB3 for the switch inputs, then Index = PORTB & $0F is all you would need. The & $0F just makes sure Index stays within range by masking out the upper 4-bits.





Bookmarks