AVR Microcontroller Keyboard Input Program, Exercises of Microprocessors

An avr microcontroller program for reading keyboard input using portb, portd, and portc. The program uses interrupts and timers to detect key presses and return the corresponding character. It can be used as a starting point for developing more complex input systems.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

parmila
parmila 🇮🇳

4.4

(9)

78 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <avr\io.h> // Most basic include files
#include <avr\interrupt.h> // Add the necessary ones
#include <avr\signal.h>
#include <util\delay.h>
void main(void)
{
unsigned int keystate;
int key_sm(int);
PORTB=0x0f;
TCCR0=0x15;
keystate=1;
while(1)
{
keystate=key_sm(keystate);
}
}
int key_sm(int keystate)
{
int keypress(void);
char buffer;
char getkey(void);
switch(keystate)
{
case 1:
keypress();
if(keypress()== 1)
{
keystate=2;
TCNT0=216;
while(TCNT0<0xFF);
return(keystate);
}
break;
case 2:
if(TIFR==1)
{
keystate=3;
buffer=getkey();
PORTD=buffer;
return(keystate);
}
break;
case 3:
if(keypress()== 1)
{
keystate=2;
docsity.com
pf3

Partial preview of the text

Download AVR Microcontroller Keyboard Input Program and more Exercises Microprocessors in PDF only on Docsity!

#include <avr\io.h> // Most basic include files #include <avr\interrupt.h> // Add the necessary ones #include <avr\signal.h> #include <util\delay.h>

void main(void) { unsigned int keystate; int key_sm(int); PORTB=0x0f; TCCR0=0x15; keystate=1; while(1) { keystate=key_sm(keystate); } }

int key_sm(int keystate) { int keypress(void); char buffer; char getkey(void); switch(keystate) { case 1: keypress(); if(keypress()== 1) { keystate=2; TCNT0=216; while(TCNT0<0xFF); return(keystate); } break;

case 2: if(TIFR==1) { keystate=3; buffer=getkey(); PORTD=buffer; return(keystate); } break;

case 3: if(keypress()== 1) { keystate=2;

TCNT0=216;

while(TCNT0<0xFF); return(keystate); } break; } }

int keypress() { char mask=0x0f; if (PORTB&mask==0x0F) { return(0); } else return(1); }

char getkey() { unsigned int l; char mask,a,b,c,r; for(l=0;l<4;l++) { mask=1<<l; PORTD=~mask; a=PORTB|mask; if(a==0xFF) { c=l; break; } }

for(l=0;l<4;l++) { mask=1<<l; b=PORTC&mask; if (b==0x00); else { r=l; break; } }

if(r<4 && c<4) { char arr[4][4]={{'0','1','2','3'},{'4','5','6','7'},{'8','9','A','B'},{'C','D' ,'E','F'}}; return(arr[c][r]); }