Keypad scan

I cant detect anything with the following code (8051 and the keypad i connected to the port 0)

#define K0_mask 0xED #define K1_mask 0x7E #define K2_mask 0x7D #define K3_mask 0x7B #define K4_mask 0xBE #define K5_mask 0xBD #define K6_mask 0xBB #define K7_mask 0xDE #define K8_mask 0xDD #define K9_mask 0xDB .... unsigned char readKeyboard() { //Enter if a key has been pressed if (portState!=0xFF) { //need a 21ms debounce period timerWait25(); //check the key pressed if (portState == K0_mask) { //Display the digit writeToLCD(0,6); //wait for the touch to be released while (portState ==K0_mask); return(0); } ...

DOESNT WORK....why ??? thanks for any help

Reply to
zebulon
Loading thread data ...

I assume portState is defined somewhere as the input port. If it's not, you never read the port. You only display anything at all if you get the right input, so how about displaying (somehow) whatever you do get, e.g. using a serial port, and work on from there?

paul Burke

Reply to
Paul Burke

snip

port 0 needs pullup resistors, do you have them ?

martin

Reply to
martin griffith

sorry i forgot to put this...

//portState points to the port 1 sfr portState=0x90;

yep there are all electronic stuff that should make it running (dont as me for electronics things , i'm just a trainee that should develop a phon software...)

When i press a 0 , i should read 11101101 , i.e 0xED...no ?

Reply to
zebulon

actually it's connected to port 1 .... I always confuse , but anyways , doesnt change anything to m problem...arghh embedded....

Reply to
zebulon

portState is declared volatile, right?

- Ark

Reply to
Ark

Get a scope, voltmeter or logic probe and look at the port pin.

I assume you wrote 0xFF to the port to initialize it. And, that somewhere you defined portState as P1.

Reply to
Neil

Stuff I forgot: as well as the pullups someone else mentioned, you have to set port 0 to 0xff (all high) when you iniyialise the system, as it's a sort of open- collector output.

But seriously, you should be poking round with a scope probe more than asking us, and printing out what the port actually reads. Check for changes, and send out using the UART.

Paul Burke

Reply to
Paul Burke

i changed the whole code , the new one , doesnt work too....i'm fed up wit this...

for(i=0;i

Reply to
zebulon

with

What do you expect this to do? (P1^7)==0 is the same as P1 == 7. For the first three cycles you are pulling down one of these bits so its bound to fail. Four the fourth cycle you'd need to press four keys to zero the top four bits...

[snip similar]

Peter

Reply to
Peter Dickerson

I cant understand what you're trying to tell me... if(P1^7==0) : doesnt it check the value of the 7th pin ??? why should i be like writting 7 to port 1 ????

i was told to put one bit (column) to 0 , everything else to 1 , the check the rows to find a 0 which means thaht a key pressed whose value will be determined both by col and rows... so i dont understant your answer at al ???

Reply to
zebulon

I think you need to spend some time learning the language instead of worrying about hardware. Your code exors the value read from port 1 with the value 7 and checks the result is 0. The only value of port 1 that when exored with 7 gives 0 is 7.

Yes. Output scanning zeros on the bottom four bits and check the top four - assuming a 4x4 matrix keypad.

My answer was that you code was gibberish, You said it didn't work, so I may be right :-)

for (i=0; i

Reply to
Peter Dickerson

I know that i'm not experimented , but i must understand...

where do you see XOR ??

P1 = 0xff & (~(1

Reply to
zebulon

If you quoted things properly it would be much easier. At any rate, you have repeated the error in "if (P1 ^ 7 == 0) {". Why do you want the xor of P1 (whatever that may be) and the constant 7? BTW, there are no prizes given for obfuscating code by eliding spaces.

--
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on 
 Click to see the full signature
Reply to
CBFalconer

'^' is the exclusive-OR operator in C '7' in binary is 0...000111

P1^7 just toggles P1's 3 lowest bits.

Reply to
Roberto Waltman

From the Keil C51 help files... :

sbit name = sfr-name ^ bit-position;

The previously declared SFR (sfr-name) is the base address for the sbit It must be evenly divisible by 8. The bit-position (which must be a numbe from 0-7) follows the carat symbol ('^') and specifies the bit position t access. For example:

.......

btw , this version works :

for(i=0;i

Reply to
zebulon

Correct, but this construct only works when you define an sbit, not in a regular C expression.

sbit mypin = P1^7 /* this is a non-standard C */

defines mypin to be bit 7 of port 1 while

if (P1^7 == 0) /* standard C */

EXORs the contents of P1 with 7

Meindert

Reply to
Meindert Sprang

i should have precised that i was working under Keil C51...sorry

Reply to
zebulon

I doesn't matter. Even with the Keil compiler the 'if ( P1^7==0 )' code does not have the interpretation you give it. sbit is a Keil extension and ^ does not have this meaning elsewhere in C, even Keil C.

Peter

Reply to
Peter Dickerson

does

does

...according to you , the doc is wrong ? please....

Reply to
zebulon

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.