xBoard MINI v2.0
Easy to Use learning and development tool for Atmel AVR family of MCUs.
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 MINI there are 4 push buttons that are connected to PORTC (bit 0,1,2,3). We can configure PC0,PC1,PC2,PC3 as input port and read the physical level on PINs. The push buttons are connected as shown below.
We will use the internal pull up of PORTs to keep them in HIGH state. When any key is pressed it will bring the line to LOW and when it is not pressed the pullups will make it HIGH. The following code checks if the button ENTER is pressed or not. Remember ENTER key is connected to PC2.
if(PINC & (1<<PC2)) { //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 enter key is not pressed the LED blinks
slowly. But if ENTER key is pressed the LED will blink fast. The
jumper JP2 must be shorted to run this program.
#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
DDRB|=(1<<PB1); //Make PB1 as Output (LED is connected to it)
PORTC|=(1<<PC2); //Enable Pullups on PC2 (Push Button ENTER is connected to it)
while(1)
{
//Turn Off the LED
PORTB|=(1<<PB1);
Wait(delay);
//Turn On the LED
PORTB&=(~(1<<PB1));
Wait(delay);
//Check input
if(PINC & (1<<PC2))
{
//Enter Key NOT pressed
delay=15;
}
else
{
//Enter Key pressed
delay=3;
}
}
}
NOTE:
Reference: