Main Menu
Home
AVR News
Atmel AVR
AVR Development Tools
Valuable Tools
WinAVR toolset
Makefile for WinAVR
AVR Virtual Simulators
Hardware for prototyping
GCC and AVR-GCC
Short introduction to C
AVR-GCC Tutorial
Example AVR Projects
AVR-GCC articles
WinAVR Site Map
Scienceprog FORUM
ScienceProg BLOG
Disclaimer
WinARM Tutorial
Latest comments
Measuring motor speed and...
Dear Administrator The code above...
More...

Programming AVR ADC module...
hi please send me example usart with...
More...

Measuring motor speed and...
main.c: In function...
More...

Running TX433 and RX433 RF...
(Hopefully this is not a re-post). I am...
More...

Running TX433 and RX433 RF...
I just bnought a pair of the RX/TX 433...
More...

Friendly sites

Recommended sites


Latest News
4x4 keypad example using AVR-GCC C language PDF Print E-mail
Written by Administrator   
Friday, 03 November 2006
Last Updated ( Friday, 03 November 2006 )

This is as simple routine how to read 4x4 keypad keys using AVR-GCC language. The keypad is connected to AVR microcontroller 8 bit port. In this example it is B port. You can change ports depending on your needs – this is only an example ant it is not the only way to this.

4x4 keypad AVR C 

Howt it works. Well very simply. PORTB is divided into two nibbles PINB0 – PINB3 as inputs (as rows) and PINB4-PINB7 as outputs (columns). The keys are checked in a loop in series. Lets say if we set first row output (PORTB bit 7) to 0 then when checking rows we are looking which bit is set to 0, because of key pressed with function bit_is_set(PINB, bitNo). This function gives non-zero if bit is clear.

10k resistors protect AVR from shortcuts.


#include <avr/io.h>
int main()
{
//high nibble for output(columns) low for input(rows);
DDRB=0xF0;
//enable internal pullups for PB0-PB3
PORTB=0x0F;
//Port D for indication only
DDRD=0xFF;
while (1) //loop key check forever
	{
		//first column
		PORTB =0b01111111;
		//check for rows and send key number to portD
		//instead sending key number to PORTD you can use
		// any function that serves pressed button
		if (bit_is_set(PINB, 3)) PORTD=1;
		if (bit_is_set(PINB, 2)) PORTD=2;
		if (bit_is_set(PINB, 1)) PORTD=3;
		if (bit_is_set(PINB, 0)) PORTD=4;
		//second column
		PORTB =0b10111111;
		if (bit_is_set(PINB, 3)) PORTD=5;
		if (bit_is_set(PINB, 2)) PORTD=6;
		if (bit_is_set(PINB, 1)) PORTD=7;
		if (bit_is_set(PINB, 0)) PORTD=8;
		//third column
		PORTB =0b11011111;
		if (bit_is_set(PINB, 3)) PORTD=9;
		if (bit_is_set(PINB, 2)) PORTD=10;
		if (bit_is_set(PINB, 1)) PORTD=11;
		if (bit_is_set(PINB, 0)) PORTD=12;
		//fourth column
		PORTB =0b11101111;
		if (bit_is_set(PINB, 3)) PORTD=13;
		if (bit_is_set(PINB, 2)) PORTD=14;
		if (bit_is_set(PINB, 1)) PORTD=15;
		if (bit_is_set(PINB, 0)) PORTD=16;
	}
}

   

Users' Comments  
 

Average user rating

 


Add your comment
Only registered users can comment an article. Please login or register.

No comment posted



mXcomment 1.0.6 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
< Prev

© 2008 WinAVR AVR-GCC Tutorial
Joomla! is Free Software released under the GNU/GPL License.