This tutorial is for beginners in the field of micro-controller.The purpose of this tutorial is 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 true, when the switch is pressed. logic false, when the switch in not pressed. A single pole single throw (SPST) switch has two connections. The switches are shown as open circuit 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.
Switch interface circuit |
A switch has an two state open state and closed state. And state of switch is monitor by microcotroller 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 microcontroller is normally in low state(0V logic 0) when we press switch pin become high state (Vcc-5V logic 1).
- In negative logic input pin of microcontroller is normally in high state(Vcc-5V logic 1)when we press switch pin become low state (0V logic 0).
Show in above circuit diagram switch is connected with P2.0 of AT89S52. In normal state P2.0 is pull-up by 10k resistor so normally P2.0 is in high state. When we press switch P2.0 become low and it's monitored by digital input pin of AT89S52 micro-controller.
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
}
}
}
#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
}
}
}
Download Complete Project:
thankss
ReplyDelete