Skip to main content

AT89S52 Button Input Tutorial (Keil uVision) — With Debounce

This tutorial explains how to use the on-board push button on the 8051 Development Board with the AT89S52 (8051) microcontroller.

You will learn how to:

  • Read a button connected to P3.7
  • Control the on-board LED connected to P2.0
  • Add debounce for stable button operation

✅ Important Notes​

  • The push button is already connected on the development board.
  • The button jumper JP2 must be connected.
  • The LED jumper JP3 must also be connected if you want to see LED output.

Button Connection Details​

On the SiliconTechnolabs 8051 Development Board:

ComponentPin ConnectionLogic Type
Push ButtonP3.7Active LOW
LEDP2.0Active LOW

Active-LOW Logic Explanation​

Button (P3.7)​

  • P3.7 = 0 → Button Pressed
  • P3.7 = 1 → Button Released

LED (P2.0)​

  • P2.0 = 0 → LED ON
  • P2.0 = 1 → LED OFF

1) Requirements​

Hardware Needed​

Software Needed​

  • Keil uVision (C51 Compiler)
  • ProgISP Programmer tool to flash AT89S52

2) Jumper Settings​

Before starting, ensure these jumpers are connected:

JumperFunction
JP2Button Enable
JP3LED Enable

Steps​

  1. Turn OFF the board power
  2. Locate jumper JP2
  3. Connect JP2 properly
  4. Locate jumper JP3
  5. Connect JP3 properly
  6. Turn ON the board

3) Create a New Project in Keil uVision​

  1. Open Keil uVision

  2. Click:

    Project → New uVision Project...

  3. Create a new folder:

    AT89S52_Button_Tutorial

  4. Save the project file inside that folder

  5. Select the microcontroller:

    Atmel → AT89S52

  6. When Keil asks to add startup code:

    ✅ Click Yes


4) Add a New C Source File​

  1. Right click Source Group 1

  2. Select:

    Add New Item to Group...

  3. Choose:

    C File (.c)

  4. Name the file:

    main.c

  5. Click Add


5) Button Input Code With Debounce​

Why Debounce?​

When a push button is pressed, the signal may rapidly fluctuate between ON/OFF for a few milliseconds.
This is called bouncing.

Debouncing ensures we detect only a valid press.


Complete Code (Button Controls LED)​

  • Press button → LED turns ON
  • Release button → LED turns OFF
  • Debounce delay included
#include <reg52.h>

// On-board LED connected to P2.0 (Active LOW)
sbit LED = P2^0;

// On-board Button connected to P3.7 (Active LOW)
sbit BUTTON = P3^7;

/* Simple delay function (approx for 11.0592 MHz crystal) */
void delay_ms(unsigned int ms)
{
unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 112; j++);
}

void main(void)
{
LED = 1; // LED OFF initially

while(1)
{
/* Check if button is pressed */
if(BUTTON == 0)
{
delay_ms(20); // Debounce delay

/* Confirm button is still pressed */
if(BUTTON == 0)
{
LED = 0; // LED ON
}
}
else
{
LED = 1; // LED OFF
}
}
}