|
| 1 | +/* |
| 2 | + * saa1064.cpp |
| 3 | + * |
| 4 | + * Copyright 2012 Warren Gill <[email protected]> |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | + * MA 02110-1301, USA. |
| 20 | + * |
| 21 | + * Thanks to John Boxall at tronixstuff |
| 22 | + * Example 39.3 - NXP SAA1064 I2C LED Driver IC Demo III |
| 23 | + * Displaying numbers on command |
| 24 | + * http://tronixstuff.wordpress.com/tutorials > chapter 39 |
| 25 | + * John Boxall July 2011 | CC by-sa-nc |
| 26 | + */ |
| 27 | + |
| 28 | +#include <Wire.h> |
| 29 | +#include <avr/pgmspace.h> |
| 30 | +#include "Arduino.h" |
| 31 | +#include "saa1064.h" |
| 32 | + |
| 33 | +// Byte representations of pins required to display each digit 0~9 |
| 34 | +// then A~F, and blank digit |
| 35 | +int SAA1064::_digits[]={ 63, 6, 91, 79, 102, 109, 125,7, 127, 111, 119, 124, 57, 94, 121, 113, 0}; |
| 36 | +uint8_t SAA1064::_brightness[]={ |
| 37 | + (byte)B00000111, // 0ma |
| 38 | + (byte)B00010111, // 3ma |
| 39 | + (byte)B00100111, // 6ma |
| 40 | + (byte)B00110111, // 9ma |
| 41 | + (byte)B01000111, // 12ma |
| 42 | + (byte)B01100111, // 18ma |
| 43 | + (byte)B01110111 // 21ma |
| 44 | +}; |
| 45 | + |
| 46 | +void SAA1064::begin(uint8_t ioaddr, int brightness) { |
| 47 | + // define the I2C address for the SAA1064 |
| 48 | + _addr = ioaddr >> 1; |
| 49 | + |
| 50 | + if (brightness > 6) brightness = 6; |
| 51 | + |
| 52 | + Wire.begin(); // Start the I2C bus |
| 53 | + Wire.beginTransmission(_addr); |
| 54 | + Wire.write((byte)B00000000); // this is the instruction byte. |
| 55 | + // Zero means the next byte is the control byte |
| 56 | + |
| 57 | + // control byte (dynamic mode on, digits 1+3 on, digits 2+4 on, |
| 58 | + // 9mA segment current |
| 59 | + // Wire.write((byte)B00110111); // 9ma |
| 60 | + Wire.write(_brightness[brightness]); |
| 61 | + Wire.endTransmission(); |
| 62 | +} |
| 63 | + |
| 64 | +void SAA1064::begin(uint8_t ioaddr) { |
| 65 | + begin(ioaddr, 2); |
| 66 | +} |
| 67 | + |
| 68 | +void SAA1064::begin() { |
| 69 | + begin(0x70, 2); |
| 70 | +} |
| 71 | + |
| 72 | +void SAA1064::clear() { |
| 73 | + Wire.beginTransmission(_addr); |
| 74 | + Wire.write(0x01); // start with digit 1 (right-hand side) |
| 75 | + Wire.write((byte)0); // blank digit 1 |
| 76 | + Wire.write((byte)0); // blank digit 2 |
| 77 | + Wire.write((byte)0); // blank digit 3 |
| 78 | + Wire.write((byte)0); // blank digit 4 |
| 79 | + Wire.endTransmission(); |
| 80 | +} |
| 81 | + |
| 82 | +void SAA1064::setBrightness(int brightness) { |
| 83 | + if (brightness > 6) brightness = 6; |
| 84 | + Wire.beginTransmission(_addr); |
| 85 | + Wire.write((byte)B00000000); // this is the instruction byte. |
| 86 | + Wire.write(_brightness[brightness]); |
| 87 | + Wire.endTransmission(); |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + displayInt |
| 92 | + displays the number 'num' |
| 93 | + zero = 0 - no leading zero |
| 94 | + zero = 1 - leading zero |
| 95 | + */ |
| 96 | +void SAA1064::displayInt(int num, int zero) { |
| 97 | + int thousand, hundred, ten, one; |
| 98 | + // breakdown number into columns |
| 99 | + thousand = num/1000; |
| 100 | + hundred = (num-(thousand*1000))/100; |
| 101 | + ten = (num-((thousand*1000)+(hundred*100)))/10; |
| 102 | + one = num-((thousand*1000)+(hundred*100)+(ten*10)); |
| 103 | + if (zero==1) { // yes to leading zero |
| 104 | + Wire.beginTransmission(_addr); |
| 105 | + Wire.write(0x01); |
| 106 | + Wire.write(_digits[one]); |
| 107 | + Wire.write(_digits[ten]); |
| 108 | + Wire.write(_digits[hundred]); |
| 109 | + Wire.write(_digits[thousand]); |
| 110 | + Wire.endTransmission(); |
| 111 | + delay(10); |
| 112 | + } else |
| 113 | + if (zero==0) { // no to leading zero |
| 114 | + if (thousand==0) { |
| 115 | + thousand=16; |
| 116 | + } |
| 117 | + if (hundred==0 && num<100) { |
| 118 | + hundred=16; |
| 119 | + } |
| 120 | + if (ten==0 && num<10) { |
| 121 | + ten=16; |
| 122 | + } |
| 123 | + Wire.beginTransmission(_addr); |
| 124 | + Wire.write(1); |
| 125 | + Wire.write(_digits[one]); |
| 126 | + Wire.write(_digits[ten]); |
| 127 | + Wire.write(_digits[hundred]); |
| 128 | + Wire.write(_digits[thousand]); |
| 129 | + Wire.endTransmission(); |
| 130 | + delay(10); |
| 131 | + } |
| 132 | +} |
0 commit comments