7 segment display library for mbed This routine support 2,4 and 8 digit anode-common or cathode-common LED.

Dependents:   7segMbed

Committer:
morecat_lab
Date:
Sun Sep 21 08:32:50 2014 +0000
Revision:
7:a19431131213
Parent:
6:1de2abf828d6
Child:
10:1b8dbe52e5a8
add _sinkDriver flag to drive segment pin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
morecat_lab 0:6bf4ee8ee342 1 /*
morecat_lab 4:858e42224b50 2 SSeg.cpp - mbed library for 2/4/8 digit seven segment LED driver.
morecat_lab 1:12daa491059c 3 Copyright 2013,2014 by morecat_lab
morecat_lab 0:6bf4ee8ee342 4
morecat_lab 1:12daa491059c 5 based on Dots library.
morecat_lab 0:6bf4ee8ee342 6 Copyright 2010 arms22. All right reserved.
morecat_lab 0:6bf4ee8ee342 7
morecat_lab 0:6bf4ee8ee342 8 This library is distributed in the hope that it will be useful,
morecat_lab 0:6bf4ee8ee342 9 but WITHOUT ANY WARRANTY; without even the implied warranty of
morecat_lab 0:6bf4ee8ee342 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
morecat_lab 0:6bf4ee8ee342 11 */
morecat_lab 0:6bf4ee8ee342 12
morecat_lab 0:6bf4ee8ee342 13 #ifndef SSEG_H
morecat_lab 0:6bf4ee8ee342 14 #define SSEG_H
morecat_lab 0:6bf4ee8ee342 15
morecat_lab 0:6bf4ee8ee342 16 #include "mbed.h"
morecat_lab 0:6bf4ee8ee342 17 #include <Timer.h>
morecat_lab 0:6bf4ee8ee342 18
morecat_lab 0:6bf4ee8ee342 19 #define NUM_PAT_0 0xfc
morecat_lab 0:6bf4ee8ee342 20 #define NUM_PAT_1 0x60
morecat_lab 0:6bf4ee8ee342 21 #define NUM_PAT_2 0xda
morecat_lab 0:6bf4ee8ee342 22 #define NUM_PAT_3 0xf2
morecat_lab 0:6bf4ee8ee342 23 #define NUM_PAT_4 0x66
morecat_lab 0:6bf4ee8ee342 24 #define NUM_PAT_5 0xb6
morecat_lab 0:6bf4ee8ee342 25 #define NUM_PAT_6 0xbe
morecat_lab 0:6bf4ee8ee342 26 #define NUM_PAT_7 0xe0
morecat_lab 0:6bf4ee8ee342 27 #define NUM_PAT_8 0xfe
morecat_lab 0:6bf4ee8ee342 28 #define NUM_PAT_9 0xf6
morecat_lab 0:6bf4ee8ee342 29 #define NUM_PAT_A 0xee
morecat_lab 0:6bf4ee8ee342 30 #define NUM_PAT_B 0x3e
morecat_lab 0:6bf4ee8ee342 31 #define NUM_PAT_C 0x9c
morecat_lab 0:6bf4ee8ee342 32 #define NUM_PAT_D 0x7a
morecat_lab 0:6bf4ee8ee342 33 #define NUM_PAT_E 0x9e
morecat_lab 0:6bf4ee8ee342 34 #define NUM_PAT_F 0x8e
morecat_lab 0:6bf4ee8ee342 35
morecat_lab 1:12daa491059c 36 /**
morecat_lab 6:1de2abf828d6 37 * Seven segment LED driver library
morecat_lab 4:858e42224b50 38 */
morecat_lab 0:6bf4ee8ee342 39 class Sseg
morecat_lab 0:6bf4ee8ee342 40 {
morecat_lab 0:6bf4ee8ee342 41 private:
morecat_lab 0:6bf4ee8ee342 42 unsigned long _lastUpdateTime;
morecat_lab 0:6bf4ee8ee342 43 int _updateInterval;
morecat_lab 0:6bf4ee8ee342 44 BusOut _segPins;
morecat_lab 0:6bf4ee8ee342 45 BusOut _digPins;
morecat_lab 0:6bf4ee8ee342 46 char _buffer[8];
morecat_lab 0:6bf4ee8ee342 47 int _numOfDigs;
morecat_lab 0:6bf4ee8ee342 48 int _dig; // support 4 or 8
morecat_lab 0:6bf4ee8ee342 49 bool _zeroSupress;
morecat_lab 0:6bf4ee8ee342 50 bool _kcommon; // Cathode-common flag
morecat_lab 7:a19431131213 51 bool _sinkDriver; // invert segment drive, if true
morecat_lab 0:6bf4ee8ee342 52 void initConv(void);
morecat_lab 0:6bf4ee8ee342 53 Timer timer;
morecat_lab 0:6bf4ee8ee342 54 public:
morecat_lab 0:6bf4ee8ee342 55 static const int numConv[16];
morecat_lab 1:12daa491059c 56 /**
morecat_lab 2:6f63a5e21d08 57 * create an 2 digit seven segment driver
morecat_lab 2:6f63a5e21d08 58 *
morecat_lab 3:77ec0c031053 59 * @param PinName a Pin No for segment A
morecat_lab 1:12daa491059c 60 * @param PinName b Pin No for segment B
morecat_lab 1:12daa491059c 61 * @param PinName c Pin No for segment C
morecat_lab 1:12daa491059c 62 * @param PinName d Pin No for segment D
morecat_lab 1:12daa491059c 63 * @param PinName e Pin No for segment E
morecat_lab 1:12daa491059c 64 * @param PinName f Pin No for segment F
morecat_lab 1:12daa491059c 65 * @param PinName g Pin No for segment G
morecat_lab 1:12daa491059c 66 * @param PinName dp Pin No for segment DP
morecat_lab 3:77ec0c031053 67 * @param PinName d1 Pin No for dight 1
morecat_lab 1:12daa491059c 68 * @param PinName d2 Pin No for dight 2
morecat_lab 1:12daa491059c 69      */
morecat_lab 0:6bf4ee8ee342 70 Sseg(PinName a,PinName b,PinName c,PinName d,
morecat_lab 0:6bf4ee8ee342 71 PinName e,PinName f,PinName g,PinName dp,
morecat_lab 0:6bf4ee8ee342 72 PinName d1,PinName d2);
morecat_lab 0:6bf4ee8ee342 73
morecat_lab 1:12daa491059c 74 /**
morecat_lab 1:12daa491059c 75 * create an 4 digit seven segment driver
morecat_lab 1:12daa491059c 76 *
morecat_lab 1:12daa491059c 77 * @param PinName a Pin No for segment A
morecat_lab 1:12daa491059c 78 * @param PinName b Pin No for segment B
morecat_lab 1:12daa491059c 79 * @param PinName c Pin No for segment C
morecat_lab 1:12daa491059c 80 * @param PinName d Pin No for segment D
morecat_lab 1:12daa491059c 81 * @param PinName e Pin No for segment E
morecat_lab 1:12daa491059c 82 * @param PinName f Pin No for segment F
morecat_lab 1:12daa491059c 83 * @param PinName g Pin No for segment G
morecat_lab 1:12daa491059c 84 * @param PinName dp Pin No for segment DP
morecat_lab 1:12daa491059c 85 * @param PinName d1 Pin No for dight 1
morecat_lab 1:12daa491059c 86 * @param PinName d2 Pin No for dight 2
morecat_lab 1:12daa491059c 87 * @param PinName d3 Pin No for dight 3
morecat_lab 1:12daa491059c 88 * @param PinName d4 Pin No for dight 4
morecat_lab 1:12daa491059c 89 */
morecat_lab 0:6bf4ee8ee342 90 Sseg(PinName a,PinName b,PinName c,PinName d,
morecat_lab 0:6bf4ee8ee342 91 PinName e,PinName f,PinName g,PinName dp,
morecat_lab 0:6bf4ee8ee342 92 PinName d1,PinName d2, PinName d3, PinName d4);
morecat_lab 0:6bf4ee8ee342 93
morecat_lab 1:12daa491059c 94 /**
morecat_lab 1:12daa491059c 95 * create an 8 digit seven segment driver
morecat_lab 1:12daa491059c 96 *
morecat_lab 1:12daa491059c 97 * @param PinName a Pin No for segment A
morecat_lab 1:12daa491059c 98 * @param PinName b Pin No for segment B
morecat_lab 1:12daa491059c 99 * @param PinName c Pin No for segment C
morecat_lab 1:12daa491059c 100 * @param PinName d Pin No for segment D
morecat_lab 1:12daa491059c 101 * @param PinName e Pin No for segment E
morecat_lab 1:12daa491059c 102 * @param PinName f Pin No for segment F
morecat_lab 1:12daa491059c 103 * @param PinName g Pin No for segment G
morecat_lab 1:12daa491059c 104 * @param PinName dp Pin No for segment DP
morecat_lab 1:12daa491059c 105 * @param PinName d1 Pin No for dight 1
morecat_lab 1:12daa491059c 106 * @param PinName d2 Pin No for dight 2
morecat_lab 1:12daa491059c 107 * @param PinName d3 Pin No for dight 3
morecat_lab 1:12daa491059c 108 * @param PinName d4 Pin No for dight 4
morecat_lab 1:12daa491059c 109 * @param PinName d5 Pin No for dight 5
morecat_lab 1:12daa491059c 110 * @param PinName d6 Pin No for dight 6
morecat_lab 1:12daa491059c 111 * @param PinName d7 Pin No for dight 7
morecat_lab 1:12daa491059c 112 * @param PinName d8 Pin No for dight 8
morecat_lab 1:12daa491059c 113 */
morecat_lab 0:6bf4ee8ee342 114 Sseg(PinName a,PinName b,PinName c,PinName d,
morecat_lab 0:6bf4ee8ee342 115 PinName e,PinName f,PinName g,PinName dp,
morecat_lab 0:6bf4ee8ee342 116 PinName d1,PinName d2, PinName d3, PinName d4,
morecat_lab 0:6bf4ee8ee342 117 PinName d5,PinName d6, PinName d7, PinName d8);
morecat_lab 0:6bf4ee8ee342 118
morecat_lab 1:12daa491059c 119 /**
morecat_lab 2:6f63a5e21d08 120 * start driver
morecat_lab 2:6f63a5e21d08 121 */
morecat_lab 0:6bf4ee8ee342 122 void begin(void);
morecat_lab 1:12daa491059c 123
morecat_lab 1:12daa491059c 124 /**
morecat_lab 1:12daa491059c 125 * use Kathode Common LED
morecat_lab 1:12daa491059c 126 */
morecat_lab 0:6bf4ee8ee342 127 void setKcommon(void);
morecat_lab 1:12daa491059c 128
morecat_lab 1:12daa491059c 129 /**
morecat_lab 1:12daa491059c 130 * use Anode Common LED (default)
morecat_lab 1:12daa491059c 131 */
morecat_lab 0:6bf4ee8ee342 132 void setAcommon(void);
morecat_lab 7:a19431131213 133
morecat_lab 7:a19431131213 134 /**
morecat_lab 7:a19431131213 135 * sink driver
morecat_lab 7:a19431131213 136 */
morecat_lab 7:a19431131213 137 void setSinkDriver(void);
morecat_lab 1:12daa491059c 138
morecat_lab 1:12daa491059c 139 /**
morecat_lab 3:77ec0c031053 140 * get a charcter pattern from a number
morecat_lab 1:12daa491059c 141 *
morecat_lab 1:12daa491059c 142 * @param i number
morecat_lab 1:12daa491059c 143 *
morecat_lab 3:77ec0c031053 144 * @returns bit pattern of number i
morecat_lab 3:77ec0c031053 145 *
morecat_lab 1:12daa491059c 146 */
morecat_lab 2:6f63a5e21d08 147 char segCh(char i);
morecat_lab 1:12daa491059c 148
morecat_lab 1:12daa491059c 149 /**
morecat_lab 5:d99849505bf7 150 * turn on DP
morecat_lab 5:d99849505bf7 151 *
morecat_lab 5:d99849505bf7 152 * @param d dight
morecat_lab 5:d99849505bf7 153 *
morecat_lab 1:12daa491059c 154 */
morecat_lab 0:6bf4ee8ee342 155 void setDot(int d);
morecat_lab 1:12daa491059c 156
morecat_lab 1:12daa491059c 157 /**
morecat_lab 1:12daa491059c 158 * turn off DP
morecat_lab 1:12daa491059c 159 *
morecat_lab 5:d99849505bf7 160 * @param d dight
morecat_lab 1:12daa491059c 161 *
morecat_lab 1:12daa491059c 162 */
morecat_lab 0:6bf4ee8ee342 163 void clearDot(int d);
morecat_lab 1:12daa491059c 164
morecat_lab 1:12daa491059c 165 /**
morecat_lab 1:12daa491059c 166 * write a number to LED
morecat_lab 1:12daa491059c 167 *
morecat_lab 3:77ec0c031053 168 * @param d number
morecat_lab 1:12daa491059c 169 *
morecat_lab 1:12daa491059c 170 */
morecat_lab 0:6bf4ee8ee342 171 void writeNum(int n);
morecat_lab 1:12daa491059c 172
morecat_lab 1:12daa491059c 173 /**
morecat_lab 1:12daa491059c 174 * write a number to 2 dight LED
morecat_lab 1:12daa491059c 175 *
morecat_lab 3:77ec0c031053 176 * @param n number
morecat_lab 1:12daa491059c 177 *
morecat_lab 1:12daa491059c 178 */
morecat_lab 0:6bf4ee8ee342 179 void writeNum2(int n);
morecat_lab 1:12daa491059c 180
morecat_lab 1:12daa491059c 181 /**
morecat_lab 1:12daa491059c 182 * write a number to 4 dight LED
morecat_lab 1:12daa491059c 183 *
morecat_lab 3:77ec0c031053 184 * @param n number
morecat_lab 1:12daa491059c 185 *
morecat_lab 1:12daa491059c 186 */
morecat_lab 0:6bf4ee8ee342 187 void writeNum4(int n);
morecat_lab 1:12daa491059c 188
morecat_lab 1:12daa491059c 189 /**
morecat_lab 1:12daa491059c 190 * write a number to 8 dight LED
morecat_lab 1:12daa491059c 191 *
morecat_lab 5:d99849505bf7 192 * @param n number
morecat_lab 1:12daa491059c 193 *
morecat_lab 1:12daa491059c 194 */
morecat_lab 0:6bf4ee8ee342 195 void writeNum8(int n);
morecat_lab 1:12daa491059c 196
morecat_lab 1:12daa491059c 197 /**
morecat_lab 1:12daa491059c 198 * write numbers to each dight of 2 dight LED
morecat_lab 1:12daa491059c 199 *
morecat_lab 1:12daa491059c 200 * @param d1 digit 1 number
morecat_lab 1:12daa491059c 201 * @param d2 digit 2 number
morecat_lab 1:12daa491059c 202 *
morecat_lab 1:12daa491059c 203 */
morecat_lab 0:6bf4ee8ee342 204 void writeNum(char d1, char d2);
morecat_lab 1:12daa491059c 205
morecat_lab 1:12daa491059c 206 /**
morecat_lab 1:12daa491059c 207 * write numbers to each dight of 4 dight LED
morecat_lab 1:12daa491059c 208 *
morecat_lab 1:12daa491059c 209 * @param d1 digit 1 number
morecat_lab 1:12daa491059c 210 * @param d2 digit 2 number
morecat_lab 1:12daa491059c 211 * @param d3 digit 3 number
morecat_lab 1:12daa491059c 212 * @param d4 digit 4 number
morecat_lab 1:12daa491059c 213 *
morecat_lab 1:12daa491059c 214 */
morecat_lab 0:6bf4ee8ee342 215 void writeNum(char d1, char d2, char d3, char d4);
morecat_lab 1:12daa491059c 216
morecat_lab 1:12daa491059c 217 /**
morecat_lab 1:12daa491059c 218 * write numbers to each dight of 8 dight LED
morecat_lab 1:12daa491059c 219 *
morecat_lab 1:12daa491059c 220 * @param d1 digit 1 number
morecat_lab 1:12daa491059c 221 * @param d2 digit 2 number
morecat_lab 1:12daa491059c 222 * @param d3 digit 3 number
morecat_lab 1:12daa491059c 223 * @param d4 digit 4 number
morecat_lab 1:12daa491059c 224 * @param d5 digit 5 number
morecat_lab 1:12daa491059c 225 * @param d6 digit 6 number
morecat_lab 1:12daa491059c 226 * @param d7 digit 7 number
morecat_lab 1:12daa491059c 227 * @param d8 digit 8 number
morecat_lab 1:12daa491059c 228 *
morecat_lab 1:12daa491059c 229 */
morecat_lab 0:6bf4ee8ee342 230 void writeNum(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);
morecat_lab 1:12daa491059c 231
morecat_lab 1:12daa491059c 232 /**
morecat_lab 1:12daa491059c 233 * zero supress: tell driver not to display 0 in the left
morecat_lab 1:12daa491059c 234 *
morecat_lab 1:12daa491059c 235 */
morecat_lab 0:6bf4ee8ee342 236 void supressZero();
morecat_lab 1:12daa491059c 237
morecat_lab 1:12daa491059c 238 /**
morecat_lab 1:12daa491059c 239 * control zero supress bit
morecat_lab 1:12daa491059c 240 *
morecat_lab 3:77ec0c031053 241 * @param t, 1:supress on, 0:supress off
morecat_lab 1:12daa491059c 242 *
morecat_lab 1:12daa491059c 243 */
morecat_lab 0:6bf4ee8ee342 244 void setZeroSupress(bool t);
morecat_lab 1:12daa491059c 245
morecat_lab 1:12daa491059c 246 /**
morecat_lab 6:1de2abf828d6 247 * write hex number to LED
morecat_lab 1:12daa491059c 248 *
morecat_lab 3:77ec0c031053 249 * @param n number
morecat_lab 1:12daa491059c 250 *
morecat_lab 1:12daa491059c 251 */
morecat_lab 0:6bf4ee8ee342 252 void writeHex(int n);
morecat_lab 1:12daa491059c 253
morecat_lab 1:12daa491059c 254 /**
morecat_lab 6:1de2abf828d6 255 * write hex number to LED
morecat_lab 1:12daa491059c 256 *
morecat_lab 3:77ec0c031053 257 * @param n (long)number
morecat_lab 1:12daa491059c 258 *
morecat_lab 1:12daa491059c 259 */
morecat_lab 0:6bf4ee8ee342 260 void writeHex(long n);
morecat_lab 1:12daa491059c 261
morecat_lab 1:12daa491059c 262 /**
morecat_lab 1:12daa491059c 263 * write patterns to each dight of 2 dight LED
morecat_lab 1:12daa491059c 264 *
morecat_lab 1:12daa491059c 265 * @param d1 digit 1 pattern
morecat_lab 1:12daa491059c 266 * @param d2 digit 2 pattern
morecat_lab 1:12daa491059c 267 *
morecat_lab 1:12daa491059c 268 */
morecat_lab 0:6bf4ee8ee342 269 void writeRawData(char d1, char d2);
morecat_lab 1:12daa491059c 270
morecat_lab 1:12daa491059c 271 /**
morecat_lab 1:12daa491059c 272 * write patterns to each dight of 4 dight LED
morecat_lab 1:12daa491059c 273 *
morecat_lab 1:12daa491059c 274 * @param d1 digit 1 pattern
morecat_lab 1:12daa491059c 275 * @param d2 digit 2 pattern
morecat_lab 1:12daa491059c 276 * @param d3 digit 3 pattern
morecat_lab 1:12daa491059c 277 * @param d4 digit 4 pattern
morecat_lab 1:12daa491059c 278 *
morecat_lab 1:12daa491059c 279 */
morecat_lab 0:6bf4ee8ee342 280 void writeRawData(char d1, char d2, char d3, char d4);
morecat_lab 1:12daa491059c 281
morecat_lab 1:12daa491059c 282 /**
morecat_lab 1:12daa491059c 283 * write patterns to each dight of 8 dight LED
morecat_lab 1:12daa491059c 284 *
morecat_lab 1:12daa491059c 285 * @param d1 digit 1 pattern
morecat_lab 1:12daa491059c 286 * @param d2 digit 2 pattern
morecat_lab 1:12daa491059c 287 * @param d3 digit 3 pattern
morecat_lab 1:12daa491059c 288 * @param d4 digit 4 pattern
morecat_lab 1:12daa491059c 289 * @param d5 digit 5 pattern
morecat_lab 1:12daa491059c 290 * @param d6 digit 6 pattern
morecat_lab 1:12daa491059c 291 * @param d7 digit 7 pattern
morecat_lab 1:12daa491059c 292 * @param d8 digit 8 pattern
morecat_lab 1:12daa491059c 293 *
morecat_lab 1:12daa491059c 294 */
morecat_lab 0:6bf4ee8ee342 295 void writeRawData(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);
morecat_lab 1:12daa491059c 296
morecat_lab 0:6bf4ee8ee342 297 void write(uint8_t x, uint8_t y, uint8_t value);
morecat_lab 1:12daa491059c 298
morecat_lab 1:12daa491059c 299 /**
morecat_lab 1:12daa491059c 300 * write patterns to a dight
morecat_lab 1:12daa491059c 301 *
morecat_lab 1:12daa491059c 302 * @param d digit
morecat_lab 4:858e42224b50 303 *
morecat_lab 1:12daa491059c 304 * @param value pattern
morecat_lab 1:12daa491059c 305 *
morecat_lab 1:12daa491059c 306 */
morecat_lab 1:12daa491059c 307 void write(uint8_t d, uint8_t value);
morecat_lab 0:6bf4ee8ee342 308 void write(uint8_t y, const uint8_t values[], uint8_t size);
morecat_lab 1:12daa491059c 309
morecat_lab 1:12daa491059c 310 /**
morecat_lab 1:12daa491059c 311 * Clear LED buffer
morecat_lab 1:12daa491059c 312 */
morecat_lab 0:6bf4ee8ee342 313 void clear(void);
morecat_lab 1:12daa491059c 314
morecat_lab 1:12daa491059c 315 /**
morecat_lab 1:12daa491059c 316 * Turn off LED
morecat_lab 1:12daa491059c 317 */
morecat_lab 0:6bf4ee8ee342 318 void turnOff(void);
morecat_lab 1:12daa491059c 319
morecat_lab 1:12daa491059c 320 /**
morecat_lab 1:12daa491059c 321 * Turn on LED
morecat_lab 1:12daa491059c 322 */
morecat_lab 0:6bf4ee8ee342 323 void turnOn(void);
morecat_lab 4:858e42224b50 324
morecat_lab 4:858e42224b50 325 /**
morecat_lab 4:858e42224b50 326 * Update One dight of LED
morecat_lab 4:858e42224b50 327 */
morecat_lab 0:6bf4ee8ee342 328 void updateSeg(void);
morecat_lab 4:858e42224b50 329
morecat_lab 4:858e42224b50 330 /**
morecat_lab 4:858e42224b50 331 * Update LED (by internal clock)
morecat_lab 4:858e42224b50 332 *
morecat_lab 4:858e42224b50 333 * @returns sync = 1, if digit == 0
morecat_lab 4:858e42224b50 334 *
morecat_lab 4:858e42224b50 335 */
morecat_lab 0:6bf4ee8ee342 336 bool update(void);
morecat_lab 4:858e42224b50 337
morecat_lab 4:858e42224b50 338 /**
morecat_lab 4:858e42224b50 339 * keep updating LED for specified period
morecat_lab 4:858e42224b50 340 *
morecat_lab 4:858e42224b50 341 * @param ms period (ms)
morecat_lab 4:858e42224b50 342 *
morecat_lab 4:858e42224b50 343 */
morecat_lab 0:6bf4ee8ee342 344 void updateWithDelay(int ms);
morecat_lab 4:858e42224b50 345
morecat_lab 4:858e42224b50 346 /**
morecat_lab 4:858e42224b50 347 * Update LED Once with 1ms delay
morecat_lab 4:858e42224b50 348 */
morecat_lab 4:858e42224b50 349 void updateOnce(void);
morecat_lab 0:6bf4ee8ee342 350 };
morecat_lab 0:6bf4ee8ee342 351
morecat_lab 0:6bf4ee8ee342 352 #endif // SSEG.h