Edit: I changed code example to answer your question much better
I never used that chip, but it looks like a 8 bit variant of the 8051, so I would make the following guess: your port are defined as "sfr". Usually your MCU header file contains definitions such as :
sfr P1=0x90;
As per your chip's datasheet on page 5, P1 is at address 0x90. So that means that you could write something like that:
unsigned char scroll(sbit pin);
int main(int argc,char** argv)
{
sbit pinMap[3]={P0^6,P0^7,P1^0}; // P0.6, P0.7 and P1.0.
char keyboardRows[3]; // Keyboard layout is: one row per byte. Column are mapped on 0 to 2.
int i;
while(1)
{
for(i=0;i<3;++i)
{
scroll(pinMap[i]);
}
}
return 0;
}
unsigned char scroll(sbit pin)
{
unsigned char rowState;
pin=1; // or HIGH.
// Save your column state in rowState here.
pin=0; // or LOW?
return rowState;
}
Now, an you didn't say what kind of problem you were having. We need more information to help you out.
Is it software or could it be hardware?
software
What kind of error are you getting?
I'm still not sure other than it doesn't work
It doesn't compile?
It is fine.
It doesn't do what you expect?
I still don't know what you get vs. what you expect.
Is it a custom matrix?
Yes and it works.
Is so, have you used pull up resistors to set a definite state when the buttons are not pressed?
It's ok.
Have to put diode to prevent ghost keys to appear (when pressing many keys at once)?
It's ok.
Edit: Ok, your compiler don't like sbit as parameter, so one may try this:
unsigned char scroll(unsigned char* port,unsigned char pin);
int main(int argc,char** argv)
{
unsigned char* portAddr={&P0,&P1,&P2}; // Put whatever here.
unsigned char pin[3]={0x40,0x80,0x01}; // P0.6, P0.7 and P1.0.
char keyboardRows[3]; // Keyboard layout is: one row per byte. Column are mapped on 0 to 2.
int i;
while(1)
{
for(i=0;i<3;++i)
{
scroll(portAddr[i],pinMap[i]);
}
}
return 0;
}
unsigned char scroll(unsigned char* port,unsigned char pin)
{
unsigned char rowState;
*port|=pin; // Let the bus weak pull-up pull the bus up.
// Save your column state in rowState here.
*port&=~pin; // Drive low the output.
return rowState;
}
If you really insist on using sbit and such things, you could make the declaration global such as this:
sbit RowPins[3]={P0^6,P0^7,P1^0};
unsigned char scroll(int rowIndex);
int main(int argc,char** argv)
{
char keyboardRows[3]; // Keyboard layout is: one row per byte. Column are mapped on 0 to 2.
int i;
while(1)
{
for(i=0;i<3;++i)
{
scroll(i);
}
}
return 0;
}
unsigned char scroll(int rowIndex)
{
RowPins[i]=HIGH;
// Read stuff here and put in "column".
RowPins[i]=LOW;
return column;
}