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:
| Component | Pin Connection | Logic Type |
|---|---|---|
| Push Button | P3.7 | Active LOW |
| LED | P2.0 | Active LOW |
Active-LOW Logic Explanation​
Button (P3.7)​
P3.7 = 0→ Button PressedP3.7 = 1→ Button Released
LED (P2.0)​
P2.0 = 0→ LED ONP2.0 = 1→ LED OFF
1) Requirements​
Hardware Needed​
- ATMEL AVR 8051 USBasp ISP Programmer
- 8051 Development Board
- USB A to B cable
Software Needed​
- Keil uVision (C51 Compiler)
- ProgISP Programmer tool to flash AT89S52
2) Jumper Settings​
Before starting, ensure these jumpers are connected:
| Jumper | Function |
|---|---|
| JP2 | Button Enable |
| JP3 | LED Enable |
Steps​
- Turn OFF the board power
- Locate jumper JP2
- Connect JP2 properly
- Locate jumper JP3
- Connect JP3 properly
- Turn ON the board
3) Create a New Project in Keil uVision​
-
Open Keil uVision
-
Click:
Project → New uVision Project...
-
Create a new folder:
AT89S52_Button_Tutorial -
Save the project file inside that folder
-
Select the microcontroller:
Atmel → AT89S52
-
When Keil asks to add startup code:
✅ Click Yes
4) Add a New C Source File​
-
Right click Source Group 1
-
Select:
Add New Item to Group...
-
Choose:
C File (.c)
-
Name the file:
main.c -
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
}
}
}