Thanks Bruce!
your code helps alot, and it looks much simpler then what I've done... thanks again!
Thanks Bruce!
your code helps alot, and it looks much simpler then what I've done... thanks again!
Hi Bruce,
If I get this right . . translates to portA / 4 & %00001111 , what I do not get is what it means, are we doing math here to get a number ??? & = AND. What does it mean, Mask result?Code:Index = (PORTA >> 2) & $0F ' Read DIP switch AND mask result
I am really trying to get my head around how this works, because it works pretty sweet, Thanks
JS
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
The OP show switch connected on RA<5:2>
>>2 : yes it's divide by 4... but also easier to figure out when you use the 'Shift it 2 positions to right' term. Here it will shift the whole PORTA reading, 2 bits to the right... hence remove PORTA<1:0>
the & $0F is indeed a Bitwise AND. This way it keep only bits <3:0>... here PORTA<5:2> once shifted to the right.
Code:76543210 Index= %10101111 Index=Index >>2 ' now %00101011 Index=Index & $0F ' now %00001011
Last edited by mister_e; - 16th January 2008 at 07:25.
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Thank You Mister_e,
I will have to think on this awhile, but I see it throws out the 2 LSB and then throws out the 4 MSB, seems odd, I just have to get my thick head around it. Would it be possible to use say PortB and use only 4 ports in BCD fashion while using the rest as outputs? Something like
Index = PORTB<3:0>?
Thanks
JS
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
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.
Thanks Bruce !
Now I Get It !
Thanks Also To Mister_e
![]()
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Bookmarks