- What it is about: Controlling 8 or 16 Leds using only 3 pins
- I used a 1 micro farad capacitor instead of 0.1 micro farad
- The red leds generally are brighter than the green one, so I use a 470Ohm resistor for the red led
- The Shift Register 74HC595, use regular output pin, so for the Trinket Pro which is compatible with the Arduino Uno, you can use the exact same pin. You can use any output pin.
- When using 2 Register 74HC595, you send 2 bytes, the first one goes to register 1 and the second byte goes to register 2. See method Send16BitValue() in source code below.
Github: Source code
//////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Register74HC595_16Bit //////////////////////////////////////////////////////////////////////////////////////////////////////////// class Register74HC595_16Bit { private: int _latchPin; // Pin connected to ST_CP of 74HC595 int _clockPin; // Pin connected to SH_CP of 74HC595 int _dataPin; // Pin connected to DS of 74HC595 public: Register74HC595_16Bit(int latchPin, int clockPin, int dataPin); void Send16BitValue(unsigned int v); void AnimateOneLeftToRightAndRightToLeft2Leds(int waitTime, int count); void AnimateOneLeftToRightAndRightToLeft1Leds(int waitTime, int count); }; //////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Register74HC595_16Bit //////////////////////////////////////////////////////////////////////////////////////////////////////////// Register74HC595_16Bit::Register74HC595_16Bit(int latchPin, int clockPin, int dataPin) { this->_latchPin = latchPin; this->_clockPin = clockPin; this->_dataPin = dataPin; //set pins to output so you can control the shift register Board.SetPinMode(latchPin, OUTPUT); Board.SetPinMode(clockPin, OUTPUT); Board.SetPinMode(dataPin, OUTPUT); } void Register74HC595_16Bit::Send16BitValue(unsigned int v) { byte low = (byte)(v & 0xff); byte high = (byte)((v >> 8) & 0xff); digitalWrite(this->_latchPin, LOW); shiftOut(this->_dataPin, this->_clockPin, MSBFIRST, high); shiftOut(this->_dataPin, this->_clockPin, MSBFIRST, low); digitalWrite(this->_latchPin, HIGH); } void Register74HC595_16Bit::AnimateOneLeftToRightAndRightToLeft2Leds(int waitTime, int count) { for (int counter = 0; counter < count; counter++) { unsigned int v = 256 + 1; for (int i = 0; i <= 7; i++) { //Board.Trace(StringFormat.Format("[%d]v:%ui", i, v)); this->Send16BitValue(v); Board.Delay(i == 0 ? waitTime * 2 : waitTime); // Wait more for the first step v = v << 1; } v = 32768 + 128; for (int i = 0; i <= 7; i++) { //Board.Trace(StringFormat.Format("[%d]v:%ui", i, v)); this->Send16BitValue(v); Board.Delay(i == 7 ? waitTime * 2 : waitTime); // Wait more for the first step v = v >> 1; } this->Send16BitValue(0); } } void Register74HC595_16Bit::AnimateOneLeftToRightAndRightToLeft1Leds(int waitTime, int count) { unsigned int _16Bits[] = { 1, // 1 2, // 2 4, // 3 8, // 4 16, // 5 32, // 6 64, // 7 128, // 8 256, // 9 512, // 10 1024, // 11 2048, // 12 4096, // 13 8192, // 14 16384, // 15 32768 // 16 }; for (int counter = 0; counter < count; counter++) { unsigned int v = 256 + 1; for (int i = 0; i < 16; i++) { //Board.Trace(StringFormat.Format("[%d]v:%ui", i, _16Bits[i])); this->Send16BitValue(_16Bits[i]); Board.Delay(waitTime); } for (int i = 15; i >=0; i--) { //Board.Trace(StringFormat.Format("[%d]v:%ui", i, _16Bits[i])); this->Send16BitValue(_16Bits[i]); Board.Delay(waitTime); } this->Send16BitValue(0); } }
No comments:
Post a Comment