0 Replies Latest reply on Feb 13, 2020 10:51 PM by MoTa_728816

    4x4 Matrix Keypad Sample

    MoTa_728816

      IMG_4132.JPG

      So yesterday I encountered another chance to write a sample program for a Matrix Keypad.

      Data entry from keyboard  to PSOC

       

      昨日、再びマトリクスキーパッドのサンプルを書く機会に恵まれました。

       

      Although I'm quite sure that I have written PSoC program(s) using Matrix Keypad I could not find any in my PC. orz

      So here is a yet another newly written sample of 4x4 Matrix Keypad for CY8CKIT-059.

       

      確か、一度ならず書いていたはずなのですが例によって見つかりません。 orz

      という訳で懲りずに再び CY8CKIT-059 用に書いてみました。

       

      I hope that next time I need this, I will be find it here...

       

      次に必要になったときには、ここで見つけられることを期待しつつ...

       

      schematic

      032-Schematic.JPG

      pins

      033-pins.JPG

      Tera Term log

      031-TeraTerm-keymatrix-5.JPG

      main.c

      ========================

      #include "project.h"

      #include "stdio.h"

       

      #define NUM_ROW 4

      #define NUM_COL 4

       

      volatile uint8_t key[NUM_ROW][NUM_COL] = { 0u } ;

      uint8_t letter[NUM_ROW][NUM_COL] = {

          { '1', '2', '3', 'A' },

          { '4', '5', '6', 'B' },

          { '7', '8', '9', 'C' },

          { '*', '0', '#', 'D' }

      } ;

         

      volatile int row_no = 0 ;

      volatile int col_no = 0 ;

       

      void scan_key_matrix(void)

      {

          switch(row_no) {

          case  0: ROW0_Write(1) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;

          case  1: ROW0_Write(0) ; ROW1_Write(1) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;

          case  2: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(1) ; ROW3_Write(0) ; break ;

          case  3: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(1) ; break ;

          default: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;

          }

       

          switch(col_no) {

          case 0: key[row_no][col_no] = COL0_Read() ; break ;

          case 1: key[row_no][col_no] = COL1_Read() ; break ;

          case 2: key[row_no][col_no] = COL2_Read() ; break ;

          case 3: key[row_no][col_no] = COL3_Read() ; break ;

          }

         

          col_no++ ;

          if (col_no >= NUM_COL) {

              col_no = 0 ;

              row_no = (row_no + 1) % NUM_ROW ;

          }

          ROW0_Write(0) ;

          ROW1_Write(0) ;

          ROW2_Write(0) ;

          ROW3_Write(0) ;

      }

       

      CY_ISR(my_tick_isr)

      {

          scan_key_matrix() ;

      }

       

      #define STR_LEN 64

      char str[STR_LEN+1] ;

      void print(char *str)

      {

          UART_PutString(str) ;

      }

       

      void printc(char c)

      {

          UART_PutChar(c) ;

      }

       

      void cls(void)

      {

          print("\033c") ; /* reset */

          CyDelay(20) ;

          print("\033[2J") ; /* clear screen */

          CyDelay(20) ;

      }

       

      void splash(void)

      {

          cls() ;

          print("5LP 4x4 Matrix Key Test") ;

          snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

          print(str) ;

      }

       

      void init_hardware(void)

      {

          CyGlobalIntEnable; /* Enable global interrupts. */

          UART_Start() ;

          splash() ;

          CySysTickStart() ;

          CySysTickSetCallback(0, my_tick_isr) ;

      }

       

      void dump_key_matrix(void)

      {

          int col, row ;

          for (row = 0 ; row < NUM_ROW ; row++) {

              for (col = 0 ; col < NUM_COL ; col++) {

                  if (key[row][col]) {

                      snprintf(str, STR_LEN, "%c ", letter[row][col]) ;

                      print(str) ;

                  } else {

                      print("- ") ;

                  }

              }

              print("\n\r") ;

          }  

      }

       

      int main(void)

      {

          init_hardware() ;

       

          for(;;)

          {

              cls() ;

              dump_key_matrix() ;

              CyDelay(1000) ;

          }

      }

      ========================

       

      And I also compiled the project for CY8CKIT-044

       

      おまけに CY8CKIT-044 でも動かしておきました。

      IMG_4133.JPG

      In the main.c, the only difference were

       

      重箱の角ですが、 059版との差は

       

      > UART_PutString(str)

      < UART_UartPutString(str)

       

      >     print("5LP 4x4 Matrix Key Test") ;

      <     print("PSoC 4 4x4 Matrix Key Test") ;

       

      moto