좀더 hal 코드를 써서 고생을 해봐야하나..

아무튼, 간단하게  float input 으로 바꾸기 위해 input mode로 바꾸고

output mode로 바꾸고 나서는 출력을 써줘야 된다.

#define BUF_SIZE 50
uint8_t usb_buf[BUF_SIZE];
uint32_t count = 0;
#if 1
int prev_val = 0;

// STM32F1: 4 bits per pin (CNF[1:0] MODE[1:0]) in CRL (pins 0-7) / CRH (pins 8-15)
static inline void pin_set_cfg(GPIO_TypeDef *port, uint16_t pin, uint32_t cfg)
{
    uint32_t num = __builtin_ctz(pin);
    uint32_t *reg = (num < 8U) ? &port->CRL : &port->CRH;
    uint32_t pos = (num % 8U) * 4U;
    *reg = (*reg & ~(0xFU << pos)) | (cfg << pos);
}

static inline void pin_hiz(GPIO_TypeDef *port, uint16_t pin)
{
    pin_set_cfg(port, pin, 0x4U);  // CNF=01 MODE=00: floating input (Hi-Z)
}

static inline void pin_output(GPIO_TypeDef *port, uint16_t pin)
{
    pin_set_cfg(port, pin, 0x2U);  // CNF=00 MODE=10: push-pull output, 2 MHz
}

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};
    pin_output(GPIOA, pin_arr[row_idx]);
    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);
    pin_hiz(GPIOA, pin_arr[row_idx]);

    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

 

전에는 이건 되었는데

 

요건 안되었었는데 input mode로 바꾸고 나서 잘 된다.

 

 

+

S1 / S4 / S16을 눌렀는데 S13 가 눌린걸로 인식되는건 다이오드 달아야지 머..

Posted by 구차니