7 Segment LED Displaydriver, I2C interface, SAA1064

Dependents:   812_hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SAA1064.cpp Source File

SAA1064.cpp

00001 /* SAA1064 - I2C LED Driver used in multiplex mode (4x 7 Segments and Decimal Point)
00002  * Copyright (c) 2013 Wim Huiskamp
00003  *
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  *
00006  * version 0.2 Initial Release
00007  * version 0.3 Improved Leading Zero suppress for writeInt 
00008 */
00009 #include "mbed.h"
00010 #include "SAA1064.h"
00011 
00012 
00013 /** Create a SAA1064 LED displaydriver object using a specified I2C bus and slaveaddress
00014   *
00015   * @param I2C &i2c the I2C port to connect to 
00016   * @param char deviceAddress the address of the SAA1064
00017   */  
00018 SAA1064::SAA1064(I2C *i2c, uint8_t deviceAddress) : _i2c(i2c) {
00019 
00020    _slaveAddress = deviceAddress;
00021    _init(); 
00022 };
00023 
00024 /** Set segment brightness
00025   *
00026   * @param intensity       intensity value, valid Range between 0-7, 0 = 0 mA/segment, 1 = 3 mA/segment etc 
00027   */  
00028 void SAA1064::setIntensity(uint8_t intensity) {
00029   uint8_t data[6];
00030   
00031   intensity = (intensity & 0x07) << 4;        // Valid Range between 0-7
00032                                               //  0 = 0 mA/segment, 1 = 3 mA/segment etc 
00033   data[0] = SAA1064_CTRL;                     // Select Control Reg
00034   data[1] = SAA1064_CTRL_DEF | intensity;     // Init Control Reg
00035 
00036   // write data to the display
00037   _i2c->write(_slaveAddress, (char*) data, 2);  
00038 
00039 };
00040 
00041 
00042 /** Write digits 
00043   *
00044   * @param digit1  LED segment pattern for digit1 (MSB) 
00045   * @param digit2  LED segment pattern for digit2 
00046   * @param digit3  LED segment pattern for digit3
00047   * @param digit4  LED segment pattern for digit4 (LSB)       
00048   */  
00049 void SAA1064::write(uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4) {
00050   uint8_t data[6];
00051   
00052   data[0] = SAA1064_DIG1;                     // Select Digit1 Reg
00053   data[1] = digit1;                           // Digit 1
00054   data[2] = digit2;                           // Digit 2 
00055   data[3] = digit3;                           // Digit 3 
00056   data[4] = digit4;                           // Digit 4
00057 
00058   // write data to the display
00059   _i2c->write(_slaveAddress, (char*) data, 5);   
00060 
00061 };
00062 
00063 
00064 /** Write Integer 
00065   *
00066   * @param value     integer value to display, valid range -999...9999 
00067   * @param dp_digit  digit where decimal point is set, valid range 1..4 (no DP shown for dp_digit = 0)
00068   * @param leading   suppress leading zero (false=show leading zero, true=suppress leading zero)
00069   */  
00070 void SAA1064::writeInt(int value, uint8_t dp_digit, bool leading) {
00071   uint8_t digit_value;
00072   uint8_t data[6];  
00073 
00074   data[0] = SAA1064_DIG1;                     // Select Digit1 Reg
00075   
00076   // limit to valid range
00077   if (value >= 9999) value = 9999;
00078   if (value <= -999) value = -999;  
00079 
00080   if (value >= 0) {
00081     // value 0...9999
00082     digit_value = value/1000; // compute thousands
00083     value = value % 1000;     // compute remainder
00084     if ((digit_value==0) && !(dp_digit==1) && leading )
00085       data[1] = SAA1064_BLNK;               // suppress leading zero    
00086     else {
00087       data[1] = SAA1064_SEGM[digit_value];
00088       leading = false;                      // dont suppress zero's  
00089     }  
00090     if (dp_digit==1) {data[1] |= SAA1064_DP;} // Set decimal point
00091 
00092     
00093     digit_value = value/100;  // compute hundreds
00094     value = value % 100;      // compute remainder
00095     if ((digit_value==0) && !(dp_digit==2) && leading)
00096       data[2] = SAA1064_BLNK;               // suppress leading zero    
00097     else {
00098       data[2] = SAA1064_SEGM[digit_value];
00099       leading = false;                      // dont suppress zero's  
00100     }  
00101     if (dp_digit==2) {data[2] |= SAA1064_DP;} // Set decimal point
00102     
00103     digit_value = value/10;   // compute tens
00104     value = value % 10;       // compute remainder
00105     if ((digit_value==0) && !(dp_digit==3) && leading)
00106       data[3] = SAA1064_BLNK;               // suppress leading zero    
00107     else {
00108       data[3] = SAA1064_SEGM[digit_value];
00109       //leading = false;                      // dont suppress zero's  
00110     }  
00111     if (dp_digit==3) {data[3] |= SAA1064_DP;} // Set decimal point
00112     
00113     //digit_value = value;      // compute units
00114     data[4] = SAA1064_SEGM[value];          // never suppress units zero  
00115     if (dp_digit==4) {data[4] |= SAA1064_DP;} // Set decimal point    
00116     
00117   }
00118   else {
00119     // value -999...-1  
00120     value = -value;
00121     data[1] = SAA1064_MINUS;               // Sign 
00122     if (dp_digit==1) {data[1] |= SAA1064_DP;} // Set decimal point
00123           
00124     digit_value = value/100;  // compute hundreds
00125     value = value % 100;      // compute remainder
00126     if ((digit_value==0) && !(dp_digit==2) && leading)
00127       data[2] = SAA1064_BLNK;               // suppress leading zero    
00128     else {
00129       data[2] = SAA1064_SEGM[digit_value];
00130       leading = false;                      // dont suppress zero's  
00131     }  
00132     if (dp_digit==2) {data[2] |= SAA1064_DP;} // Set decimal point
00133         
00134     digit_value = value/10;   // compute tens
00135     value = value % 10;       // compute remainder
00136     if ((digit_value==0) && !(dp_digit==3) && leading)
00137       data[3] = SAA1064_BLNK;               // suppress leading zero    
00138     else {
00139       data[3] = SAA1064_SEGM[digit_value];
00140       //leading = false;                      // dont suppress zero's  
00141     }  
00142     if (dp_digit==3) {data[3] |= SAA1064_DP;} // Set decimal point
00143     
00144     //digit_value = value;      // compute units
00145     data[4] = SAA1064_SEGM[value];          // never suppress units zero         
00146     if (dp_digit==4) {data[4] |= SAA1064_DP;} // Set decimal point    
00147   } 
00148 
00149   // write data to the display
00150   _i2c->write(_slaveAddress, (char*) data, 5);   
00151      
00152 };
00153 
00154 /** snake: show a short animation
00155   *
00156   * @param count     number of times animation is repeated, valid range 0..15 
00157   *
00158   */  
00159 void SAA1064::snake(uint8_t count) {
00160   uint8_t i;
00161   const float step = 0.1;
00162 //  const float loop = 0.1;
00163   
00164   count = count & 0x0F; // Limit max count
00165     
00166   for (i=0; i<count; i++) {
00167     write(0x00,0x00,0x00,0x01); wait(step); 
00168     write(0x00,0x00,0x01,0x01); wait(step);     
00169     write(0x00,0x01,0x01,0x01); wait(step); 
00170     write(0x01,0x01,0x01,0x00); wait(step); 
00171     write(0x21,0x01,0x00,0x00); wait(step);     
00172     write(0x31,0x00,0x00,0x00); wait(step);         
00173     write(0x38,0x00,0x00,0x00); wait(step);         
00174     write(0x18,0x08,0x00,0x00); wait(step);         
00175     write(0x08,0x08,0x08,0x00); wait(step);         
00176     write(0x00,0x08,0x08,0x08); wait(step);           
00177     write(0x00,0x00,0x08,0x0C); wait(step);         
00178     write(0x00,0x00,0x00,0x0E); wait(step);         
00179     write(0x00,0x00,0x00,0x06); wait(step);         
00180     write(0x00,0x00,0x00,0x02); wait(step);         
00181     write(0x00,0x00,0x00,0x00); wait(step);             
00182                                         
00183 //    wait(loop)    
00184   }
00185 
00186 }        
00187 
00188 
00189 /** splash: show a short animation
00190   *
00191   * @param count     number of times animation is repeated, valid range 0..15 
00192   *
00193   */  
00194 void SAA1064::splash (uint8_t count){  
00195   uint8_t i;
00196   const float step = 0.3;
00197 //  const float loop = 0.1;
00198   
00199   count = count & 0x0F; // Limit max count
00200     
00201   for (i=0; i<count; i++) {
00202     write(0x00,0x40,0x40,0x00); wait(step);       
00203     write(0x39,0x09,0x09,0x0F); wait(step); 
00204                                         
00205 //    wait(loop)    
00206   }
00207   write(0x00,0x00,0x00,0x00);
00208 }        
00209 
00210 
00211 /** Initialise LED driver 
00212   *
00213   */  
00214 void SAA1064::_init() { 
00215   uint8_t data[6];
00216   
00217   data[0] = SAA1064_CTRL;                     // Select Control Reg
00218   data[1] = SAA1064_CTRL_DEF | SAA1064_INT3;  // Init Control Reg
00219   data[2] = SAA1064_BLNK;                     // Digit 1: All Segments Off
00220   data[3] = SAA1064_BLNK;                     // Digit 2: All Segments Off 
00221   data[4] = SAA1064_BLNK;                     // Digit 3: All Segments Off      
00222   data[5] = SAA1064_BLNK;                     // Digit 4: All Segments Off   
00223 
00224 //  data[2] = SAA1064_ALL;                      // Digit 1: All Segments On 
00225 //  data[3] = SAA1064_ALL;                      // Digit 2: All Segments On 
00226 //  data[4] = SAA1064_ALL;                      // Digit 3: All Segments On 
00227 //  data[5] = SAA1064_ALL;                      // Digit 4: All Segments On     
00228 
00229   // write data to the display
00230   _i2c->write(_slaveAddress, (char*) data, 6);  
00231   
00232 };