Appendix DExample Keyboard Interface

A partial solution to the keyboard-scan design challenge is provided in the following code. This is based around a three-column by four-row keypad so will need a slight extension for the four by four keypad suggested. Functions are used to set up the required resources, inputs, outputs and timer. Other functions provide delays to control scanning and de-bounce tests. The operations to activate the scan columns and obtain row data are in further functions to simplify main() as much as possible. The translation of the row and column vales to the actual key-face signature is also provided.

#include “stm32f4_discovery.h”

void SetupOutPins(void);
void SetupInputPins(void);
void SetupTimerDelay(uint16_t time);
void Set_Column(int i);
void Timed_Wait(uint16_t time);
uint8_t get_row(void);
char get_key(uint8_t col, uint8_t row);
int main(void)
   uint8_t i, row;
   char key;

   SetupTimerDelay(1000); /* 1000us de-bounce */

      for (i = 1; i <4; i++) /* scan columns */
         row =get_row();
         if (row != 0)
            key = get_key(i, row);
         Timed_Wait(100); /* 100ms column scan */

uint8_t get_row()
   uint16_t row, rowx;

   row = (GPIO_Read_InputData(GPIOB) & 0xf000);
   if (row != 0)
      Timed_Wait(10); /* try again in 1ms */
      rowx = (GPIO_Read_InputData(GPIOB) & 0xf000);
      if ...

Get Digital Interface Design and Application now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.