Guest User

cs120 lab11 part 1

a guest
May 23rd, 2019
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Returns '\0' if no key pressed, else returns char '1', '2', ... '9', 'A', ...
  2. // If multiple keys pressed, returns leftmost-topmost one
  3. // Keypad must be connected to port C
  4. /* Keypad arrangement
  5. PC4 PC5 PC6 PC7
  6. col 1 2 3 4
  7. row
  8. PC0 1 1 | 2 | 3 | A
  9. PC1 2 4 | 5 | 6 | B
  10. PC2 3 7 | 8 | 9 | C
  11. PC3 4 * | 0 | # | D
  12. */
  13. #include <avr/io.h>
  14. #include <bit.h>
  15.  
  16. unsigned char GetKeypadKey() {
  17.  
  18. PORTC = 0xEF; // Enable col 4 with 0, disable others with 1’s
  19. asm("nop"); // add a delay to allow PORTC to stabilize before checking
  20. if (GetBit(PINC,0)==0) { return('1'); }
  21. if (GetBit(PINC,1)==0) { return('4'); }
  22. if (GetBit(PINC,2)==0) { return('7'); }
  23. if (GetBit(PINC,3)==0) { return('*'); }
  24.  
  25. // Check keys in col 2
  26. PORTC = 0xDF; // Enable col 5 with 0, disable others with 1’s
  27. asm("nop"); // add a delay to allow PORTC to stabilize before checking
  28. if (GetBit(PINC,0)==0) { return('2'); }
  29. if (GetBit(PINC,1)==0) { return('5'); }
  30. if (GetBit(PINC,2)==0) { return('8'); }
  31. if (GetBit(PINC,3)==0) { return('0'); }
  32. // ... *****FINISH*****
  33.  
  34. // Check keys in col 3
  35. PORTC = 0xBF; // Enable col 6 with 0, disable others with 1’s
  36. asm("nop"); // add a delay to allow PORTC to stabilize before checking
  37. if (GetBit(PINC,0)==0) { return('3'); }
  38. if (GetBit(PINC,1)==0) { return('6'); }
  39. if (GetBit(PINC,2)==0) { return('9'); }
  40. if (GetBit(PINC,3)==0) { return('#'); }
  41. // ... *****FINISH*****
  42. PORTC = 0x7F;
  43. asm("nop");
  44. if (GetBit(PINC,0)==0) { return('A'); }
  45. if (GetBit(PINC,1)==0) { return('B'); }
  46. if (GetBit(PINC,2)==0) { return('C'); }
  47. if (GetBit(PINC,3)==0) { return('D'); }
  48. // Check keys in col 4
  49. // ... *****FINISH*****
  50.  
  51. return('\0'); // default value
  52.  
  53. }
  54.  
  55. int main(void)
  56. {
  57. unsigned char x;
  58. DDRB = 0xFF; PORTB = 0x00; // PORTB set to output, outputs init 0s
  59. DDRC = 0xF0; PORTC = 0x0F; // PC7..4 outputs init 0s, PC3..0 inputs init 1s
  60. while(1) {
  61. x = GetKeypadKey();
  62. switch (x) {
  63. case '\0': PORTB = 0x1F; break; // All 5 LEDs on
  64. case '1': PORTB = 0x01; break; // hex equivalent
  65. case '2': PORTB = 0x02; break;
  66. case '3': PORTB = 0x03; break;
  67. case '4': PORTB = 0x04; break;
  68. case '5': PORTB = 0x05; break;
  69. case '6': PORTB = 0x06; break;
  70. case '7': PORTB = 0x07; break;
  71. case '8': PORTB = 0x08; break;
  72. case '9': PORTB = 0x09; break;
  73. case 'A': PORTB = 0x0A; break;
  74. case 'B': PORTB = 0x0B; break;
  75. case 'C': PORTB = 0x0C; break;
  76. // . . . ***** FINISH *****
  77.  
  78. case 'D': PORTB = 0x0D; break;
  79. case '*': PORTB = 0x0E; break;
  80. case '0': PORTB = 0x00; break;
  81. case '#': PORTB = 0x0F; break;
  82. default: PORTB = 0x1B; break; // Should never occur. Middle LED off.
  83. }
  84. }
  85. }
RAW Paste Data