Using getc

Dependencies:   ShiftReg mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* ELEC1620 Application Board Example
00002 
00003 Example of using getc to receive data from the PC to control
00004 the application board
00005 
00006 (c) Dr Craig A. Evans, University of Leeds, Feb 2017
00007 
00008 */
00009 
00010 #include "mbed.h"
00011 #include "ShiftReg.h"  // include ShiftReg library
00012 
00013 ShiftReg shift;  // create ShiftReg object
00014 Serial pc(USBTX,USBRX);  
00015 
00016 int main()
00017 {
00018     // values for 0 - 9 in hex
00019     int seven_seg_array [] = {
00020         0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67
00021     };
00022 
00023     // write 0 to 7-seg to turn it off
00024     shift.write(0x00);
00025 
00026     while(1) {
00027 
00028         // readable tells us if a character is waiting to be read
00029         if ( pc.readable() ) {
00030             // if one is there, then read in using getc
00031             char c = pc.getc();    
00032             
00033             // check if it is a digit that has been received - note ' ' for char
00034             if (c >= '0' && c <= '9') {
00035                 // the received char is in ASCII so convert to int by substracting
00036                 // the ASCII value of '0'
00037                 int value = c - '0';
00038                 
00039                 // make that value appear on the 7-seg display
00040                 shift.write(seven_seg_array [value]);
00041                 
00042             }
00043             
00044         }
00045         
00046 
00047     }
00048 }