Taking Input AVR

July 25, 2009 by admin
Filed under: Microcontrollers 

Taking Input AVR (Input Event Handelling)

switch

#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=0×00; // initially LED off

PORTD=0xFF;  // Internal pullup is enabled

while(1==1)  // infinite loop

{

if(!(PIND & 0×01))  // check whether the switch is pressed or not

{

while(!(PIND & 0×01)); //wait for debounce

PORTA=(1<<3);    // LED on

_delay_ms(500);   // On for a half second

PORTA=0×00;    // LED turns off

}  // if ends here

}  // while(1 ) ends here

return 0;

} // main ends here

Explanation- As we know that DDRx registers are used to set data direction of PORTx either as input or output. Since in this case switch is connected to PIN0 of PORTD, it is declared as input pin.

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. AVR family controllers has internal pullups which can be enabled by declaring the pin as input and writing a 1 initially to that pin (In our case it is PIN 0 of PORTD).

pullup

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.

According to debouncing concept the event occured when the switch is released and not when switch is pressed.

debounce

For more concepts and ideas keep visiting electroons.com

Contact-

Devesh Samaiya

devesh@electroons.com

Ph- +91-9977411065


Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





Anti-Spam Protection by WP-SpamFree