It's ok skimask, I understand what you're trying to say. I'm just new in the world of PIC and know nothing about it.Now I need to deal with PIC to finish my project. So, I just do reverse Engineering..study the codes given and learn how it function.
All right I've seen this code a bit now and I have to admit I don't know WHY it works. I do know that may or may not work if you change the port (that's informative I know) which might be related to some weak pull up issue (?) or some other circuit design point I missed.
Here's the question - This code sets all values of the portD to zero
' Wait for keypress
For col = 0 to 3 ' 4 columns in keypad
PORTD = 0 ' All output pins low
TRISD = (dcd col) ^ $ff ' Set one column pin to output
row = PORTD >> 4 ' Read row
If row != $f Then gotkey ' If any keydown, exit
then changes the direction of one pin/column at a time to output instead of input. Great. The question is: why by simply changing the pin to output does the value of that pin go from low (portd=0) to high?
The output state of the pin is stored, even when it is an input. When you make it an input, it automatically goes wherever the input is pulling it. As soon as you make it an output, the old output value is restored.
The way to ensure it goes to the desired state is to set the value prior to TRISing it to an output.
Ah gotcha. So the column is switched to output, then the rows (which are 4 bits over) are examined to see if that row is linked to the column. Thanks - I don't know how I missed it now that I see it. This should help with a keypad with a different number of buttons I've been working on.
So I remember now why I was having trouble following this. The value of the D port is 0. When set to output it draws the input port low. I didn't realize you had to have pull up resistors in the keypad circuit to keep the input port high whenever not being pulled low. The original circuit using this code that I saw had weak pull up resistors enabled and no external resistors, but the port I was using did not have weak pull up resistors associated with it.
Bookmarks