

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


#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;
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]); }