Bucle a través de puertos (avr)

-1

Estoy intentando escanear una matriz de claves que tiene 9 filas de largo. Actualmente mi código es un conjunto de sentencias if, una por fila, y me gustaría que se redujera a un pequeño bucle if. No creo que pueda colocar PINB, PIND, etc. en una matriz, ya que simplemente almacenaría el contenido actual del PIN, ¿no?

Aquí está el código que estoy tratando de condensar:

static inline uint64_t Buttons_GetStatus(void)
{
  uint64_t currentStatus = 0;

  // Scan the matrix, one column at a time
  for (uint8_t column = 0x00; column < 0x06; column++)
  {
    PORTB |= COLUMNS_ALL;
    PORTB &= ~(0x01 << column);
    if (!(PINB & 0b01000000))
    {
      currentStatus |= matrix[0][column];
    }
    if (!(PINB & 0b10000000))
    {
      currentStatus |= matrix[1][column];
    }
    if (!(PIND & 0b00000001))
    {
      currentStatus |= matrix[2][column];
    }
    if (!(PIND & 0b00000010))
    {
      currentStatus |= matrix[3][column];
    }
    if (!(PIND & 0b00000100))
    {
      currentStatus |= matrix[4][column];
    }
    if (!(PIND & 0b00001000))
    {
      currentStatus |= matrix[5][column];
    }
    if (!(PIND & 0b00010000))
    {
      currentStatus |= matrix[6][column];
    }
    if (!(PIND & 0b00100000))
    {
      currentStatus |= matrix[7][column];
    }
    if (!(PIND & 0b01000000))
    { 
      currentStatus |= matrix[8][column];
    }
  }

  return currentStatus;
}

y quiero que se vea algo como esto:

static inline uint64_t Buttons_GetStatus(void)
{
  uint64_t currentStatus = 0;

  // Scan the matrix, one column at a time
  for (uint8_t column = 0x00; column < 0x06; column ++)
  {
    PORTB |= COLUMNS_ALL;
    PORTB &= ~(0x01 << column);
    for (int row = 0; row < 9; row++)
    {
      if (!(rows[row][PIN] & rows[row][BIT]))
      {
        currentStatus |= matrix[row][column];
      }
    }
  }

  return currentStatus;
}

En caso de que ayude, estoy trabajando con un ejemplo de la biblioteca LUFA y este código está en mi archivo personalizado Buttons.h para mi placa.

    
pregunta ben

1 respuesta

1
pinVal = (PIND & 0b01111111);
for (int row = 0; row < 9; row++) {
  if (pinVal & 0x1) {
    currentStatus |= matrix[row+2][column];
  }
  pinVal = pinVal >> 1;
}

Y luego lo mismo para PINB .

    
respondido por el AlexZam

Lea otras preguntas en las etiquetas