embeded/Cortex-M3 STM2026. 9. 8. 16:22

귀찮으니 오늘도 대충대충

그나저나 고스팅이란걸 겪어보니 참 난해하구만?

 

핀은 아래처럼 구성하고

 

C1~C4는 A0~A3에 연결함 (C1을 LSB로 보게 하기 위해서)

 

코드는 효율적이진 못하지만 일단 대충 이런느낌으로 작성

row를 high / low로 바꾸면서 col을 읽는구조로 해놨고(col - pin 0~3, row - pin 4~7)

#define BUF_SIZE 50
uint8_t usb_buf[BUF_SIZE];
uint32_t count = 0;
#if 1
int prev_val = 0;
void gpio_proc()
{
  int val = 0;
  // row
  // val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) << 4);
  // val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) << 5);
  // val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) << 6);
  // val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) << 7);

  char *swname[4][4] = {
    {"S1", "S2", "S3", "S4"},
    {"S5", "S6", "S7", "S8"},
    {"S9", "S10", "S11", "S12"},
    {"S13", "S14", "S15", "S16"}
  };

  for(uint16_t row_idx = 0; row_idx < 4; row_idx ++)
  {
    uint16_t pin_arr[] = {GPIO_PIN_4, GPIO_PIN_5, GPIO_PIN_6, GPIO_PIN_7};
    HAL_GPIO_WritePin(GPIOA, pin_arr[row_idx], GPIO_PIN_SET);
    HAL_Delay(1);

    // column
    val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) << 0);
    val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) << 1);
    val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) << 2);
    val |= (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3) << 3);

    HAL_GPIO_WritePin(GPIOA, pin_arr[row_idx], GPIO_PIN_RESET);

    int col_idx = 0;
    for(int col_idx = 0; col_idx < 4; col_idx++)
    {
      if((val >> col_idx) & 0x01)
      {
        char *name = swname[row_idx][col_idx];
        sprintf((char*)usb_buf + strlen(usb_buf), "%s ", name);
      }
    }

    val = 0;
  }

  sprintf((char*)usb_buf + strlen(usb_buf), " --- %d %X\r\n", count, val);
  CDC_Transmit_FS(usb_buf, BUF_SIZE);
  memset(usb_buf,0,BUF_SIZE);
  count++;

}
#endif

 

한 열에는 누르면 전부 인식되지만

 

한 행에 누르면 (S4 + S8 + S12)

해당 라인 자체가 눌리지 않은것으로 나온다. 이래서 다이오드 다는건가?

'embeded > Cortex-M3 STM' 카테고리의 다른 글

stm32 usb composite / report id  (0) 2026.09.04
로터리 엔코더.. 2?  (0) 2026.08.31
bluepill / 로터리 엔코더 테스트  (0) 2026.08.28
stm32 port 전체 한번에 출력 변경하기  (0) 2026.06.22
stm32 uart data bit  (0) 2026.03.17
Posted by 구차니