Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 7SegmentDisplay by
main.cpp@2:3cf4eba9de56, 2016-09-04 (annotated)
- Committer:
- captaintim
- Date:
- Sun Sep 04 11:54:00 2016 +0000
- Revision:
- 2:3cf4eba9de56
- Parent:
- 1:46dbd77e0701
- Child:
- 3:c747b832518f
displays decimal number to a 4 digit, 7segment display (LDQ-N514R1), reveal how to finesse digits out one at a time for display, and flash display fast enough to show decimal number
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ShingyoujiPai | 0:463ff11d33fa | 1 | #include "mbed.h" |
| ShingyoujiPai | 0:463ff11d33fa | 2 | |
| captaintim | 1:46dbd77e0701 | 3 | /*test program to learn how to write code for a 4 digit, 7-segment LED display LDQ-N524R1 |
| captaintim | 1:46dbd77e0701 | 4 | The schematic for this (COMMON CATHODE) display shows the following connections |
| captaintim | 1:46dbd77e0701 | 5 | schematic located at http://www.lumex.com/ldq-n514ri (open Specs PDF for drawing) |
| captaintim | 1:46dbd77e0701 | 6 | The Common Cathode means that to turn on the segments of the 7-segment (including decimal point) |
| captaintim | 1:46dbd77e0701 | 7 | you write a one to that segment. |
| captaintim | 1:46dbd77e0701 | 8 | For this 4 digit display (LDQ-N524R1), each digit works backward--like its wired Common Anode so |
| captaintim | 1:46dbd77e0701 | 9 | You write a ZERO to turn on the selected digit AND a ONE to turn off the digit. |
| captaintim | 1:46dbd77e0701 | 10 | |
| captaintim | 1:46dbd77e0701 | 11 | Pin Out wiring guide:(connect the display pin # to XX mbed pin) |
| ShingyoujiPai | 0:463ff11d33fa | 12 | |
| captaintim | 1:46dbd77e0701 | 13 | CONTROL DISPLAY Pin# MBED Pin# |
| captaintim | 1:46dbd77e0701 | 14 | ---------------------------------- |
| captaintim | 1:46dbd77e0701 | 15 | Digit1 12 14 |
| captaintim | 1:46dbd77e0701 | 16 | Digit2 9 13 |
| captaintim | 1:46dbd77e0701 | 17 | Digit3 8 12 |
| captaintim | 1:46dbd77e0701 | 18 | Digit4 6 11 |
| captaintim | 1:46dbd77e0701 | 19 | DP 3 10 |
| captaintim | 1:46dbd77e0701 | 20 | A 11 21 |
| captaintim | 1:46dbd77e0701 | 21 | B 7 22 |
| captaintim | 1:46dbd77e0701 | 22 | C 4 23 |
| captaintim | 1:46dbd77e0701 | 23 | D 2 24 |
| captaintim | 1:46dbd77e0701 | 24 | E 1 25 |
| captaintim | 1:46dbd77e0701 | 25 | F 5 26 |
| captaintim | 1:46dbd77e0701 | 26 | G 5 27 |
| captaintim | 1:46dbd77e0701 | 27 | |
| captaintim | 1:46dbd77e0701 | 28 | There is no blanking on this display, |
| captaintim | 2:3cf4eba9de56 | 29 | Program demonstrates writing to all four digits of the 7-segment LED display of the LDQ-N514R1 a decimal number |
| captaintim | 2:3cf4eba9de56 | 30 | Since the segments only light up for one selected digit, all 4 digit has to be flashed to show 4 separate numbers |
| captaintim | 2:3cf4eba9de56 | 31 | Television used this by flashing 30 screens per second call persistence enhanced cathode ray tubes. |
| ShingyoujiPai | 0:463ff11d33fa | 32 | |
| captaintim | 1:46dbd77e0701 | 33 | Author: Cap'n Tim Johnson PE |
| captaintim | 1:46dbd77e0701 | 34 | Retired Professor |
| captaintim | 1:46dbd77e0701 | 35 | Wentworth Institude of Technology |
| captaintim | 1:46dbd77e0701 | 36 | Dept. Electrical Engineering and Technology |
| captaintim | 1:46dbd77e0701 | 37 | Boston, MA |
| captaintim | 1:46dbd77e0701 | 38 | */ |
| captaintim | 1:46dbd77e0701 | 39 | |
| captaintim | 1:46dbd77e0701 | 40 | //Setup: |
| captaintim | 1:46dbd77e0701 | 41 | DigitalOut Digit1(p14); //construct to control digits |
| captaintim | 1:46dbd77e0701 | 42 | DigitalOut Digit2(p13); |
| captaintim | 1:46dbd77e0701 | 43 | DigitalOut Digit3(p12); |
| captaintim | 1:46dbd77e0701 | 44 | DigitalOut Digit4(p11); |
| captaintim | 2:3cf4eba9de56 | 45 | DigitalOut myled (LED1); //Signal used for runtime checking |
| captaintim | 1:46dbd77e0701 | 46 | |
| captaintim | 1:46dbd77e0701 | 47 | //these are the pins associated with writing to the "led" |
| captaintim | 1:46dbd77e0701 | 48 | DigitalOut led[8]={p21, p22, p23, p24, p25, p26, p27, p10}; |
| captaintim | 1:46dbd77e0701 | 49 | |
| captaintim | 1:46dbd77e0701 | 50 | //segments are in alphabetical order a-g, followed by Decimal point in the array below |
| captaintim | 1:46dbd77e0701 | 51 | |
| captaintim | 1:46dbd77e0701 | 52 | int matrix[11][8]={ |
| captaintim | 1:46dbd77e0701 | 53 | {1,1,1,1,1,1,0,0}, //zero |
| captaintim | 1:46dbd77e0701 | 54 | {0,1,1,0,0,0,0,0}, //one |
| captaintim | 1:46dbd77e0701 | 55 | {1,1,0,1,1,0,1,0}, //two |
| captaintim | 1:46dbd77e0701 | 56 | {1,1,1,1,0,0,1,0}, //three |
| captaintim | 1:46dbd77e0701 | 57 | {0,1,1,0,0,1,1,0}, //four |
| captaintim | 1:46dbd77e0701 | 58 | {1,0,1,1,0,1,1,0}, //five |
| captaintim | 1:46dbd77e0701 | 59 | {1,0,1,1,1,1,1,0}, //six |
| captaintim | 1:46dbd77e0701 | 60 | {1,1,1,0,0,0,0,0}, //seven |
| ShingyoujiPai | 0:463ff11d33fa | 61 | {1,1,1,1,1,1,1,0}, //eight |
| captaintim | 1:46dbd77e0701 | 62 | {1,1,1,0,0,1,1,0}, //nine |
| ShingyoujiPai | 0:463ff11d33fa | 63 | {0,0,0,0,0,0,0,1} //dot |
| ShingyoujiPai | 0:463ff11d33fa | 64 | }; |
| ShingyoujiPai | 0:463ff11d33fa | 65 | |
| captaintim | 2:3cf4eba9de56 | 66 | //create matrix to handle decimal point |
| captaintim | 2:3cf4eba9de56 | 67 | int matrixdecimal[11][8]={ |
| captaintim | 2:3cf4eba9de56 | 68 | {1,1,1,1,1,1,0,1}, //zero |
| captaintim | 2:3cf4eba9de56 | 69 | {0,1,1,0,0,0,0,1}, //one |
| captaintim | 2:3cf4eba9de56 | 70 | {1,1,0,1,1,0,1,1}, //two |
| captaintim | 2:3cf4eba9de56 | 71 | {1,1,1,1,0,0,1,1}, //three |
| captaintim | 2:3cf4eba9de56 | 72 | {0,1,1,0,0,1,1,1}, //four |
| captaintim | 2:3cf4eba9de56 | 73 | {1,0,1,1,0,1,1,1}, //five |
| captaintim | 2:3cf4eba9de56 | 74 | {1,0,1,1,1,1,1,1}, //six |
| captaintim | 2:3cf4eba9de56 | 75 | {1,1,1,0,0,0,0,1}, //seven |
| captaintim | 2:3cf4eba9de56 | 76 | {1,1,1,1,1,1,1,1}, //eight |
| captaintim | 2:3cf4eba9de56 | 77 | {1,1,1,0,0,1,1,1}, //nine |
| captaintim | 2:3cf4eba9de56 | 78 | {0,0,0,0,0,0,0,1} //dot...actually a leading null space |
| captaintim | 2:3cf4eba9de56 | 79 | }; |
| ShingyoujiPai | 0:463ff11d33fa | 80 | |
| captaintim | 2:3cf4eba9de56 | 81 | //set persistance (wait value on digits or flash rate) |
| captaintim | 2:3cf4eba9de56 | 82 | float f = 0.005; |
| captaintim | 2:3cf4eba9de56 | 83 | //enter a float number here (with a decimal in it) starting point |
| captaintim | 2:3cf4eba9de56 | 84 | volatile float temp = 31.45; |
| captaintim | 2:3cf4eba9de56 | 85 | |
| captaintim | 2:3cf4eba9de56 | 86 | int main(){ |
| captaintim | 2:3cf4eba9de56 | 87 | //finessing the digits out one at a time knowing 2nd digit has the decimal |
| captaintim | 2:3cf4eba9de56 | 88 | int tempasinteger = temp * 100; //removes decimal and typecasts float to integer |
| captaintim | 2:3cf4eba9de56 | 89 | int temp1a=tempasinteger/1000; //typecast float to integer for 1st digit (was) |
| captaintim | 2:3cf4eba9de56 | 90 | int temp2 = tempasinteger - temp1a*1000; //gets last 3 digits |
| captaintim | 2:3cf4eba9de56 | 91 | int temp2a = temp2/100; //typecast float to integer for 2nd digit |
| captaintim | 2:3cf4eba9de56 | 92 | int temp3 = temp2 - temp2a*100; //get last 2 digits |
| captaintim | 2:3cf4eba9de56 | 93 | |
| captaintim | 2:3cf4eba9de56 | 94 | int temp3a=temp3/10; //typecast float to integer for 3rd digit |
| captaintim | 2:3cf4eba9de56 | 95 | int temp4=temp3-temp3a*10; //gets last digit |
| captaintim | 2:3cf4eba9de56 | 96 | int temp4a = temp4; //convenient renaming for writing digit to display |
| captaintim | 2:3cf4eba9de56 | 97 | |
| captaintim | 2:3cf4eba9de56 | 98 | //begin one write of the display by turning off all the digits |
| captaintim | 1:46dbd77e0701 | 99 | Digit1 = 1; //turn off digit1 |
| captaintim | 1:46dbd77e0701 | 100 | Digit2 = 1; //turn off digit2 |
| captaintim | 1:46dbd77e0701 | 101 | Digit3 = 1; //turn off digit3 |
| captaintim | 1:46dbd77e0701 | 102 | Digit4 = 1; //turn off digit4 |
| captaintim | 2:3cf4eba9de56 | 103 | while (1) { |
| captaintim | 2:3cf4eba9de56 | 104 | //turns off last led's segments values |
| captaintim | 1:46dbd77e0701 | 105 | for(int i = 0; i<8;i++){ |
| captaintim | 2:3cf4eba9de56 | 106 | led[i] = 0;} |
| captaintim | 2:3cf4eba9de56 | 107 | //belows holds row of matrix and assign column value from matrix |
| captaintim | 2:3cf4eba9de56 | 108 | Digit1 = 0; //turns on digit1 |
| captaintim | 2:3cf4eba9de56 | 109 | for (int i=0; i<8; i++){ |
| captaintim | 2:3cf4eba9de56 | 110 | led[i] = matrix[temp1a][i]; |
| captaintim | 2:3cf4eba9de56 | 111 | } |
| captaintim | 2:3cf4eba9de56 | 112 | wait(f); |
| captaintim | 2:3cf4eba9de56 | 113 | Digit1=1; |
| ShingyoujiPai | 0:463ff11d33fa | 114 | |
| captaintim | 2:3cf4eba9de56 | 115 | //turns off last led's segments values |
| captaintim | 2:3cf4eba9de56 | 116 | for(int i = 0; i<8;i++){ |
| captaintim | 2:3cf4eba9de56 | 117 | led[i] = 0;} |
| captaintim | 2:3cf4eba9de56 | 118 | //belows holds row of matrix and assign column value from matrix |
| captaintim | 2:3cf4eba9de56 | 119 | Digit2 = 0; //turns on digit2 with decimal |
| captaintim | 1:46dbd77e0701 | 120 | for (int i=0; i<8; i++){ |
| captaintim | 2:3cf4eba9de56 | 121 | //since we have prior knowledge of the decimal location |
| captaintim | 2:3cf4eba9de56 | 122 | //and this is a demonstration program, we add the decimal for this digit |
| captaintim | 2:3cf4eba9de56 | 123 | led[i] = matrixdecimal[temp2a][i]; |
| captaintim | 2:3cf4eba9de56 | 124 | } |
| captaintim | 2:3cf4eba9de56 | 125 | wait(f); |
| captaintim | 2:3cf4eba9de56 | 126 | Digit2=1; |
| captaintim | 2:3cf4eba9de56 | 127 | |
| captaintim | 2:3cf4eba9de56 | 128 | //turns off last led's segments values |
| captaintim | 2:3cf4eba9de56 | 129 | for(int i = 0; i<8;i++){ |
| captaintim | 2:3cf4eba9de56 | 130 | led[i] = 0;} |
| captaintim | 2:3cf4eba9de56 | 131 | //belows holds row of matrix and assign column value from matrix |
| captaintim | 2:3cf4eba9de56 | 132 | Digit3 = 0; //turns on digit3 |
| captaintim | 2:3cf4eba9de56 | 133 | for (int i=0; i<8; i++){ |
| captaintim | 2:3cf4eba9de56 | 134 | led[i] = matrix[temp3a][i]; |
| captaintim | 2:3cf4eba9de56 | 135 | } |
| captaintim | 2:3cf4eba9de56 | 136 | wait(f); |
| captaintim | 2:3cf4eba9de56 | 137 | Digit3=1; |
| captaintim | 2:3cf4eba9de56 | 138 | |
| captaintim | 2:3cf4eba9de56 | 139 | //turns off last led's segments values |
| captaintim | 2:3cf4eba9de56 | 140 | for(int i = 0; i<8;i++){ |
| captaintim | 2:3cf4eba9de56 | 141 | led[i] = 0;} |
| captaintim | 2:3cf4eba9de56 | 142 | //belows holds row of matrix and assign column value from matrix |
| captaintim | 2:3cf4eba9de56 | 143 | Digit4 = 0; //turns on digit4 |
| captaintim | 2:3cf4eba9de56 | 144 | for (int i=0; i<8; i++){ |
| captaintim | 2:3cf4eba9de56 | 145 | led[i] = matrix[temp4a][i]; } |
| captaintim | 2:3cf4eba9de56 | 146 | wait(f); |
| captaintim | 2:3cf4eba9de56 | 147 | Digit4=1; |
| captaintim | 2:3cf4eba9de56 | 148 | |
| captaintim | 2:3cf4eba9de56 | 149 | } |
| ShingyoujiPai | 0:463ff11d33fa | 150 | } |
