7 Segment LED Displaydriver, I2C interface, SAA1064

Dependents:   812_hello

Committer:
wim
Date:
Sun Sep 08 22:23:52 2013 +0000
Revision:
0:48adc4a70511
Child:
1:79cb73f852da
SAA1064 test and LPC812 test

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 0:48adc4a70511 11 /** Create an SAA1064 object connected to the specified I2C object and using the specified deviceAddress
wim 0:48adc4a70511 12 *
wim 0:48adc4a70511 13 * @param I2C &i2c the I2C port to connect to
wim 0:48adc4a70511 14 * @param char deviceAddress the address of the SAA1064
wim 0:48adc4a70511 15 */
wim 0:48adc4a70511 16 SAA1064::SAA1064(I2C *i2c, char deviceAddress) : _i2c(i2c) {
wim 0:48adc4a70511 17
wim 0:48adc4a70511 18 _slaveAddress = deviceAddress;
wim 0:48adc4a70511 19 _init();
wim 0:48adc4a70511 20 };
wim 0:48adc4a70511 21
wim 0:48adc4a70511 22 char SAA1064::read() {
wim 0:48adc4a70511 23 char tmp = 0;
wim 0:48adc4a70511 24
wim 0:48adc4a70511 25 return tmp;
wim 0:48adc4a70511 26 };
wim 0:48adc4a70511 27
wim 0:48adc4a70511 28 void SAA1064::write(char byte) {
wim 0:48adc4a70511 29 };
wim 0:48adc4a70511 30
wim 0:48adc4a70511 31
wim 0:48adc4a70511 32
wim 0:48adc4a70511 33 void SAA1064::setIntensity(unsigned char intensity) {
wim 0:48adc4a70511 34 char data[6];
wim 0:48adc4a70511 35
wim 0:48adc4a70511 36 intensity = (intensity & 0x07) << 4; // Valid Range between 0-7
wim 0:48adc4a70511 37 // 0 = 0 mA/segment, 1 = 3 mA/segment etc
wim 0:48adc4a70511 38 data[0] = SAA1064_CTRL; // Select Control Reg
wim 0:48adc4a70511 39 data[1] = SAA1064_CTRL_DEF | intensity; // Init Control Reg
wim 0:48adc4a70511 40
wim 0:48adc4a70511 41 // write data to the display
wim 0:48adc4a70511 42 _i2c->write(_slaveAddress, data, 2);
wim 0:48adc4a70511 43
wim 0:48adc4a70511 44 };
wim 0:48adc4a70511 45
wim 0:48adc4a70511 46
wim 0:48adc4a70511 47
wim 0:48adc4a70511 48 void SAA1064::write(unsigned char digit1, unsigned char digit2, unsigned char digit3, unsigned char digit4) {
wim 0:48adc4a70511 49 char 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 0:48adc4a70511 58 _i2c->write(_slaveAddress, data, 5);
wim 0:48adc4a70511 59
wim 0:48adc4a70511 60 };
wim 0:48adc4a70511 61
wim 0:48adc4a70511 62 void SAA1064::writeInt(int value, unsigned char dp_digit, bool leading) {
wim 0:48adc4a70511 63 unsigned char digit_value;
wim 0:48adc4a70511 64 char data[6];
wim 0:48adc4a70511 65
wim 0:48adc4a70511 66 data[0] = SAA1064_DIG1; // Select Digit1 Reg
wim 0:48adc4a70511 67
wim 0:48adc4a70511 68 // limit to valid range
wim 0:48adc4a70511 69 if (value >= 9999) value = 9999;
wim 0:48adc4a70511 70 if (value <= -999) value = -999;
wim 0:48adc4a70511 71
wim 0:48adc4a70511 72 if (value >= 0) {
wim 0:48adc4a70511 73 // value 0...9999
wim 0:48adc4a70511 74 digit_value = value/1000; // compute thousands
wim 0:48adc4a70511 75 value = value % 1000; // compute remainder
wim 0:48adc4a70511 76 if ((digit_value==0)&& leading)
wim 0:48adc4a70511 77 data[1] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 78 else {
wim 0:48adc4a70511 79 data[1] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 80 leading = false; // dont suppress zero's
wim 0:48adc4a70511 81 }
wim 0:48adc4a70511 82 if (dp_digit==1) {data[1] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 83
wim 0:48adc4a70511 84
wim 0:48adc4a70511 85 digit_value = value/100; // compute hundreds
wim 0:48adc4a70511 86 value = value % 100; // compute remainder
wim 0:48adc4a70511 87 if ((digit_value==0) && leading)
wim 0:48adc4a70511 88 data[2] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 89 else {
wim 0:48adc4a70511 90 data[2] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 91 leading = false; // dont suppress zero's
wim 0:48adc4a70511 92 }
wim 0:48adc4a70511 93 if (dp_digit==2) {data[2] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 94
wim 0:48adc4a70511 95 digit_value = value/10; // compute tens
wim 0:48adc4a70511 96 value = value % 10; // compute remainder
wim 0:48adc4a70511 97 if ((digit_value==0) && leading)
wim 0:48adc4a70511 98 data[3] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 99 else {
wim 0:48adc4a70511 100 data[3] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 101 //leading = false; // dont suppress zero's
wim 0:48adc4a70511 102 }
wim 0:48adc4a70511 103 if (dp_digit==3) {data[3] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 104
wim 0:48adc4a70511 105 //digit_value = value; // compute units
wim 0:48adc4a70511 106 data[4] = SAA1064_SEGM[value]; // never suppress units zero
wim 0:48adc4a70511 107 if (dp_digit==4) {data[4] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 108
wim 0:48adc4a70511 109 }
wim 0:48adc4a70511 110 else {
wim 0:48adc4a70511 111 // value -999...-1
wim 0:48adc4a70511 112 value = -value;
wim 0:48adc4a70511 113 data[1] = SAA1064_MINUS; // Sign
wim 0:48adc4a70511 114 if (dp_digit==1) {data[1] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 115
wim 0:48adc4a70511 116 digit_value = value/100; // compute hundreds
wim 0:48adc4a70511 117 value = value % 100; // compute remainder
wim 0:48adc4a70511 118 if ((digit_value==0) && leading)
wim 0:48adc4a70511 119 data[2] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 120 else {
wim 0:48adc4a70511 121 data[2] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 122 leading = false; // dont suppress zero's
wim 0:48adc4a70511 123 }
wim 0:48adc4a70511 124 if (dp_digit==2) {data[2] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 125
wim 0:48adc4a70511 126 digit_value = value/10; // compute tens
wim 0:48adc4a70511 127 value = value % 10; // compute remainder
wim 0:48adc4a70511 128 if ((digit_value==0) && leading)
wim 0:48adc4a70511 129 data[3] = SAA1064_BLNK; // suppress leading zero
wim 0:48adc4a70511 130 else {
wim 0:48adc4a70511 131 data[3] = SAA1064_SEGM[digit_value];
wim 0:48adc4a70511 132 //leading = false; // dont suppress zero's
wim 0:48adc4a70511 133 }
wim 0:48adc4a70511 134 if (dp_digit==3) {data[3] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 135
wim 0:48adc4a70511 136 //digit_value = value; // compute units
wim 0:48adc4a70511 137 data[4] = SAA1064_SEGM[value]; // never suppress units zero
wim 0:48adc4a70511 138 if (dp_digit==4) {data[4] |= SAA1064_DP;} // Set decimal point
wim 0:48adc4a70511 139 }
wim 0:48adc4a70511 140
wim 0:48adc4a70511 141 // write data to the display
wim 0:48adc4a70511 142 _i2c->write(_slaveAddress, data, 5);
wim 0:48adc4a70511 143
wim 0:48adc4a70511 144 };
wim 0:48adc4a70511 145
wim 0:48adc4a70511 146
wim 0:48adc4a70511 147 void SAA1064::_init() {
wim 0:48adc4a70511 148 char data[6];
wim 0:48adc4a70511 149
wim 0:48adc4a70511 150 data[0] = SAA1064_CTRL; // Select Control Reg
wim 0:48adc4a70511 151 data[1] = SAA1064_CTRL_DEF | SAA1064_INT3; // Init Control Reg
wim 0:48adc4a70511 152 data[2] = SAA1064_BLNK; // Digit 1: All Segments Off
wim 0:48adc4a70511 153 data[3] = SAA1064_BLNK; // Digit 2: All Segments Off
wim 0:48adc4a70511 154 data[4] = SAA1064_BLNK; // Digit 3: All Segments Off
wim 0:48adc4a70511 155 data[5] = SAA1064_BLNK; // Digit 4: All Segments Off
wim 0:48adc4a70511 156
wim 0:48adc4a70511 157 // data[2] = SAA1064_ALL; // Digit 1: All Segments On
wim 0:48adc4a70511 158 // data[3] = SAA1064_ALL; // Digit 2: All Segments On
wim 0:48adc4a70511 159 // data[4] = SAA1064_ALL; // Digit 3: All Segments On
wim 0:48adc4a70511 160 // data[5] = SAA1064_ALL; // Digit 4: All Segments On
wim 0:48adc4a70511 161
wim 0:48adc4a70511 162 // write data to the display
wim 0:48adc4a70511 163 _i2c->write(_slaveAddress, data, 6);
wim 0:48adc4a70511 164
wim 0:48adc4a70511 165 };