O'Reilly logo

Digital Interface Design and Application by Jonathan A. Dell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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;

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

   while(1)
   {
      for (i = 1; i <4; i++) /* scan columns */
      {
         Set_Column(i);
         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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required