ad space

Programming & Interfacing LED and Switch with AT89S52

 Here are some simple example of interfacing LED and Switch with AT89S52 microcontroller. The program code is for blinking a LED(such program is often called Hello World program for micrcontroller) and also a program code is provided that turns a LED when a switch is pressed. These simple programs can be used to test whether the microcontroller is working or not, that is testing micrcontroller IC and also helpful for testing program(hex files) and hardware programmer. These program can be used also on AT89C51, AT89C52, AT89S51 and similar 8051 based microcontroller.

LED blink with AT89S52

In this we will turn on and off a LED which is connected AT89S52 micrcontroller Port 2 Pin 0(or P2.0 in 8051 convention). The circuit diagram below how to connect the LED and how to connect crystal, capacitors and reset resistor to the micrcontroller. The capacitors on quartz crystal used here is 27pF but one can also used 22pF, 30pF. The power supply is 5V.

LED blink AT89S52

The following is the program code to blink a LED which connected to Port 2 pin 0(P.0) via a 220Ohm resistor.


#include <reg52.h>
#include <stdio.h>

void msdelay(unsigned int);

sbit LED = P2^0;

void main(){
	
	while(1){
		LED = 1;
		msdelay(100);
		LED = 0;
        msdelay(100);
	}
}

void msdelay(unsigned int ms){
  unsigned int i,j;
  for(i=0;i<ms;i++){
    for(j=0;j<1275;j++);
  }
}

The above program code blinks the LED with 100ms delay. The msdelay() function creates this millisecond delay. Note that this delay works with 11.0592MHz quartz crystal. This C program used Keil compiler. Notice how easy to access port pin in the above program as compared to PIC or AVR micrcontroller.

Switch and LED with AT89S52

Here a push button switch is connected to Port 2 Pin 1(P2.1) and the LED is connected to Port 2 Pin 0(P2.0) as before. When the push button is pressed the LED is turned on otherwise it is off. The circuit diagram of interfacing switch and LED with AT89S52 microcontroller is shown below. The push button switch is pulled high using internal pull-up resistor(inside microcontroller) at one end and grounded on the other end. The other connection is explained above.

switch led at89s52

 The program code for AT89S52 to turn on the LED with the push button switch is pressed is below.


#include <reg52.h>
#include <stdio.h>

sbit LED = P2^0;
sbit BTN = P2^1;

void main(){
	unsigned char btnState;
	BTN = 1;	//make button pin an input pin
	
	while(1){
		btnState = BTN;
			if(btnState == 0){
				LED = 1;
			}
			else{
				LED = 0;
			}
	}
}

 Here, the led and push button pins P2.0 and P2.1 are given alias name LED and  BTN respectively. A variable btnState is created to store the state of push button switch. The button pin BTN is made input with internal pull-up resistor. Then in the while loop we read in the state of the button and then turn on or off the LED depending upon the state of the button using if and else statements.

Post a Comment

Previous Post Next Post