Sunday, 15 July 2018

Interface a Switch with 8051 Microcontroller

Aim:

This tutorial is for beginners in the field of microcontroller. In this case the microcontroller is AT89C51, a reprogrammable derivative of 8051. This purpose of this tutorial is to familiarize with the use of push button switch with the microcontroller.
10pcs-each-x-12-font-b-models-b-font-Push-button-font-b-switch-b-font

Description:


Fig. 1 shows how to interface the switch to microcontroller. A simple switch has an open state and closed state. However, a microcontroller needs to see a definite high or low voltage level at a digital input. A switch requires a pull-up or pull-down resistor to produce a definite high or low voltage when it is open or closed. A resistor placed between a digital input and the supply voltage is called a “pull-up” resistor because it normally pulls the pin’s voltage up to the supply.
Interfacing switch to Microcontroller
Fig. 1 Interfacing switch to Microcontroller
We now want to control the LED by using switches in 8051 Development board. It works by turning ON a LED & then turning it OFF when switch is going to LOW or HIGH

Block Diagram

3 switch

Schematic

interrupt

Code

// ****************************************************
// Project: Interfacing single switch to 8051
// Author: Code Bloges
// Module description: Operate array of LED's
// ****************************************************
#include<reg51.h>
#define ldata P2
sbit LED = P1^0;  // define Port2 pin as an LED
sbit Switch = P3^2;  // define Port1 pin as Switch
void delay(int);
// Init CCT function
void cct_init(void)
{
                P0 = 0x00;    // Make all pins zero
                P1 = 0x00;    // Make all pins zero
                P2 = 0x00;    // Make all pins zero
                P3 = 0x00;    // Make all pins zero
}
void main()
{
                while(1)  // infinite loop
                {
                                if(Switch==0)
                                {
                                                LED = 1;  // toggle LED
                                }
                                else
                                {
                                                LED = 0;               
                                }
                }// end of while loop
} // end of main

Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment