Driving the 7 segment display
Dependencies: SevenSegLed mbed
main.cpp@0:440732350fa8, 2014-02-07 (annotated)
- Committer:
- salatron
- Date:
- Fri Feb 07 10:39:28 2014 +0000
- Revision:
- 0:440732350fa8
7 Segment Display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
salatron | 0:440732350fa8 | 1 | /******************************************************************************* |
salatron | 0:440732350fa8 | 2 | * This program demonstrates how to drive the seven segment display * |
salatron | 0:440732350fa8 | 3 | * * |
salatron | 0:440732350fa8 | 4 | * Jon Fuge * |
salatron | 0:440732350fa8 | 5 | * V1.0 13/12/2013 First issue of code * |
salatron | 0:440732350fa8 | 6 | *******************************************************************************/ |
salatron | 0:440732350fa8 | 7 | #include "mbed.h" |
salatron | 0:440732350fa8 | 8 | #include "SevenSegLed.h" |
salatron | 0:440732350fa8 | 9 | |
salatron | 0:440732350fa8 | 10 | // Lets define some "macros" to make the code easier to read |
salatron | 0:440732350fa8 | 11 | #define mUnits D_7seg[1] // "mUnits" will be substiuted for "D_7seg[1]" |
salatron | 0:440732350fa8 | 12 | #define mTens D_7seg[0] // "mTens" will be substiuted for "D_7seg[0]" |
salatron | 0:440732350fa8 | 13 | #define mDot D_dot[1] // "mDot" will be substiuted for "D_dot[1]" |
salatron | 0:440732350fa8 | 14 | |
salatron | 0:440732350fa8 | 15 | void attimeout(); //declare prototype for timeout handler. |
salatron | 0:440732350fa8 | 16 | //configure sevensegled pin connection mapping. |
salatron | 0:440732350fa8 | 17 | // |
salatron | 0:440732350fa8 | 18 | // common type (0:anode common 1:cathode common) |
salatron | 0:440732350fa8 | 19 | // | display mode (0:smooth 1:hard) |
salatron | 0:440732350fa8 | 20 | // | | segA segB segC segD segE segF segG segP com1 com2 |
salatron | 0:440732350fa8 | 21 | SevenSegLed segmentled(0, 0, P1_23, P1_28, P0_16, P1_31, P1_13, P1_16, P1_19, P0_23, p21, P1_25); |
salatron | 0:440732350fa8 | 22 | |
salatron | 0:440732350fa8 | 23 | //Define two arrays to house the data to be outputted. D_7seg can contain digits 0‐F and D_dot 0‐1. |
salatron | 0:440732350fa8 | 24 | // |
salatron | 0:440732350fa8 | 25 | // 0 1 //0 = leftmost digit, 1 = rightmost digit |
salatron | 0:440732350fa8 | 26 | // | | |
salatron | 0:440732350fa8 | 27 | uint8_t D_7seg[2] = {0, 0}; // number (0x00:"0", ... , 0x09:"9", 0x0A:"A", ... , 0x0F:"F", other:" ") |
salatron | 0:440732350fa8 | 28 | uint8_t D_dot[2] = {0, 0}; // dotpoint. (0:off 1:on) |
salatron | 0:440732350fa8 | 29 | Ticker timeout; //Create an instance of class Ticker called timeout. |
salatron | 0:440732350fa8 | 30 | |
salatron | 0:440732350fa8 | 31 | int main() |
salatron | 0:440732350fa8 | 32 | { |
salatron | 0:440732350fa8 | 33 | timeout.attach_us(&attimeout, 500000); // Set up interrupt to call attimeout() every half a second. |
salatron | 0:440732350fa8 | 34 | for(;;) |
salatron | 0:440732350fa8 | 35 | { |
salatron | 0:440732350fa8 | 36 | segmentled.SevenSegLed_main(D_7seg, D_dot); // Keep the displays multiplexed. |
salatron | 0:440732350fa8 | 37 | } |
salatron | 0:440732350fa8 | 38 | } |
salatron | 0:440732350fa8 | 39 | |
salatron | 0:440732350fa8 | 40 | void attimeout() //Timer interrupt routine. |
salatron | 0:440732350fa8 | 41 | { |
salatron | 0:440732350fa8 | 42 | mDot = 1 - mDot; |
salatron | 0:440732350fa8 | 43 | if (mDot == 1) |
salatron | 0:440732350fa8 | 44 | { |
salatron | 0:440732350fa8 | 45 | mUnits++; // This means the same as D_7seg[0] = D_7seg[0] + 1; Increment "units" |
salatron | 0:440732350fa8 | 46 | |
salatron | 0:440732350fa8 | 47 | if (mUnits > 9) |
salatron | 0:440732350fa8 | 48 | { // "units" digit should be in the range 0 ‐> 9 |
salatron | 0:440732350fa8 | 49 | mUnits = 0; // Reset the "units" to 0 |
salatron | 0:440732350fa8 | 50 | mTens++; // Increment "tens" digit |
salatron | 0:440732350fa8 | 51 | |
salatron | 0:440732350fa8 | 52 | if (mTens > 9) |
salatron | 0:440732350fa8 | 53 | { // "tens" digit should be in the range 0 ‐> 9 |
salatron | 0:440732350fa8 | 54 | mTens = 0; // Reset the "tens" to 0 |
salatron | 0:440732350fa8 | 55 | } |
salatron | 0:440732350fa8 | 56 | } |
salatron | 0:440732350fa8 | 57 | } |
salatron | 0:440732350fa8 | 58 | } |