7 Segment LED Displaydriver, I2C interface, SAA1064

Dependents:   812_hello

Committer:
wim
Date:
Tue Sep 10 19:59:35 2013 +0000
Revision:
1:79cb73f852da
Parent:
0:48adc4a70511
Child:
2:970360b29a2a
First release version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:48adc4a70511 1 /* SAA1064 - I2C LED Driver used in multiplex mode (4x 7 Segments and Decimal Point)
wim 0:48adc4a70511 2 * Copyright (c) 2013 Wim Huiskamp
wim 0:48adc4a70511 3 *
wim 0:48adc4a70511 4 * Released under the MIT License: http://mbed.org/license/mit
wim 0:48adc4a70511 5 *
wim 0:48adc4a70511 6 * version 0.2 Initial Release
wim 0:48adc4a70511 7 */
wim 0:48adc4a70511 8 #include "mbed.h"
wim 0:48adc4a70511 9 #include "SAA1064.h"
wim 0:48adc4a70511 10
wim 1:79cb73f852da 11
wim 1:79cb73f852da 12 /** Create a SAA1064 LED displaydriver object using a specified I2C bus and slaveaddress
wim 1:79cb73f852da 13 *
wim 1:79cb73f852da 14 * @param I2C &i2c the I2C port to connect to
wim 1:79cb73f852da 15 * @param char deviceAddress the address of the SAA1064
wim 1:79cb73f852da 16 */
wim 1:79cb73f852da 17 SAA1064::SAA1064(I2C *i2c, uint8_t deviceAddress) : _i2c(i2c) {
wim 0:48adc4a70511 18
wim 0:48adc4a70511 19 _slaveAddress = deviceAddress;
wim 0:48adc4a70511 20 _init();
wim 0:48adc4a70511 21 };
wim 0:48adc4a70511 22
wim 1:79cb73f852da 23 /** Set segment brightness
wim 1:79cb73f852da 24 *
wim 1:79cb73f852da 25 * @param intensity intensity value, valid Range between 0-7, 0 = 0 mA/segment, 1 = 3 mA/segment etc
wim 1:79cb73f852da 26 */
wim 1:79cb73f852da 27 void SAA1064::setIntensity(uint8_t intensity) {
wim 1:79cb73f852da 28 uint8_t data[6];
wim 0:48adc4a70511 29
wim 0:48adc4a70511 30 intensity = (intensity & 0x07) << 4; // Valid Range between 0-7
wim 0:48adc4a70511 31 // 0 = 0 mA/segment, 1 = 3 mA/segment etc
wim 0:48adc4a70511 32 data[0] = SAA1064_CTRL; // Select Control Reg
wim 0:48adc4a70511 33 data[1] = SAA1064_CTRL_DEF | intensity; // Init Control Reg
wim 0:48adc4a70511 34
wim 0:48adc4a70511 35 // write data to the display
wim 1:79cb73f852da 36 _i2c->write(_slaveAddress, (char*) data, 2);
wim 0:48adc4a70511 37
wim 0:48adc4a70511 38 };
wim 0:48adc4a70511 39
wim 0:48adc4a70511 40
wim 1:79cb73f852da 41 /** Write digits
wim 1:79cb73f852da 42 *
wim 1:79cb73f852da 43 * @param digit1 LED segment pattern for digit1 (MSB)
wim 1:79cb73f852da 44 * @param digit2 LED segment pattern for digit2
wim 1:79cb73f852da 45 * @param digit3 LED segment pattern for digit3
wim 1:79cb73f852da 46 * @param digit4 LED segment pattern for digit4 (LSB)
wim 1:79cb73f852da 47 */
wim 1:79cb73f852da 48 void SAA1064::write(uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4) {
wim 1:79cb73f852da 49 uint8_t data[6];
wim 0:48adc4a70511 50
wim 0:48adc4a70511 51 data[0] = SAA1064_DIG1; // Select Digit1 Reg
wim 0:48adc4a70511 52 data[1] = digit1; // Digit 1
wim 0:48adc4a70511 53 data[2] = digit2; // Digit 2
wim 0:48adc4a70511 54 data[3] = digit3; // Digit 3
wim 0:48adc4a70511 55 data[4] = digit4; // Digit 4
wim 0:48adc4a70511 56
wim 0:48adc4a70511 57 // write data to the display
wim 1:79cb73f852da 58 _i2c->write(_slaveAddress, (char*) data, 5);
wim 0:48adc4a70511 59
wim 0:48adc4a70511 60 };
wim 0:48adc4a70511 61
wim 1:79cb73f852da 62
wim 1:79cb73f852da 63 /** Write Integer
wim 1:79cb73f852da 64 *
wim 1:79cb73f852da 65 * @param value integer value to display, valid range -999...9999
wim 1:79cb73f852da 66 * @param dp_digit digit where decimal point is set, valid range 1..4 (no DP shown for dp_digit = 0)
wim 1:79cb73f852da 67 * @param leading suppress leading zero (false=show leading zero, true=suppress leading zero)
wim 1:79cb73f852da 68 */
wim 1:79cb73f852da 69 void SAA1064::writeInt(int value, uint8_t dp_digit, bool leading) {
wim 1:79cb73f852da 70 uint8_t digit_value;
wim 1:79cb73f852da 71 uint8_t data[6];
wim 0:48adc4a70511 72
wim 0:48adc4a70511 73 data[0] = SAA1064_DIG1; // Select Digit1 Reg
wim 0:48adc4a70511 74
wim 0:48adc4a70511 75 // limit to valid range
wim 0:48adc4a70511 76 if (value >= 9999) value = 9999;
wim 0:48adc4a70511 77 if (value <= -999) value = -999;
wim 0:48adc4a70511 78
wim 0:48adc4a70511 79 if (value >= 0) {
wim 0:48adc4a70511 80 // value 0...9999
wim 0:48adc4a70511 81 digit_value = value/1000; // compute thousands
wim 0:48adc4a70511 82 value = value % 1000; // compute remainder
wim 0:48adc4a70511 83 if ((digit_value==0)&& leading)
wim 0:48adc4a70511 84 data[1] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 85 else {
wim 0:48adc4a70511 86 data[1] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 87 leading = false; // dont suppress zero's
wim 0:48adc4a70511 88 }
wim 0:48adc4a70511 89 if (dp_digit==1) {data[1] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 90
wim 0:48adc4a70511 91
wim 0:48adc4a70511 92 digit_value = value/100; // compute hundreds
wim 0:48adc4a70511 93 value = value % 100; // compute remainder
wim 0:48adc4a70511 94 if ((digit_value==0) && leading)
wim 0:48adc4a70511 95 data[2] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 96 else {
wim 0:48adc4a70511 97 data[2] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 98 leading = false; // dont suppress zero's
wim 0:48adc4a70511 99 }
wim 0:48adc4a70511 100 if (dp_digit==2) {data[2] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 101
wim 0:48adc4a70511 102 digit_value = value/10; // compute tens
wim 0:48adc4a70511 103 value = value % 10; // compute remainder
wim 0:48adc4a70511 104 if ((digit_value==0) && leading)
wim 0:48adc4a70511 105 data[3] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 106 else {
wim 0:48adc4a70511 107 data[3] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 108 //leading = false; // dont suppress zero's
wim 0:48adc4a70511 109 }
wim 0:48adc4a70511 110 if (dp_digit==3) {data[3] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 111
wim 0:48adc4a70511 112 //digit_value = value; // compute units
wim 0:48adc4a70511 113 data[4] = SAA1064_SEGM[value]; // never suppress units zero
wim 0:48adc4a70511 114 if (dp_digit==4) {data[4] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 115
wim 0:48adc4a70511 116 }
wim 0:48adc4a70511 117 else {
wim 0:48adc4a70511 118 // value -999...-1
wim 0:48adc4a70511 119 value = -value;
wim 0:48adc4a70511 120 data[1] = SAA1064_MINUS; // Sign
wim 0:48adc4a70511 121 if (dp_digit==1) {data[1] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 122
wim 0:48adc4a70511 123 digit_value = value/100; // compute hundreds
wim 0:48adc4a70511 124 value = value % 100; // compute remainder
wim 0:48adc4a70511 125 if ((digit_value==0) && leading)
wim 0:48adc4a70511 126 data[2] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 127 else {
wim 0:48adc4a70511 128 data[2] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 129 leading = false; // dont suppress zero's
wim 0:48adc4a70511 130 }
wim 0:48adc4a70511 131 if (dp_digit==2) {data[2] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 132
wim 0:48adc4a70511 133 digit_value = value/10; // compute tens
wim 0:48adc4a70511 134 value = value % 10; // compute remainder
wim 0:48adc4a70511 135 if ((digit_value==0) && leading)
wim 0:48adc4a70511 136 data[3] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 137 else {
wim 0:48adc4a70511 138 data[3] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 139 //leading = false; // dont suppress zero's
wim 0:48adc4a70511 140 }
wim 0:48adc4a70511 141 if (dp_digit==3) {data[3] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 142
wim 0:48adc4a70511 143 //digit_value = value; // compute units
wim 0:48adc4a70511 144 data[4] = SAA1064_SEGM[value]; // never suppress units zero
wim 0:48adc4a70511 145 if (dp_digit==4) {data[4] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 146 }
wim 0:48adc4a70511 147
wim 0:48adc4a70511 148 // write data to the display
wim 1:79cb73f852da 149 _i2c->write(_slaveAddress, (char*) data, 5);
wim 0:48adc4a70511 150
wim 0:48adc4a70511 151 };
wim 0:48adc4a70511 152
wim 1:79cb73f852da 153 /** snake: show a short animation
wim 1:79cb73f852da 154 *
wim 1:79cb73f852da 155 * @param count number of times animation is repeated, valid range 0..15
wim 1:79cb73f852da 156 *
wim 1:79cb73f852da 157 */
wim 1:79cb73f852da 158 void SAA1064::snake(uint8_t count) {
wim 1:79cb73f852da 159 uint8_t i;
wim 1:79cb73f852da 160 const float step = 0.1;
wim 1:79cb73f852da 161 // const float loop = 0.1;
wim 1:79cb73f852da 162
wim 1:79cb73f852da 163 count = count & 0x0F; // Limit max count
wim 1:79cb73f852da 164
wim 1:79cb73f852da 165 for (i=0; i<count; i++) {
wim 1:79cb73f852da 166 write(0x00,0x00,0x00,0x01); wait(step);
wim 1:79cb73f852da 167 write(0x00,0x00,0x01,0x01); wait(step);
wim 1:79cb73f852da 168 write(0x00,0x01,0x01,0x01); wait(step);
wim 1:79cb73f852da 169 write(0x01,0x01,0x01,0x00); wait(step);
wim 1:79cb73f852da 170 write(0x21,0x01,0x00,0x00); wait(step);
wim 1:79cb73f852da 171 write(0x31,0x00,0x00,0x00); wait(step);
wim 1:79cb73f852da 172 write(0x38,0x00,0x00,0x00); wait(step);
wim 1:79cb73f852da 173 write(0x18,0x08,0x00,0x00); wait(step);
wim 1:79cb73f852da 174 write(0x08,0x08,0x08,0x00); wait(step);
wim 1:79cb73f852da 175 write(0x00,0x08,0x08,0x08); wait(step);
wim 1:79cb73f852da 176 write(0x00,0x00,0x08,0x0C); wait(step);
wim 1:79cb73f852da 177 write(0x00,0x00,0x00,0x0E); wait(step);
wim 1:79cb73f852da 178 write(0x00,0x00,0x00,0x06); wait(step);
wim 1:79cb73f852da 179 write(0x00,0x00,0x00,0x02); wait(step);
wim 1:79cb73f852da 180 write(0x00,0x00,0x00,0x00); wait(step);
wim 1:79cb73f852da 181
wim 1:79cb73f852da 182 // wait(loop)
wim 1:79cb73f852da 183 }
wim 0:48adc4a70511 184
wim 1:79cb73f852da 185 }
wim 1:79cb73f852da 186
wim 1:79cb73f852da 187
wim 1:79cb73f852da 188 /** splash: show a short animation
wim 1:79cb73f852da 189 *
wim 1:79cb73f852da 190 * @param count number of times animation is repeated, valid range 0..15
wim 1:79cb73f852da 191 *
wim 1:79cb73f852da 192 */
wim 1:79cb73f852da 193 void SAA1064::splash (uint8_t count){
wim 1:79cb73f852da 194 uint8_t i;
wim 1:79cb73f852da 195 const float step = 0.3;
wim 1:79cb73f852da 196 // const float loop = 0.1;
wim 1:79cb73f852da 197
wim 1:79cb73f852da 198 count = count & 0x0F; // Limit max count
wim 1:79cb73f852da 199
wim 1:79cb73f852da 200 for (i=0; i<count; i++) {
wim 1:79cb73f852da 201 write(0x00,0x40,0x40,0x00); wait(step);
wim 1:79cb73f852da 202 write(0x39,0x09,0x09,0x0F); wait(step);
wim 1:79cb73f852da 203
wim 1:79cb73f852da 204 // wait(loop)
wim 1:79cb73f852da 205 }
wim 1:79cb73f852da 206 write(0x00,0x00,0x00,0x00);
wim 1:79cb73f852da 207 }
wim 1:79cb73f852da 208
wim 1:79cb73f852da 209
wim 1:79cb73f852da 210 /** Initialise LED driver
wim 1:79cb73f852da 211 *
wim 1:79cb73f852da 212 */
wim 0:48adc4a70511 213 void SAA1064::_init() {
wim 1:79cb73f852da 214 uint8_t data[6];
wim 0:48adc4a70511 215
wim 0:48adc4a70511 216 data[0] = SAA1064_CTRL; // Select Control Reg
wim 0:48adc4a70511 217 data[1] = SAA1064_CTRL_DEF | SAA1064_INT3; // Init Control Reg
wim 0:48adc4a70511 218 data[2] = SAA1064_BLNK; // Digit 1: All Segments Off
wim 0:48adc4a70511 219 data[3] = SAA1064_BLNK; // Digit 2: All Segments Off
wim 0:48adc4a70511 220 data[4] = SAA1064_BLNK; // Digit 3: All Segments Off
wim 0:48adc4a70511 221 data[5] = SAA1064_BLNK; // Digit 4: All Segments Off
wim 0:48adc4a70511 222
wim 0:48adc4a70511 223 // data[2] = SAA1064_ALL; // Digit 1: All Segments On
wim 0:48adc4a70511 224 // data[3] = SAA1064_ALL; // Digit 2: All Segments On
wim 0:48adc4a70511 225 // data[4] = SAA1064_ALL; // Digit 3: All Segments On
wim 0:48adc4a70511 226 // data[5] = SAA1064_ALL; // Digit 4: All Segments On
wim 0:48adc4a70511 227
wim 0:48adc4a70511 228 // write data to the display
wim 1:79cb73f852da 229 _i2c->write(_slaveAddress, (char*) data, 6);
wim 0:48adc4a70511 230
wim 0:48adc4a70511 231 };