As a contribution to the DIY remote control, I am playing with a 4x4 keypad by ACT (purchased awhile ago from Jamco). I have the 4 pins of the Columns(or Rows) connected to P0-p3 on the ABB and the other 4 pins of the Rows(or Columns) plugged into P4-p7 on the ABB. Should be a piece of cake. I thought at first I could simply get away with code that looked like:
DirA = 0xf ' Sets the 4 pins P0-p3 as output pins
DirB = 0x0 ' Sets the 4 pins p4-p7 as input pins
...
CheckKeypad:
fKeypadChanged = 0 ' assume nothing changed.
_CKPAgain:
wKeypadButtonsLast = wKeypadButtons ' save the old value
OutA = 0x1
wKeypadButtons.nib0 = inB
OutA = 0x2
wKeypadButtons.nib1 = inB
OutA = 0x4
wKeypadButtons.nib2 = inB
OutA = 0x8
wKeypadButtons.nib3 = inB
if wKeypadButtons <> wkeypadButtonsLast then
fKeypadChanged = 1 ' ok something changed
' quick and dirty try to handle debounce by pausing 1 and recalcing again until
' we get the same value.
pause 10
goto _CKPAgain
endif
' Ok we are here with a stable keypad state. If it changed we need to calculate a new PWM value
' to return.
if fKeypadChanged then
' We will generate a pulsewidth between 1100 and 1900 that will translate to zero or one button
' pressed. Could easily be sped up with assembly...
if wKeypadButtons = 0 then
wKeypadPWM = 1100
else
wKeypadPWM = 1150
wKeypadMask = 0x1
while ((wKeypadButtons & wKeypadMask) = 0)
wKeypadPWM = wKeypadPWM + 50
wKeypadMask = wKeypadMask << 1 ' shift the mask down
wend
endif
endif
return
So far this approach has not worked. I did find one problem on my side. I am using an ABB and still had the jumpers in for the PS2, buttons, leds… Removed them. Still does not give me the results I am looking for.
The code is trying to set one bit high in P0-P3 and then check the 4 bits in P4-P7, where hopefully only 1 button will be high iff a button is pressed. So far that has not worked out that way. I was also wondering about P7 on the ABB as there appears to be more circuitry there including a resistor and transistor…
I have tried setting the IO registers directly (PMR5, PCR5 and PDR5 and that has not worked yet. Also I set individual bits of PCR5 to be the correct bits for Input or Output, but when I check PCR5 it always shows a value of 0xff, but that may be normal as it shows all of the pins as W and not R/W… Will play around more later, but now the heating people are here… May try moving wires to other pins…
Kurt