Sunday 23 November 2014

Switch Interfacing

In the previous post we learned how to blink an LED using Atmega16. In this post we will see how we can interface push button with the microcontroller.
Our objective will be to control the glowing of an LED with the help of a push button.The following is the circuit diagram:
Circuit Diagram

In this circuit the push button S1 is connected to PD0 pin of  PORTD and the LED is connected to the PC0 pin of PORTC.

Start by including the required header files and declaring PORTC as output and PORTD as input port.
As you can notice in the code, we have activated the internal pull-up resistance on port D. To know more about activating pull-up resistance, please refer to the following article-
http://avrlogic.blogspot.in/2014/11/pull-up-resistance.html

Now to detect if the switch is pressed or not, we use an if statement.
Just as PORTx command is used to write a logic level to port x, PINx command is used to read the logic level of a port x.
Now, since the switch is connected to PD0, we mask all bits of PORTD, except PD0 and check its logic level. If the logic level is LOW, the control goes within if statement.
The logic levels on PORTC are then inverted. A delay of 40 ms is added to remove debounce.
Hence, on pressing the switch once, the LED will start glowing. On pressing it again, the LED switches off.
The detection of the switch being pressed is placed within an infinite loop since we want this operation to continue indefinitely. This method of checking continuously is known as Pollling.


Therefore, we have successfully interfaced a switch with the Atmega16 microcontroller.



4 comments:

  1. how do we interface 4 LEDs and a switch to control them all on the same PORT?

    ReplyDelete
  2. Nice blog Content.It is very informative and helpful. Please share more content. Thanks.
    Microcontroller Training in Delhi

    ReplyDelete
  3. Data science is the analysis of where data comes from, what it signifies and how it very well may be turned into a valuable and important resource in the creation of business and IT strategies. Data science is a multidisciplinary blend of data inference, algorithm development, and technology so as to solve diagnostically complex problems. It is the field of Big Data which seeks to provide meaningful data from large measures of complex data.
    For More Info: Data Science Course in Gurgaon

    ReplyDelete