PDA

View Full Version : Assigning Two Byte Variables from Switch Settings



John25
- 17th January 2025, 23:00
I am working on a small initial project where I would like to read a six-position dip switch on RA0 to RA5.

My intention is to assign RA0-RA3 to BYTE variable SW4 and RA4-RA5 to variable SW2. I have been able to load all six bits RA0-RA5 into SW4 using:

SW4 = PORTA

Is there a way to only load bits RA0-RA3 into SW4, or is the best way to mast out bits 4 and 5 after loading the variable? And doing the same when I load the last two bits into SW2 and just mask out bits 0-3?

I thought I had seen a variation of setting variable to only include certain bits, but unable to find that reference now.

Thoughts?
Thanks..

John25
- 18th January 2025, 02:49
After doing some reading, I found the Logical Operator function will do exactly what I need.

After grabbing the data from PORTA, I

resulta = PORTA & %00001111' Returns 8-bit binary result for right four bits
resultb = PORTA & %00110000 ' Returns 8-bit binary result for bits 4 and 5

Just took a bit of reading, but have it working now.

John