Advertisement
Guest User

keypad

a guest
Apr 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <stdio.h>
  2. /*
  3. * startup.c
  4. *
  5. */
  6. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  7.  
  8. void startup ( void )
  9. {
  10. __asm volatile(
  11. " LDR R0,=0x2001C000\n" /* set stack */
  12. " MOV SP,R0\n"
  13. " BL main\n" /* call main */
  14. "_exit: B .\n" /* never return */
  15. ) ;
  16. }
  17. typedef struct {
  18. uint32_t moder;
  19. uint16_t otyper; // +0x4
  20. uint16_t otReserved; //
  21. uint32_t ospeedr; // +0x8
  22. uint32_t pupdr; // +0xc (12)
  23. uint8_t idrLow; // +0x10
  24. uint8_t idrHigh; // +0x11
  25. uint16_t idrReserved;
  26. uint8_t odrLow; // +0x14
  27. uint8_t odrHigh; // +0x15
  28. uint16_t odrReserved;
  29. } GPIO;
  30. typedef volatile GPIO* gpioptr;
  31. #define GPIO_D (*(gpioptr)0x40020c00)
  32.  
  33. void app_init(void){
  34. GPIO_D.moder = 0x55005555;
  35. GPIO_D.otyper = GPIO_D.otyper & 0x00FF;
  36. GPIO_D.pupdr = GPIO_D.pupdr & 0x0000FFFF | 0x00AA0000;
  37. }
  38. void kbdActivate (unsigned int row){
  39. switch (row){
  40. case 1: GPIO_D.odrHigh = 0x10; break;
  41. case 2: GPIO_D.odrHigh = 0x20; break;
  42. case 3: GPIO_D.odrHigh = 0x40; break;
  43. case 4: GPIO_D.odrHigh = 0x80; break;
  44. default: GPIO_D.odrHigh = 0x00; break;
  45. }
  46. }
  47.  
  48. int kbdGetCol (void){
  49. unsigned char c;
  50. c = GPIO_D.idrHigh;
  51. if (c & 0x8) return 4;
  52. if (c & 0x4) return 3;
  53. if (c & 0x2) return 2;
  54. if (c & 0x1) return 1;
  55. return 0;
  56. }
  57.  
  58. unsigned char keyb(void){
  59. unsigned char key[]={1,2,3,0xA,4,5,6,0xB,7,8,9,0xC,0xE,0,0xF,0xD};
  60. int row, col;
  61. for (row=1; row <=4; row++){
  62. kbdActivate(row);
  63. if ((col = kbdGetCol () )){
  64. kbdActivate(0);
  65. return key [4*(row-1)+(col-1)];
  66. }
  67. }
  68. kbdActivate (0);
  69. return 0xFF;
  70. }
  71.  
  72.  
  73.  
  74. void out7seg(unsigned char c){
  75. switch (c){
  76. case 0 : GPIO_D.odrLow = 0x3F; break;
  77. case 1 : GPIO_D.odrLow = 0x06; break;
  78. case 2 : GPIO_D.odrLow = 0x5B; break;
  79. case 3 : GPIO_D.odrLow = 0x4F; break;
  80. case 4 : GPIO_D.odrLow = 0x66; break;
  81. case 5 : GPIO_D.odrLow = 0x6D; break;
  82. case 6 : GPIO_D.odrLow = 0x7D; break;
  83. case 7 : GPIO_D.odrLow = 0x07; break;
  84. case 8 : GPIO_D.odrLow = 0x7F; break;
  85. case 9 : GPIO_D.odrLow = 0x6F; break;
  86. case 10: GPIO_D.odrLow = 0x77; break;
  87. case 11: GPIO_D.odrLow = 0x7C; break;
  88. case 12: GPIO_D.odrLow = 0x39; break;
  89. case 13: GPIO_D.odrLow = 0x5E; break;
  90. case 14: GPIO_D.odrLow = 0x79; break;
  91. case 15: GPIO_D.odrLow = 0x71; break;
  92. default: GPIO_D.odrLow = 0x00; break;
  93. }
  94. }
  95.  
  96. void main(void){
  97. app_init();
  98. while(1){
  99. out7seg(keyb());
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement