Taking Input - key press program  
Any embedded system will be of no use if it cant interact with the outside world. In this second experiment you will learn to know; how to make your microcontroller to respond its outside world???

Schematic Diagram -



In case of taking input at any of the pins of controller it is very neccessary to externally or internally pullup the pin. Pull up is basically a resistance of about 10K which is used to pull the input pin in high state when the switch is not pressed. Here we are using AVR microcontroller which has internel pull-ups so no need to use external one.

In order to read from a PIN of AVR MC, it must be programmed as input. While a pin of AVR MC is in input configuration it does not drive the port pin and the port pin is said to be floating. A floating pin can be taken to any voltage level by even a weak drive or EMI. So our input pin must have some initial fixed logic state which is achived using pull up resistor, which is inbuilt in AVR MC.

To enable the internal Pull Ups of an AVR while using it as input; configure that pin (input pin(n)) as input (DDRx(n)=0) and write a logic '1' on that pin initially.


/*****************************************************************************************
Example - toggle an LED connected to PORTA_3 when Switch at PORTD_0 is pressed and released
Note - Connect a LED at PORTA_3 and a Push to ON switch on PORTD_0.
Here LED is connected in sourcing mode Logic1 -> LED ON
Logic0 -> LED Off
******************************************************************************************/
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{

	DDRD=0xFE;   	// PORTD_0 declared as input
	DDRA=0xFF; 	// PORTA is declared as output
	PORTA=000; 	// initially LED off
	PORTD=0xFF;  	// Internal pullup is enabled

	while(1==1) 	// infinite loop
	{
		if(!(PIND & 001))  		// check whether the switch is pressed or not
		{
			while(!(PIND & 001)); 	//wait for debounce
			PORTA=(1<<3);    	// LED on
			_delay_ms(500);  	// On for a half second
			PORTA=000;    		// LED turns off
		}  	// if ends here
	}  		// while(1 ) ends here

return 0;

} // main ends here

Debounce -
when we press a mechanical switch, we think that it goes pressed for only once, but when we look the phenomenon in a microscopic way, it get pushed many times, which results in a undetermined state at last and this shown an abnormal behaviour when interfaced with the MCU. So to get rid off this problem concept of debounce is used.



Debounce can be implemented in two ways ->
  • Hardware Debounce - Using a series RC network
  • Software Debounce - Using coding tweak


     We tried to implement a type of software debounce in the above code...
    if(!(PIND & 001)) // Check whether PORTD_0==0 or not? { while(!(PIND & 001));// Waiting for switch to release }
    Using statment like while(!(PIND & 0x01)); we tell controller to wait until user release the pressed key. In this way controller perform any given task not on key press but on key release. So problem of debounce is solved using a simple software tweal.

    If you are still not aware of PORT,PIN,DDR registers learn it right now.

    devesh@electroons.com
    Best viewed at Firefox 1024x768 resolution