Et eksempel på hvordan man kan lese inn et 8-knapp keypad og konverter inngangs-bittene til verdier som kan brukes i programmet

Dependencies:   mbed

Committer:
benrammok
Date:
Fri Nov 06 22:29:52 2015 +0000
Revision:
0:4c68dc485cb8
8-button keypad, can be extended

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benrammok 0:4c68dc485cb8 1 #include "mbed.h"
benrammok 0:4c68dc485cb8 2
benrammok 0:4c68dc485cb8 3 //Definerer BusIn
benrammok 0:4c68dc485cb8 4 BusIn numpad(PA_10, PB_3, PB_5,PB_4, PB_10, PA_8, PA_9, PB_6);
benrammok 0:4c68dc485cb8 5
benrammok 0:4c68dc485cb8 6 //Funksjonsprototyper
benrammok 0:4c68dc485cb8 7 int returnValueKeyPressed(int, int);
benrammok 0:4c68dc485cb8 8
benrammok 0:4c68dc485cb8 9 int main() {
benrammok 0:4c68dc485cb8 10 numpad.mode(PullUp);
benrammok 0:4c68dc485cb8 11 int sum;
benrammok 0:4c68dc485cb8 12 int tall1, tall2;
benrammok 0:4c68dc485cb8 13 while(1) {
benrammok 0:4c68dc485cb8 14 sum = 0;
benrammok 0:4c68dc485cb8 15 printf("Press first number: ");
benrammok 0:4c68dc485cb8 16
benrammok 0:4c68dc485cb8 17 //Så lenge ingen av knappene trykkes
benrammok 0:4c68dc485cb8 18 while(returnValueKeyPressed(numpad.read(), 1)==0){};
benrammok 0:4c68dc485cb8 19
benrammok 0:4c68dc485cb8 20 //Bruker funksjonen returnValueKeyPressed som gjør om BusIn signal til korrensponderende tall
benrammok 0:4c68dc485cb8 21 tall1 = returnValueKeyPressed(numpad.read(), 1);
benrammok 0:4c68dc485cb8 22 printf(" %d\r\n", returnValueKeyPressed(numpad.read(), 1));
benrammok 0:4c68dc485cb8 23
benrammok 0:4c68dc485cb8 24 //Så lenge en eller fler av knappene ikke er sluppet
benrammok 0:4c68dc485cb8 25 while(returnValueKeyPressed(numpad.read(), 1)!=0){};
benrammok 0:4c68dc485cb8 26 printf("Press second number: ");
benrammok 0:4c68dc485cb8 27
benrammok 0:4c68dc485cb8 28 //Så lenge ingen av knappene trykkes
benrammok 0:4c68dc485cb8 29 while(returnValueKeyPressed(numpad.read(), 1)==0){};
benrammok 0:4c68dc485cb8 30
benrammok 0:4c68dc485cb8 31 tall2 = returnValueKeyPressed(numpad.read(), 1);
benrammok 0:4c68dc485cb8 32 printf(" %d\r\n", returnValueKeyPressed(numpad.read(), 1));
benrammok 0:4c68dc485cb8 33
benrammok 0:4c68dc485cb8 34 //Så lenge en eller fler av knappene ikke er sluppet
benrammok 0:4c68dc485cb8 35 while(returnValueKeyPressed(numpad.read(), 1)!=0){};
benrammok 0:4c68dc485cb8 36 sum = tall1 + tall2;
benrammok 0:4c68dc485cb8 37 printf("The sum of the two numbers is %d\r\n", sum);
benrammok 0:4c68dc485cb8 38 wait(1);
benrammok 0:4c68dc485cb8 39 }
benrammok 0:4c68dc485cb8 40
benrammok 0:4c68dc485cb8 41 }
benrammok 0:4c68dc485cb8 42
benrammok 0:4c68dc485cb8 43
benrammok 0:4c68dc485cb8 44 //Funksjon for å konvertere inngangsbit til tilhørende verdi på et 8-knapper numpad, kan utvides til 12-knapper
benrammok 0:4c68dc485cb8 45 int returnValueKeyPressed(int key, int inv = 0){
benrammok 0:4c68dc485cb8 46 int opKey;
benrammok 0:4c68dc485cb8 47
benrammok 0:4c68dc485cb8 48 //Betingelse som sjekker om det er snakk om inverterte innganger
benrammok 0:4c68dc485cb8 49 if(inv){
benrammok 0:4c68dc485cb8 50 opKey = ~key & 0xFF; //Inverterer Key og masker for 8-bit, kan forandres til flere bit
benrammok 0:4c68dc485cb8 51 }else{
benrammok 0:4c68dc485cb8 52 opKey = key & 0xFF; //Masker for 8-bit, kan forandres til flere bit
benrammok 0:4c68dc485cb8 53 }
benrammok 0:4c68dc485cb8 54
benrammok 0:4c68dc485cb8 55 switch(opKey){
benrammok 0:4c68dc485cb8 56 case 1: // 0000 0001
benrammok 0:4c68dc485cb8 57 return 1;
benrammok 0:4c68dc485cb8 58 case 2: // 0000 0010
benrammok 0:4c68dc485cb8 59 return 2;
benrammok 0:4c68dc485cb8 60 case 4: // 0000 0100
benrammok 0:4c68dc485cb8 61 return 3;
benrammok 0:4c68dc485cb8 62 case 8: // 0000 1000
benrammok 0:4c68dc485cb8 63 return 4;
benrammok 0:4c68dc485cb8 64 case 16: // 0001 0000
benrammok 0:4c68dc485cb8 65 return 5;
benrammok 0:4c68dc485cb8 66 case 32: // 0010 0000
benrammok 0:4c68dc485cb8 67 return 6;
benrammok 0:4c68dc485cb8 68 case 64: // 0100 0000
benrammok 0:4c68dc485cb8 69 return 7;
benrammok 0:4c68dc485cb8 70 case 128: // 1000 0000
benrammok 0:4c68dc485cb8 71 return 8;
benrammok 0:4c68dc485cb8 72 default:
benrammok 0:4c68dc485cb8 73 return 0;
benrammok 0:4c68dc485cb8 74 }
benrammok 0:4c68dc485cb8 75 }