SEARCH
TOOLBOX
LANGUAGES
modified on 18 September 2014 at 16:23 ••• 27,437 views

06-03 KeyPressDisplay

From Manuals

(Difference between revisions)
Jump to: navigation, search
Support (Talk | contribs)
(Created page with 'This program reads a number pressed on the personal computers keyboard while using a terminal program and displays the number on the 7 segment display. /* Program Example 6.3: …')
Newer edit →

Revision as of 15:46, 14 August 2014

This program reads a number pressed on the personal computers keyboard while using a terminal program and displays the number on the 7 segment display.

/* Program Example 6.3: Host keypress to 7-seg display
                                                              */
#include "mbed.h"
Serial pc(USBTX, USBRX);                         // comms to host PC
BusOut Seg1(D0,D1,D2,D3,D4,D5,D6,D7);            // A,B,C,D,E,F,G,DP
BusOut Seg2(D8,D9,D10,D11,D12,D13,D16,D17);      // A,B,C,D,E,F,G,DP

void SegInit(void);                          // function prototype
void HostInit(void);                         // function prototype
char GetKeyInput(void);                      // function prototype
char SegConvert(char SegValue);              // function prototype
char data1, data2;                           // variable declarations

int main() {         // main program
  SegInit();         // call function to initialise the 7-seg displays
  HostInit();        // call function to initialise the host terminal
  while (1) {                 // infinite loop
    data2 = GetKeyInput();    // call function to get 1st key press
    Seg2=SegConvert(data2);   // call function to convert and output
    data1 = GetKeyInput();    // call function to get 2nd key press
    Seg1=SegConvert(data1);   // call function to convert and output
    pc.printf("  ");          // display spaces between numbers
  }
}
// functions
void SegInit(void) {
  Seg1=SegConvert(0);        // initialise to zero
  Seg2=SegConvert(0);        // initialise to zero
}

void HostInit(void) {
  pc.printf("\n\rType two digit numbers to be displayed\n\r");
}

char GetKeyInput(void) {
  char c = pc.getc();      // get keyboard data (ascii 0x30-0x39)
  pc.printf("%c",c);       // print ascii value to host PC terminal
  return (c&0x0F);         // apply bit mask to convert to decimal, and return
}

char SegConvert(char SegValue) {        // function 'SegConvert'
  char SegByte=0x00;
  switch (SegValue) {                   //DP G F E D C B A
    case 0 : SegByte = 0x3F;break;      // 0 0 1 1 1 1 1 1 binary
    case 1 : SegByte = 0x06;break;      // 0 0 0 0 0 1 1 0 binary
    case 2 : SegByte = 0x5B;break;      // 0 1 0 1 1 0 1 1 binary
    case 3 : SegByte = 0x4F;break;      // 0 1 0 0 1 1 1 1 binary
    case 4 : SegByte = 0x66;break;      // 0 1 1 0 0 1 1 0 binary
    case 5 : SegByte = 0x6D;break;      // 0 1 1 0 1 1 0 1 binary
    case 6 : SegByte = 0x7D;break;      // 0 1 1 1 1 1 0 1 binary
    case 7 : SegByte = 0x07;break;      // 0 0 0 0 0 1 1 1 binary
    case 8 : SegByte = 0x7F;break;      // 0 1 1 1 1 1 1 1 binary
    case 9 : SegByte = 0x6F;break;      // 0 1 1 0 1 1 1 1 binary
  }
    return SegByte;
}



File:BAM210 06 03 SCH.png
Schematic for example 06-03