Softata: A 74HC595 Shift Register Class
softata rpipico firmata arduino csharp grove swagger asp.net blockly
Using a 74HC595 as a 1 to 8 DEMUX as previously used with the Bargraph Display. This enables code reuse in other Softata scenarios; in particular for Actuators.
The Bargraph-74HC595 Schematic
About
The 74HC595 is a 1 data pin in and 8 bits out shift register. The latch pin is set low. One byte of data as a sequence of 8 bits is clocked in from the microprocessor via the data pin with clocking sequenced by the clock pin. The latch pin is then set high and the 8 bits are presented to the storage register. Internally there is 8 D-Type Flip Flops with the Qout of each connected to Din of the next. The 8 Qs are only latched to the 8 bit storage register when the latch pin goes high. Also, the storage register outputs are controlled by the OE (output enable) pin.
THe Arduino code the 8 bits are shifted out using the command
shiftOut(dataPin, clockPin, ORDER , byteData)
- ORDER is either
MSBFIRST
orLSBFIRST
meaning the most significant bit or the least significant bit is shifted first. - byeData, The 8 bit data entity to be shifted.
Multiple ICs can be cascaded so that multiple bytes can be shifted out from the microprocessor. There is an output pin from the first, that becomes the data in for the second, etc. All cascaded ICs use that the same latch and clock signals.
“The Serial out pin (pin 9) of the first shift register is connected to the serial data input (pin 14) of the second register, etc.”
To shift out multiple bytes, say 16 bits, you separate the 16 bits into two 8 byte values (MSByte and LSByte) and send one then the other.
- If using MSBFIRST, MSByte (most significant byte) is sent first
- Otherwise (LSBFIRST) LSByte (least significant byte) is sent first
Code Refactoring
The ‘595 shift register code in the Bargraph display code has been refactored into a separate class IC_74HC595_ShiftRegister
. This adds a header and source file for the class to the Arduino sketch /src
folder along with the other Softata device class files.
class IC_74HC595_ShiftRegister
{
public:
IC_74HC595_ShiftRegister();
IC_74HC595_ShiftRegister(byte * settings, byte numSettings);
bool Setup();
bool Setup(byte * settings, byte numSettings);
virtual bool SetBitState(bool state,int index);
virtual bool SetBit(int index );
virtual bool ClearBit(int index );
virtual bool ToggleBit(int index );
virtual bool Write(int num, int numBytes=1);
// Can daisy chain two 74HC595s as per top link. //Assume data is 16 bits
}
- There is a default constructor with specific Pico pin connections, and a more general constructor enabling specification of the pins used.
- The Setup methods are only used by the constructors but are public in keeping with the Actuators interface.
- The Write method is for shifting out one or two bytes. The
numBytes
parameter (default 1) determines which. It uses LSBFIRST. There is an internal functionwrite595()
that does the actual one or two byte shifting and takes a direction parameter so can action LSBFirst or MSBFirst. - The Bargraph uses this class with the
WriteString()
method that converts the string to a byte then calls this. - Four individual bit manipulation methods were included as shown and have been added to the general Actuator class, and implemented as dummies in the Servo class. The modified Actuator header file can be viewed here.
The ic_74hc595_shiftRegister.cpp
code can be viewed here
The Bargraph class then instantiates the new class and then uses pointer references to the instantiated object’s methods (->). The modified Bargraph source file can be viewed here
Next
The ‘595 code has been refactored from the Bargraph class into a new class. Some individual bit manipulation methods were added to the new class. Next will be an actuator that uses this new class, a Demux class that provides 8 or 16 bits of general purpose output requiring only 3 pins from the Pico.
Topic | Subtopic | |
< Prev: | Note to Self | Handling a Pull Request |
This Category Links | ||
Category: | Softata Index: | Softata |
Next: > | Softata | Custom Blocks via ASP.NET Controller |
< Prev: | Softata | Latest NetCoreBlockly Updates |