Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 BusInOut colBus( p26, p25, p24); 00004 const PinName rowPins[] = { p30, p29, p28, p27 }; 00005 00006 volatile bool irqFinished = false; 00007 00008 inline char toChar( unsigned char r, unsigned char c) { 00009 static const char keytable[] = { '1', '2', '3', 'A', 00010 '4', '5', '6', 'B', 00011 '7', '8', '9', 'C', 00012 '*', '0', '#', 'D' 00013 }; 00014 return keytable[r*4+c]; 00015 } 00016 00017 inline char toBinary( unsigned char r, unsigned char c) { 00018 static const char keytable[] = { 0x1, 0x2, 0x3, 0xC, 00019 0x4, 0x5, 0x6, 0xD, 00020 0x7, 0x8, 0x9, 0xE, 00021 0xA, 0x0, 0xB, 0xF 00022 }; 00023 return keytable[r*4+c]; 00024 } 00025 00026 inline void start() { 00027 colBus.output(); 00028 colBus = 0xF; 00029 } 00030 00031 // simple log2, only 4bits needed 00032 inline unsigned char _lg2( unsigned char c) { 00033 return c&1 ? 0 : c&2 ? 1 : c&4 ? 2 : 3; 00034 } 00035 00036 inline char pin2Row(PinName pin) { 00037 return rowPins[0] == pin ? 0 : rowPins[1] == pin ? 1 : rowPins[2] == pin ? 2 : 3; 00038 } 00039 00040 BusOut code(LED4, LED3, LED2, LED1); //show binary code of key 00041 00042 void checkColumn(PinName pinPressed) { 00043 { //debounce 00044 DigitalIn in(pinPressed); 00045 for (char c=0; c<100; ++c) { 00046 if ( !in.read()) { 00047 irqFinished=true; 00048 return; // key not long enough pressed 00049 } 00050 wait_us(100); 00051 } 00052 } 00053 // check column 00054 DigitalOut p(pinPressed); 00055 p = 0x1; 00056 colBus.input(); 00057 unsigned char col = colBus.read(); 00058 if (col) { 00059 col = _lg2(col); 00060 { // blink to show key pressed 00061 code = 0; 00062 wait_ms(150); 00063 code = 0xF; 00064 wait_ms(150); 00065 } 00066 char row = pin2Row(pinPressed); 00067 code = toBinary(row,col); 00068 // printf("%d, %d, %c\r\n", row, col, toChar(row,col)); 00069 } 00070 irqFinished = true; 00071 } 00072 00073 void trigger27() { 00074 checkColumn(p27); 00075 } 00076 void trigger28() { 00077 checkColumn(p28); 00078 } 00079 void trigger29() { 00080 checkColumn(p29); 00081 } 00082 void trigger30() { 00083 checkColumn(p30); 00084 } 00085 00086 void checkRow() { 00087 InterruptIn irqIn27(p27), irqIn28(p28), irqIn29(p29), irqIn30(p30); 00088 irqIn27.rise( &trigger27); 00089 irqIn28.rise( &trigger28); 00090 irqIn29.rise( &trigger29); 00091 irqIn30.rise( &trigger30); 00092 } 00093 00094 int main() { 00095 while (1) { 00096 start(); 00097 irqFinished = false; 00098 checkRow(); 00099 while (!irqFinished); 00100 } 00101 }
Generated on Sun Jul 24 2022 05:01:51 by
1.7.2