# key scanning for 89c52

Status
Not open for further replies.

#### garg29

generating millisecond in 89c52

hi friends,
i have designed a unit in which i m measuring temp using ds1820 with 89c52and dispklaying the result on lcd(16 X 2) .
the unit incorporates a menu key for setting max temp value .it uses two additional keys (plus and minus) for setting the

values.

If the temp goes beyond this value a relay is activated .
Now my problem is if sometime the user presses this menu key to set the temp. the microcontroller doesnt detect the key press

cause it is busy reading ds1820 which includes some small delay routines also as required by ds1820.

how can i improve key scanning i.e.. how can i make the system or microcontroller respond to any of its key as soon as it is

pressed.it should not miss the key scanning.

----- coding for plus minus key
------
-----
-----
ret

loop:

write_ds1820:
----
-----
------
also includes delay routines

-----
-----
---- also includes delay routines

display_lcd:
----
---
--
ajmp loop

with best regards
amit

#### techie

Reading DS1820 should not take more than a few milliseconds. Keyscanning can be a lot slower. The tasks that you have described should not be a problem for keypad scanning. You can even scan at 100ms interval and still dont see any perceptable delay in response.

#### tobb

##### Member level 5
key scanning

Hi.. if you use low Crystal for 89C52 and have a slow and long delay for 18S20 then it could slow for keyscan.. and also it is possible to use interrupt for keymatrix.. But for your code I think it should not be a problem. Try check your delay code first or you could also try put

#### IanP

Buttons, keys or keyboards should in most cases activate an interrupt. This should put the other tasks aside for a while, read a key or button, set some flags or bytes, which will be acted upon in the main loop, and go back to where you stopped, and continue..
You will not be loosing anything ..

#### garg29

"IanP " as u said the keys should generate a interrupt but can u please tell me how to generate interrupt for a whole keypad...cause 8051 has two external interrupt we can use only two keys on interrupt.... please solve my problem..

#### IanP

That depends on the cofiguation of your circuit.
If it is kepad-keyboard configuration, you can employ proper key-pad decoder IC, such as MM74C922/923, and these IC have "data available" line which can be connected to the µP interrupt.
If you have several buttons connected to several µP pins you can realize AND function by connectig diodes or multi-input AND (NAND) gates.
There is always a solution..
Regards,
IanP

#### techie

I use keypads in a lot of projects and have almost never used interrupts. They require extra hardware and extra software. Since keyscanning does not require high speeds, you can check them every 50ms or so. The 50ms tick can be generated by the system timer besides doing other timed tasks.

Here is what you do.

Code:
main() {

initializations();

while (1) {
if  (key_data != 0) {
}
update LCD
}
}

//------------- Interrupt routine of Timer 0
timer_int interrupt 1 {   // occuring every 50ms
do other timed things
read  buttons and set key_data to value other than zero
}

Status
Not open for further replies.