このライブラリは1桁から8桁までのSeven segment Numeric LEDを制御します。 LEDはanode commonとcathode common を使用することができます。 LEDの表示は1秒で表示をスムースに切り替えるモードと、直ぐに切り替えるモードの2つのモードを選択することができます。 This library to control the Seven segment Numeric LED 8 digit of 1. You can use the LED cathode common and anode common. Switch mode LED display and a second displayed a smooth, you can choose two modes to switch modes quickly.
Dependents: kitchenTimer_Clock kitchenTimer LPC1114FN28_kitchenTimer_Clock SevenSegmentLedSample ... more
Revision 0:5410d6e2bce7, committed 2011-11-20
- Comitter:
- suupen
- Date:
- Sun Nov 20 00:22:05 2011 +0000
- Child:
- 1:3429249e30f9
- Commit message:
- V1.1 2011/11/20 This has changed the name of the library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SevenSegLed.cpp Sun Nov 20 00:22:05 2011 +0000 @@ -0,0 +1,272 @@ +/********************************************************** + +* SevenSegLed.cpp +* dynamic control of seven segment led +* +**********************************************************/ +#define _SEVENSEGLED_C + +#include "types.h" +#include "mbed.h" +#include "SevenSegLed.h" + + + + +/** Create a seven segment led object connected to the specified DigtalOutput pin + */ +SevenSegLed::SevenSegLed(uint8_t commonPole, uint8_t smooth, PinName seg_a, PinName seg_b, PinName seg_c, PinName seg_d, PinName seg_e, PinName seg_f, PinName seg_g, PinName seg_p, PinName com_1, PinName com_2, PinName com_3, PinName com_4): + _seg_a(seg_a), _seg_b(seg_b), _seg_c(seg_c), _seg_d(seg_d), _seg_e(seg_e), _seg_f(seg_f), _seg_g(seg_g), _seg_p(seg_p), + _com_1(com_1), _com_2(com_2), _com_3(com_3), _com_4(com_4){ + + + timer.attach_us(this, &SevenSegLed::segmentGrayDataKosin, 10000); // led smooth control 10ms timer inttruupt + + // data table set of Brightness + // DT_pwmGray[] = i ^ 2 + for(uint32_t i = 0; i < Z_grayMax + 1; i++){ + DT_pwmGray[i] = (uint8_t)(((i * i) * Z_pwmGrayMax ) / (Z_grayMax * Z_grayMax)); + } + + // check connect com_x + D_comNull = Z_ketaSuu; + if(com_4 == NC){D_comNull--;} + if(com_3 == NC){D_comNull--;} + if(com_2 == NC){D_comNull--;} + if(com_1 == NC){D_comNull--;} + + // Those who will be reading the LED display + D_smooth = smooth; + + // common and segment pin display data set + if(commonPole == 0){ + // Anode common + D_commonOn = 1; + D_commonOff = 0; + D_segmentOn = 0; + D_segmentOff = 1; + } + else{ + // Cathod common + D_commonOn = 0; + D_commonOff = 1; + D_segmentOn = 1; + D_segmentOff = 0; + } + +} + + + +/************************************** +* 7segment no gray data kosin +* 100ms goto no syori +**************************************/ +void SevenSegLed::segmentGrayDataKosin(void){ + uint8_t keta; + uint8_t seg; + + uint8_t segMask; + uint8_t segData; + + + //********************************************************* + // 7segment no shuturyoku pattern + //********************************************************* + // seg: a b c d e f g p + // bit: 7 6 5 4 3 2 1 0 + // --------------------- + #define D_0 (0xfc) // 0 1 1 1 1 1 1 0 0 + #define D_1 (0x60) // 1 0 1 1 0 0 0 0 0 + #define D_2 (0xda) // 2 1 1 0 1 1 0 1 0 + #define D_3 (0xf2) // 3 1 1 1 1 0 0 1 0 + #define D_4 (0x66) // 4 0 1 1 0 0 1 1 0 + #define D_5 (0xb6) // 5 1 0 1 1 0 1 1 0 + #define D_6 (0xbe) // 6 1 0 1 1 1 1 1 0 + #define D_7 (0xe4) // 7 1 1 1 0 0 1 0 0 + #define D_8 (0xfe) // 8 1 1 1 1 1 1 1 0 + #define D_9 (0xf6) // 9 1 1 1 1 0 1 1 0 + #define D_A (0xee) // A 1 1 1 0 1 1 1 0 + #define D_b (0x3e) // b 0 0 1 1 1 1 1 0 + #define D_C (0x9c) // C 1 0 0 1 1 1 0 0 + #define D_d (0x7a) // d 0 1 1 1 1 0 1 0 + #define D_E (0x9e) // E 1 0 0 1 1 1 1 0 + #define D_F (0x8e) // F 1 0 0 0 1 1 1 0 + #define D_NULL (0x00) // NULL 0 0 0 0 0 0 0 0 + // (No indication) + + + const unsigned char DT_segData[17] = {D_0, D_1, D_2, D_3, D_4, D_5, D_6, D_7, D_8, D_9, D_A, D_b, D_C, D_d, D_E, D_F, D_NULL}; + uint8_t work; + + for(keta = 0; keta < Z_ketaSuu; keta++){ + // number data set + work = D_7seg[keta]; + if(work > 0x10){work = 0x10;} // error data then NULL + segData = DT_segData[work]; + + // dot data set + if(D_dot[keta] != 0){segData |= 0x01;} + + // segment data set + segMask = 0x80; + + for(seg = 0; seg < Z_segSuu; seg++){ + if(D_smooth == Z_smooth){ + // LED display Smooth + if((segData & segMask) != 0){ + // segment tento + if(D_7segGray[keta][seg] < Z_grayMax){D_7segGray[keta][seg]++;} + } + else{ + // segment syoto + if(D_7segGray[keta][seg] > 0){D_7segGray[keta][seg]--;} + } + segMask = segMask >> 1; + } + else{ + // LED display Hard + if((segData & segMask) != 0){ + // segment tento + D_7segGray[keta][seg] = Z_pwmGrayMax; + } + else{ + // segment syoto + D_7segGray[keta][seg] = 0; + } + segMask = segMask >> 1; + } + } + } +} + + +/************************************** +* main +**************************************/ +void SevenSegLed::SevenSegLed_main(uint8_t* number, uint8_t* dot) { + + for(uint8_t i = 0; i < Z_ketaSuu; i++){ + D_7seg[i] = number[i]; + D_dot[i] = dot[i]; + } + + + // dynamic shuturyoku shori + output(); +} + + +/************************************** +* comAllClear +* +* common pin o subete OFF suru +**************************************/ +void SevenSegLed::comAllClear(void){ + + switch (D_comNull){ + case 4: // com_1 - com_4 is all connect + _com_4 = D_commonOff; + //break; + case 3: // com_4 Null + _com_3 = D_commonOff; + // break; + case 2: // com_3 Null + _com_2 = D_commonOff; + //break; + case 1: // com_2 Null + _com_1 = D_commonOff; + //break; + case 0: // com_1 Null + // nothing + break; + default: + // nothing + break; + } +} + +/************************************** +* segAllClear +* +* segment pin o subete OFF suru +**************************************/ +void SevenSegLed::segAllClear(void){ + _seg_a = D_segmentOff; + _seg_b = D_segmentOff; + _seg_c = D_segmentOff; + _seg_d = D_segmentOff; + _seg_e = D_segmentOff; + _seg_f = D_segmentOff; + _seg_p = D_segmentOff; +} + +/************************************** +* segDataSet +* +* segment pin ni shuturyoku data o settei +**************************************/ +void SevenSegLed::segDataSet(uint8_t keta){ + + for(uint8_t i = 0; i < Z_pwmGrayMax + 1; i++){ + if(DT_pwmGray[D_7segGray[keta][0]] <= i){_seg_a = D_segmentOff;}else{_seg_a = D_segmentOn;} + if(DT_pwmGray[D_7segGray[keta][1]] <= i){_seg_b = D_segmentOff;}else{_seg_b = D_segmentOn;} + if(DT_pwmGray[D_7segGray[keta][2]] <= i){_seg_c = D_segmentOff;}else{_seg_c = D_segmentOn;} + if(DT_pwmGray[D_7segGray[keta][3]] <= i){_seg_d = D_segmentOff;}else{_seg_d = D_segmentOn;} + if(DT_pwmGray[D_7segGray[keta][4]] <= i){_seg_e = D_segmentOff;}else{_seg_e = D_segmentOn;} + if(DT_pwmGray[D_7segGray[keta][5]] <= i){_seg_f = D_segmentOff;}else{_seg_f = D_segmentOn;} + if(DT_pwmGray[D_7segGray[keta][6]] <= i){_seg_g = D_segmentOff;}else{_seg_g = D_segmentOn;} + if(DT_pwmGray[D_7segGray[keta][7]] <= i){_seg_p = D_segmentOff;}else{_seg_p = D_segmentOn;} + + } +} + +/************************************** +* output +* +* dynamic dosa saseru. +* kono kansu wo jiikou suru tabi ni common pin o kirikaeru +**************************************/ +void SevenSegLed::output(void){ + static uint8_t M_seg = 0; + + if(M_seg >= D_comNull){M_seg = 0;} + + // com, seg syokika + comAllClear(); + segAllClear(); + + + + // common output + if(D_comNull != 0){ + // If the terminal output processing + switch(M_seg){ + case 0: + _com_1 = D_commonOn; + break; + case 1: + _com_2 = D_commonOn; + break; + case 2: + _com_3 = D_commonOn; + break; + case 3: + _com_4 = D_commonOn; + break; + default: + break; + } + } + + // segmant output + if(M_seg < Z_ketaSuu){ + segDataSet(M_seg); + } + + + // com, seg syokika + comAllClear(); + segAllClear(); + M_seg++; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SevenSegLed.h Sun Nov 20 00:22:05 2011 +0000 @@ -0,0 +1,185 @@ +/* mbed Seven segment LED (Maximum four digit) Library + * Copyright (c) 2011 suupen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +/***********************************************************************/ +/* */ +/* ledDynamic.h */ +/* */ +/***********************************************************************/ +#ifndef _SEVENSEGLED_H +#define _SEVENSEGLED_H + +/** Seven segment Numeric LED control class + * + * Example: + * @code + * //----------------------------------------------------------------------------- + * //sevenSegmentLed Library Example + * // + * //This program by one every second counts, do a 4-digit seven-segment LED display. + * // + * //seven segment numeric LED Display : LTC4627P + * // http://www.excesssolutions.com/mas_assets/acrobat/ES5721.pdf + * // + * // LTC4627T Resister mbed + * // Pin No Function [ohm] Function + * // --------------------------------------------------------------------------- + * // 1 Common Anode Digit 1 - P29 + * // 2 Common Anode Digit 2 - P13 + * // 3 Cathode D 200 P22 + * // 4 Common Anode L1,L2,L3 - - + * // 5 Cathode E 200 P24 + * // 6 Common Anode Digit 3 - P25 + * // 7 Cathode D.p. 200 P30 + * // 8 Common Anode Digit 4 - P27 + * // 9 No Connection - - + * // 10 No Pin - - + * // 11 Cathode F 200 P16 + * // 12 No Pin - - + * // 13 Cathode C,L3 200 P17 + * // 14 Cathode A,L1 200 P14 + * // 15 Cathode G 200 P19 + * // 16 Cathode B,L2 200 P20 + * //--------------------------------------------------------------------------- + * #include "mbed.h" + * #include "SevenSegLed.h" + * + * // common type (0:anode common 1:cathode common) + * // | + * // | display mode (0:smooth 1:hard) + * // | | + * // | | segA segB segC segD segE segF segG segP com1 com2 com3 com4 + * // | | | | | | | | | | | | | | + * SevenSegLed segmentled(0, 0, p14, p20, p17, p22, p24, p16, p19, p30, p29, p13, p25, p27); + * + * + * // 1 2 3 4digit + * // | | | | + * uint8_t D_7seg[4] = {0, 0, 0, 0}; // seven segment digit number (0x00:"0", 0x01:"1", ... , 0x09:"9", 0x0A:"A", ... , 0x0F:"F", other:" ") + * uint8_t D_dot[4] = {0, 0, 0, 0}; // seven segment digit dotpoint. (0:off 1:on) + * + * + * Timer timer; // 1second timer + * + * int main() { + * uint16_t counter = 0; + * + * timer.start(); + * + * while(1) { + * // After one second to start the process + * if(timer.read_ms() >= 1000){ + * timer.reset(); + * counter++; + * + * // Display digit data updates + * D_7seg[0] = (uint8_t)((counter & 0xF000) >> 12); + * D_7seg[1] = (uint8_t)((counter & 0x0F00) >> 8); + * D_7seg[2] = (uint8_t)((counter & 0x00F0) >> 4); + * D_7seg[3] = (uint8_t)(counter & 0x000F); + * + * // Display dot point data updates + * D_dot[0] = 0; + * D_dot[1] = 0; + * D_dot[2] = 0; + * D_dot[3] = 0; + * + * // dot point data set + * D_dot[counter & 0x0003] = 1; + * } + * + * // seven segment display to output data + * // This function, please repeat the process in less than 1ms. + * segmentled.SevenSegLed_main(D_7seg, D_dot); + * + * } + * } + * @endcode + */ + + + +#include "types.h" + +class SevenSegLed { +public: + + + + /** Create a seven segment led array object connected to the specified DigitalOut pin + * @param commonPole The polarity of the seven segment led common 0:Anode common, 1:Cathode common + * @param smooth Reading the LED display method. 0:Smooth changing the LED display in one second 1:Quickly changing the LED display + * @param seg_a - seg_p DigitalOut pin to connect to. To provide members with an array of uint8_t digit minutes. 4 digits maximum + * @param com_1 - com_4 DigitalOut pin to connect to. To provide members with an array of uint8_t digit minutes. 4 digits maximum + */ + SevenSegLed(uint8_t commonPole, uint8_t smooth, PinName seg_a, PinName seg_b, PinName seg_c, PinName seg_d, PinName seg_e, PinName seg_f, PinName seg_g, PinName seg_p, PinName com_1 = NC, PinName com_2 = NC, PinName com_3 = NC, PinName com_4 = NC); + + /** Data set to the seven segment LED display + * @param number Array variable address pointer of Numerical data 0 - 9,A - F : The figures show, 0x10:off + * @param dot Array variable address pointer of dot data 0:off 1:on + */ + void SevenSegLed_main(uint8_t* number, uint8_t* dot); + +private: +void segmentGrayDataKosin(void); +void comAllClear(void); +void segAllClear(void); +void segDataSet(uint8_t keta); +void output(void); + +// pin set_seg, _com; + DigitalOut _seg_a, _seg_b, _seg_c, _seg_d, _seg_e, _seg_f, _seg_g, _seg_p; + DigitalOut _com_1, _com_2, _com_3, _com_4; + + Ticker timer; + +#define Z_ketaSuu (4) // 7segment no keta suu +#define Z_segSuu (8) // 7segmetn no segment suu (a,b,...,g,Dp) +#define Z_grayMax (100) // grayData max 100 kaicho +#define Z_pwmGrayMax (100) // pwm max (led heno pwm syuturyoku no max) + + +uint8_t D_7seg[Z_ketaSuu]; // digit number display request 0:"0", 1:"1", ... , 9:"9", A:"A", B:"b", C:"C", D:"d", E:"E", F:"F" + // [0]:digit1, [1]:digit2, ... ,[3]:digit4 +uint8_t D_dot[Z_ketaSuu]; // digit dot display request 0:off 1(not 0):on + // [0]:digit1, [1]:digit2, ... ,[3]:digit4 + + +uint8_t D_7segGray[Z_ketaSuu][Z_segSuu]; // hyoji segment no gray data 0:syoto 1:min - 10:max + +uint8_t DT_pwmGray[Z_grayMax + 1]; // gray data kara pwm data heno henkan table + +uint8_t D_comNull; // comX Null check No set (0:all com is NC 1:com1 connect, 2:com2 connect,...,4:com4 connect(all com connect) + +uint8_t D_smooth; // Those who will be reading the LED display 0:smooth 1:hard +#define Z_smooth (0) +#define Z_hard (1) + +uint8_t D_commonOn; // common On level set 0:Anode common 1:Cathode common +uint8_t D_commonOff; + +uint8_t D_segmentOn; // segment On level set 0:Cathode common 1:Anode common +uint8_t D_segmentOff; +}; + +#endif // _SEVENSEGLED_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/types.h Sun Nov 20 00:22:05 2011 +0000 @@ -0,0 +1,114 @@ +/*----------------------------------------------------------------------------*/ +/* File Information */ +/*----------------------------------------------------------------------------*/ +/* Name : types.h */ +/* Type : C Programming Language Header */ +/*----------------------------------------------------------------------------*/ +/*----------------------------------------------------------------------------*/ + +#ifndef __TYPES_H__ +#define __TYPES_H__ + +#include "stdint.h" +/* +typedef char int8_t; +typedef unsigned char uint8_t; +typedef signed short int16_t; +typedef unsigned short uint16_t; +typedef signed int int32_t; +typedef unsigned int uint32_t; +typedef signed long long int64_t; +typedef unsigned long long uint64_t; +*/ +//typedef bool bool_t; +typedef enum{TRUE, FALSE} bool_t; + +//========================================================================= +// byte bit access +//========================================================================= +typedef union{ // BYTE/NIBBLE/BIT access + uint8_t byte; // Byte access + struct{ // Nibble access + uint8_t lo : 4; // lower(Bit0 - 3) + uint8_t hi : 4; // upper(Bit4 - 7) + }nibble; + struct{ // Bit access + uint8_t b0 : 1; // Bit0 + uint8_t b1 : 1; // Bit1 + uint8_t b2 : 1; // Bit2 + uint8_t b3 : 1; // Bit3 + uint8_t b4 : 1; // Bit4 + uint8_t b5 : 1; // Bit5 + uint8_t b6 : 1; // Bit6 + uint8_t b7 : 1; // Bit7 + }bits; +}byte_t; + +//========================================================================= +// word bit access +//========================================================================= +typedef union{ // WORD/BYTE/NIBBLE/BIT access + uint16_t word; // Word access + struct{ // Byte access + uint8_t b0; // upper byte + uint8_t b1; // lower byte + }byte; + struct { // Nibble access + uint8_t n0 : 4; // lower byte low(Bit 0 - 3) + uint8_t n1 : 4; // lower byte up (Bit 4 - 7) + uint8_t n2 : 4; // upper byte low(Bit 8 - 11) + uint8_t n3 : 4; // upper byte up (Bit12 - 15) + }nibble; + struct{ // Bit acces + uint8_t b0 : 1; // Bit0 + uint8_t b1 : 1; // Bit1 + uint8_t b2 : 1; // Bit2 + uint8_t b3 : 1; // Bit3 + uint8_t b4 : 1; // Bit4 + uint8_t b5 : 1; // Bit5 + uint8_t b6 : 1; // Bit6 + uint8_t b7 : 1; // Bit7 + uint8_t b8 : 1; // Bit8 + uint8_t b9 : 1; // Bit9 + uint8_t b10: 1; // Bit10 + uint8_t b11: 1; // Bit11 + uint8_t b12: 1; // Bit12 + uint8_t b13: 1; // Bit13 + uint8_t b14: 1; // Bit14 + uint8_t b15: 1; // Bit15 + }bits; +}word_t; + + +//========================================================================= +// ascii code +//========================================================================= +#define Z_NUL (0x00) +#define Z_SOH (0x01) +#define Z_STX (0x02) +#define Z_ETX (0x03) +#define Z_EOT (0x04) +#define Z_ENQ (0x05) +#define Z_ACK (0x06) +#define Z_BEL (0x07) + +#define Z_BS (0x08) +#define Z_HT (0x09) +#define Z_LF (0x0A) +#define Z_HM (0x0B) +#define Z_FF (0x0C) +#define Z_CR (0x0D) +#define Z_SO (0x0E) +#define Z_SI (0x0F) + +#define Z_DLE (0x10) +#define Z_DC1 (0x11) +#define Z_DC2 (0x12) +#define Z_DC3 (0x13) +#define Z_DC4 (0x14) +#define Z_NAK (0x15) +#define Z_SYN (0x16) +#define Z_ETB (0x17) + + +#endif /* __TYPES_H__*/ \ No newline at end of file