This tutorial is for beginners in the field of micro-controller. The purpose of this tutorial is to use of push-button switch with the micro-controller. It will be useful whenever a decision is to be made according to the press of a switch.
Input/output devices are very critical components of an embedded system. The first input device we will study is the switch. It allows the human to input binary information into the microcontroller. Typically we define the Two-state, logic as true when the switch is pressed. logic false, when the switch is not pressed. A single pole single throw (SPST) switch has two connections. The switches are shown as open circuits in Figure 1. which is a normally open switch (NO), the resistance between the connections is infinite (over 1000 MΩ) if the switch is not pressed and zero (under 0.1 Ω) if the switch is pressed.
A switch has a two-state open state and a closed state. And the state of the switch is monitored by the microcontroller at a digital input pin. 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 VCC(5v) is called a "pull-up" resistor because it normally pulls the pin's voltage up to the VCC(5v). A resistor placed between a digital input and the GND(0v) is called a "pull-down" resistor because it normally pulls the pin's voltage to the GND(0v).
In positive logic input pin of the microcontroller is normally in a low state(0V logic 0) when we press switch pin becomes a high state (Vcc-5V logic 1).
In negative logic input pin of the microcontroller is normally in a high state(Vcc-5V logic 1)when we press the switch pin becomes a low state (0V logic 0).
Circuit Diagram
Show in above circuit diagram switch is connected with P2.0 of AT89S52. In a normal state, P2.0 is pull-up by a 10k resistor so normally P2.0 is in the high state. When we press switch P2.0 becomes low and it's monitored by the digital input pin of the AT89S52 microcontroller.
Sample Code
#include <reg51.h>// Include address of register
sbit Switch = P2^0;//Switch is connected to P2.0 of AT89S52
void main()//main function
{
while(1)//Infinite loop
{
if(Switch == 0)//switch is pressed
{
//When Switch is pressed
//type your code here ex. turn on led
}
else
{
//In normal condition switch not pressed
}
}
}
Comments
Post a Comment
if you have any doubt comment me I will try to resolve