PDA

View Full Version : Programing Question



willie
- 20th February 2006, 00:10
Hello All,
I got a MELabs LabX1 and I don't know whay some things are in the Key Scan routine mean.
The code is

getkeyu:
' Wait for all keys up
PORTB = 0 ' All output pins low
TRISB = $f0 ' Bottom 4 pins out, top 4 pins in
If ((PORTB >> 4) != $f) Then getkeyu ' If any keys down, loop

Pause 50 ' Debounce

getkeyp:
' Wait for keypress
For col = 0 to 3 ' 4 columns in keypad
PORTB = 0 ' All output pins low
TRISB = (dcd col) ^ $ff ' Set one column pin to output
row = PORTB >> 4 ' Read row
If row != $f Then gotkey ' If any keydown, exit
Next col

Goto getkeyp ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16
key = (col * 4) + (ncd (row ^ $f))
Return ' Subroutine over

Can someone tell me what that the ! means and the ^ means in the code?

Thank for your help.
Willie

DynamoBen
- 20th February 2006, 01:37
!= means "Not Equal" could use <> instead

^ means "Bitwise Exclusive OR"

4.17.14. Bitwise Operators

Bitwise operators act on each bit of a value in boolean fashion. They can be used to isolate bits or add bits into a value.

B0 = B0 & %00000001 ' Isolate bit 0 of B0
B0 = B0 | %00000001 ' Set bit 0 of B0
B0 = B0 ^ %00000001 ' Reverse state of bit 0 of B0

muddy0409
- 20th February 2006, 04:12
Just reading this reply, DynamoBen, and have come up with a question with the Math Operators.

Using the BITWISE NOT ~ I guess B0 = B0 ~ %11111111 would invert the whole byte? and B0 = B0 ~ % 00001111 would invert the lower nibble? So the 0 means leave that bit alone, and 1 means invert that bit?

Right or wrong???????

DynamoBen
- 20th February 2006, 04:25
When dealing with bitwise Operators:

0 means leave that bit alone
1 means take action based on operator

You have the hang of it basically think of them as filters.

Using your code $F = %00001111