Taking Input - key press program  
Second experiment is taking input from outside world and program the device to act according to the input given.

Schematic Diagram -

In case of taking input at any of the pins of controller it is very neccessary to externally or internally pullup the pin. Pull up is basically a resistance of about 10K which is used to pull the input pin in high state when the switch is not pressed.

Example - toggle an LED connected to PORT0_0 when Switch at P1_0 is rpressed and released
Note - Connect a LED at PORT0_0 and a Push to ON switch on P1_0.
Here LED is connected in sinking mode Logic1 -> LED Off
Logic0 -> LED On
#include < at89x52.h> // Header file for AT89S52
void main()
P1_0=1; // initially Input pin must be high

P0_0=1; // initially LED is Off

while(1==1) // Infinite Loop



P0_0=~P0_0; // Toggle LED state
}// End of Code

Debounce -
when we press a mechanical switch, we think that it goes pressed for only once, but when we look the phenomenon in a microscopic way, it get pushed many times, which results in a undetermined state at last and this shown an abnormal behaviour when interfaced with the MCU. So to get rid off this problem concept of debounce is used.

Debounce can be implemented in two ways ->
  • Hardware Debounce - Using a series RC network
  • Software Debounce - Using coding tweak

    We implemented a type of software debounce in the above code...
    while(P1_0!=1);// Waiting for switch to release
    P0_0=~P0_0; // Toggle LED state

    Using statment like while(P1_0!=1); we tell controller to wait until user release the pressed key. In this way controller perform any given task not on key press but on key release. So problem of debounce is solved using a simple software tweal.

    Best viewed at Firefox 1024x768 resolution