Dynamic Lighting Lib. of 7Seg-LED (add DP) x4. Features: - Unified Anode/Cathode and NPN/PNP sw. - Non-use wait(); Useing Interrupt Timer. - Modulates Brightness with PWM. - Display Hex. - Using BusOut(s) of Digits(4) and Segments(8). - Sinmple Interface of Class.
LED7segX4.h
00001 /** 00002 * Dynamic Lighting Lib. of 7Seg-LED (add DP) x4. 00003 * 00004 * Features: 00005 * - Unified Anode/Cathode common 7Segs, and NPN/PNP switchings. 00006 * - Using BusOut(s) of Digits(0-3) and Segments(A-G,DP). 00007 * - Simple Interface of Class. 00008 * - Non-use wait(); Useing Interrupt Timer: Ticker and Timeut. 00009 * - Modulates Brightness; You Can Control of Periodic-Time and Duty. 00010 * - Display Hex(0-9,A-F) of 4 Digit; Using char[4]. 00011 */ 00012 00013 /** @code 00014 #include "mbed.h" 00015 00016 #include "LED7segX4.h" 00017 00018 // port make. 00019 BusOut pDgit(p21, p22, p23, p24); // D1, D2, D3, D4 00020 BusOut pSeg(p11, p12, p13, p14, p15, p16, p17, p18); // A, B, C, D, E, F, G, DP 00021 00022 // Make instance of 7Seg-class. 00023 LED7segX4 led7seg(pDgit, pSeg); 00024 00025 // for Operation check. 00026 DigitalOut myled(LED1); 00027 00028 int main() { 00029 00030 char val[4]; 00031 char dp; 00032 int duty; 00033 00034 // In this case: Anode Common, PNP swiching for Dig-common, 00035 // and sink port(BusOut) for seg. 00036 led7seg.init(true, true, 1000, 40); 00037 00038 val[0]= 0; 00039 val[1]= 0; 00040 val[2]= 0; 00041 val[3]= 0; 00042 00043 led7seg.set7seg(val); 00044 led7seg.setDP(0b0001); 00045 00046 while(true) { 00047 myled = !myled; 00048 wait_ms(30); 00049 00050 if(++duty > 100) 00051 duty= 30; 00052 00053 val[3] += 1; 00054 for(int i= 0; i < 3; i++){ 00055 if(val[3-i] > 15){ 00056 if(i < 3){ 00057 val[3-i]= 0; 00058 val[2-i] += 1; 00059 } 00060 dp += 1; 00061 } 00062 } 00063 if(val[0]==15 && val[1]==15 && val[2]==15 && val[3]==15){ 00064 // = FFFF 00065 val[0]= 0; 00066 val[1]= 0; 00067 val[2]= 0; 00068 val[3]= 0; 00069 } 00070 if(dp > 0b1111) 00071 dp= 0; 00072 00073 led7seg.set7seg(val); 00074 led7seg.setDP(dp); 00075 led7seg.setPWM(1000, duty); 00076 00077 } 00078 00079 return 0; 00080 } 00081 * @endcode 00082 */ 00083 00084 #pragma once 00085 00086 #include "mbed.h" 00087 00088 class LED7segX4{ 00089 public: 00090 00091 /** Constructor 00092 * @param BusOut; _portDig: Bus4 of Digit(3~0) 00093 * @param BusOut; _portSeg: Bus8 of Segment(a~g+dp) 00094 */ 00095 LED7segX4(BusOut &portDig, BusOut &portSeg); 00096 00097 ~LED7segX4(); 00098 00099 00100 /** Initialize 00101 * @param bool; dig: Reverce(H/L) Digit(3~0) 00102 * @param bool; seg: Reverce(H/L) Segment(a~g+dp) 00103 * @param int; timePeriod[us]: time of priend in PWM. 00104 * @param int; duty[%]: duty of PWM. 00105 */ 00106 void init(bool revDig= false, bool revSeg= false, int timePeriod= 1000, int duty= 80); 00107 00108 00109 /** set seg TYPE; 00110 * @param bool; dig: Digit(3~0) 00111 * @param bool; seg: Segment(a~g+dp) 00112 */ 00113 void setReverse(bool dig, bool seg); 00114 00115 /** Set 7seg. 00116 * @param chr[4]; D3~D1, 0x00(0)~0x15(F) and 0x16(OFF/NULL). 00117 */ 00118 void set7seg(char chr[]); 00119 00120 /** Set DotPoint. 00121 * @param chr; 0b0000~0b1111, bit0~3; TRUE=1. 00122 */ 00123 void setDP(char chr); 00124 00125 /** Set PWM (Britness). 00126 * @param int; periodic time [us]. "for Each Digit." 00127 * @param int; duty ratio;1~100[%] in timePeriod. 00128 * ex) In case of 1,000us and 60%, 00129 */ 00130 void setPWM(int timePeriod, int duty); 00131 00132 00133 00134 private: 00135 00136 // ********************** Variable ****************************** 00137 00138 // pin/port set 00139 BusOut &_portDig; 00140 BusOut &_portSeg; 00141 00142 // TimerIRQ 4 PWM 00143 Ticker ticker; // Duty : Bright 00144 Timeout timeout; // Duty : OFF 00145 float _dutySec; // Duty(Bright) Time-width [s], in 1 period. 00146 00147 // Define 00148 char _def7seg[17]; // Define Segment; 0~f + 16:OFF(Null) 00149 00150 00151 // Local Hold Val 00152 char _seg7[5]; // 4+Null ex) 2.3E5 -> 2, 3, 14, 5 00153 char _dp; // 0b0000~0b1111; ex) 12.3E. -> 0b0101 00154 bool _revDig; // REVERSE H/L 4 Transistor swDigit 00155 bool _revSeg; // AND swSeg 00156 int _currentDigit; // current digit-Num (0~3). This shifts in timeperiod of PWM. 00157 00158 00159 // ********************** Internal Function ********************* 00160 void init_def7seg(); // Set/Define of seg pattern. 00161 00162 // For DUTY of PWM. ON(Shift) and OFF. 00163 void OnSet(); 00164 void OffSet(); 00165 00166 }; 00167 00168 // EOF
Generated on Fri Jul 15 2022 14:46:57 by
