www.eXtremeElectronics.co.in

Receiving Input.

xBoard v2.0

Easy to Use learning and development tool for Atmel AVR family of MCUs.

 

<< Return To Help Index

Here I will show you how you can sense the external world. In this tutorial, we will learn how to receive the most basic kind of input i.e. digital input. By digital input I mean that the input we will sense is either HIGH or LOW. In digital electronics a LOW is anything less that half the Vcc. In our case Vcc is 5v so any thing below 2.5v is considered LOW or 0. And any voltage higher that 2.5v is HIGH of 1.

The PORTs of Microcontroller can act as input pins. These can receive digital inputs. For example in xBoard there is 1 push button that is connected to PORTD (bit 2). We can configure PD2 as input port and read the physical level on PINs. The push buttons is connected as shown below.

push button connetion in xboard
Push Button Connection
push button on xboard
Push Button On xBoard

 

We will use the internal pull up of PORTs to keep PD2 in HIGH state. When any key is pressed it will bring the line to LOW and when it is not pressed the pull-ups will make it HIGH. The following code checks if the button ENTER is pressed or not. Remember ENTER key is connected to PC2.

if(PIND & (1<<PD2))
{

		//HIGH i.e. NOT pressed

}

else

{

		//LOW i.e. Pressed

}


Below a complete program is provided which controls the blinking rate of an LED by using a push button. When the key is not pressed the LED blinks slowly. But if BUTTON is pressed the LED will blink fast.

Jumper Settings


/*********************************************************************

                 xBoard(TM) v2.0 Sample Programs

               ------------------------------------


Description : Demonstrate how to use ports in Input Mode.
         : Push Button is connected on PD2 (INPUT MODE)
         : LED is connected on PD7 (OUTPUT MODE)
         : >> When USER Button is NOT pressed LED blinks at 
              Normal Rate
         : >> When USER Button is Pressed LED blinks FAST

Author      : Avinash Gupta 2008
Web         : www.eXtremeElectronics.co.in
                   
**********************************************************************/

#include <avr/io.h>
#include <util/delay.h>

void Wait(uint8_t delay)
{
   uint8_t i;
   for(i=0;i<delay;i++)
   {
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);
   }

}



void main()
{
   uint8_t delay=15; //Delay for blinking

   //Setup PORTs

   DDRD|=(1<<PD7);      //Make PD7 as Output (LED is connected to it)

   PORTD|=(1<<PD2);  //Enable Pullups on PC2 (Push Button is connected to it)

   while(1)
   {
      //Turn Off the LED
      PORTD|=(1<<PD7);

      Wait(delay);

      //Turn On the LED

      PORTD&=(~(1<<PD7));

      Wait(delay);

      //Check input
      if(PIND & (1<<PD2))
      {
         //Enter Key NOT pressed

         delay=15;
      }
      else

      {
         //Enter Key pressed
         delay=3;
      }
   }
}

onboard led of xboard
User LED On xBoard

NOTE:

Reference:

 

<< Return To Help Index