7 Segment display counter
Dependencies: SevenSegLed mbed
main.cpp@0:134a896bf705, 2013-11-14 (annotated)
- Committer:
- jf1452
- Date:
- Thu Nov 14 14:21:22 2013 +0000
- Revision:
- 0:134a896bf705
First code issue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jf1452 | 0:134a896bf705 | 1 | #include "mbed.h" //Include the mbed header file which contains the prototypes of the libraries used here (DigitalOut, Ticker) as well as various syntax definitions. |
jf1452 | 0:134a896bf705 | 2 | #include "SevenSegLed.h" //Include the SevenSegLed header file which contains the class for the SevenSegLed output library. |
jf1452 | 0:134a896bf705 | 3 | |
jf1452 | 0:134a896bf705 | 4 | // Lets define some "macros" to make the code easier to read |
jf1452 | 0:134a896bf705 | 5 | |
jf1452 | 0:134a896bf705 | 6 | #define mUnits D_7seg[1] // When the compiler finds "units" it will substiute it for "D_7seg[1]" |
jf1452 | 0:134a896bf705 | 7 | #define mTens D_7seg[0] // When the compiler finds "units" it will substiute it for "D_7seg[1]" |
jf1452 | 0:134a896bf705 | 8 | #define mDot D_dot[1] // When the compiler finds "units" it will substiute it for "D_7seg[1]" |
jf1452 | 0:134a896bf705 | 9 | |
jf1452 | 0:134a896bf705 | 10 | void attimeout(); //prototype declared above main function to avoid errors. |
jf1452 | 0:134a896bf705 | 11 | |
jf1452 | 0:134a896bf705 | 12 | //Below is the creation of an instance of SevenSegLed called segmentled. |
jf1452 | 0:134a896bf705 | 13 | //It is constructed with the information relating to the pins which are used, the type of display (common anode or cathode) and |
jf1452 | 0:134a896bf705 | 14 | //the type of transition between outputs. Display mode = smooth will fade the new number in while hard will just change it immediately. |
jf1452 | 0:134a896bf705 | 15 | //com1 and com2 are the pins which power each display. As we only use one pin, com1 is filled with any unused pin and com2 is filled with |
jf1452 | 0:134a896bf705 | 16 | //the pin that is used for MUX. As com2 will go low when not in use the other display will be selected and the MUX effect will still work. |
jf1452 | 0:134a896bf705 | 17 | // |
jf1452 | 0:134a896bf705 | 18 | // common type (0:anode common 1:cathode common) |
jf1452 | 0:134a896bf705 | 19 | // | |
jf1452 | 0:134a896bf705 | 20 | // | display mode (0:smooth 1:hard) |
jf1452 | 0:134a896bf705 | 21 | // | | |
jf1452 | 0:134a896bf705 | 22 | // | | segA segB segC segD segE segF segG segP com1 com2 |
jf1452 | 0:134a896bf705 | 23 | // | | | | | | | | | | | | |
jf1452 | 0:134a896bf705 | 24 | SevenSegLed segmentled(0, 0, P1_23, P1_28, P0_16, P1_31, P1_13, P1_16, P1_19, P0_23, p21, P1_25); |
jf1452 | 0:134a896bf705 | 25 | |
jf1452 | 0:134a896bf705 | 26 | //Define two arrays to house the data to be outputted. D_7seg can contain digits 0-F and D_dot 0-1. |
jf1452 | 0:134a896bf705 | 27 | // |
jf1452 | 0:134a896bf705 | 28 | // 0 1 //0 = leftmost digit, 1 = rightmost digit |
jf1452 | 0:134a896bf705 | 29 | // | | |
jf1452 | 0:134a896bf705 | 30 | uint8_t D_7seg[2] = {0, 0}; // seven segment digit number (0x00:"0", 0x01:"1", ... , 0x09:"9", 0x0A:"A", ... , 0x0F:"F", other:" ") |
jf1452 | 0:134a896bf705 | 31 | uint8_t D_dot[2] = {0, 0}; // seven segment digit dotpoint. (0:off 1:on) |
jf1452 | 0:134a896bf705 | 32 | |
jf1452 | 0:134a896bf705 | 33 | Ticker timeout; //Create an instance of class Ticker called timeout. |
jf1452 | 0:134a896bf705 | 34 | |
jf1452 | 0:134a896bf705 | 35 | int main() |
jf1452 | 0:134a896bf705 | 36 | { |
jf1452 | 0:134a896bf705 | 37 | |
jf1452 | 0:134a896bf705 | 38 | timeout.attach_us(&attimeout, 500000); //Call attach function in timeout instance to call attimeout() every 500,000us (half a second). |
jf1452 | 0:134a896bf705 | 39 | |
jf1452 | 0:134a896bf705 | 40 | for(;;) { |
jf1452 | 0:134a896bf705 | 41 | segmentled.SevenSegLed_main(D_7seg, D_dot); //Repeatedly call the Segment output function to keep the displays multiplexed. |
jf1452 | 0:134a896bf705 | 42 | } |
jf1452 | 0:134a896bf705 | 43 | } |
jf1452 | 0:134a896bf705 | 44 | |
jf1452 | 0:134a896bf705 | 45 | void attimeout() //Timer interrupt routine. |
jf1452 | 0:134a896bf705 | 46 | { |
jf1452 | 0:134a896bf705 | 47 | mDot = 1 - mDot; |
jf1452 | 0:134a896bf705 | 48 | if (mDot == 1) { |
jf1452 | 0:134a896bf705 | 49 | mUnits++; // This means the same as D_7seg[0] = D_7seg[0] + 1; Increment "units" digit |
jf1452 | 0:134a896bf705 | 50 | if (mUnits > 9) { // "units" digit should be in the range 0 -> 9 |
jf1452 | 0:134a896bf705 | 51 | mUnits = 0; // Reset the "units" to 0 |
jf1452 | 0:134a896bf705 | 52 | mTens++; // Increment "tens" digit |
jf1452 | 0:134a896bf705 | 53 | if (mTens > 9) { // "tens" digit should be in the range 0 -> 9 |
jf1452 | 0:134a896bf705 | 54 | mTens = 0; // Reset the "tens" to 0 |
jf1452 | 0:134a896bf705 | 55 | } |
jf1452 | 0:134a896bf705 | 56 | } |
jf1452 | 0:134a896bf705 | 57 | } |
jf1452 | 0:134a896bf705 | 58 | } |