SW7495 4-Bit (non-latching) Shift Register

Library for sending data to the 7495 shift register. This is a simple 4-bit non-latching type which can work in parallel or serial mode.

This uses the Direct IO library to speed up the setting of the pins used to control the chip.

ShiftReg_7495.h

/* Project: 7495 4-Bit Shift Register Interfacing.
 * File: Shift_7495.h
 * Description: Interface with a 7495 4-bit shift register in
 *     parallel or serial mode.
 *     Uses the DirectIO library.
 *
 * Copyright (C) 2014 Marc Symonds
 * All rights reserved.
 *
 * This software may be used and redistributed, with or without
 * modification, as long as it is understood that this software
 * is provided as-is without any explicit or implied warranties
 * of merchantability or fitness of purpose.
 */
 
#ifndef ShiftReg_7495_h
#define ShiftReg_7495_h

#include "Arduino.h"
#include "DirectIO.h"

class ShiftReg_7495
{
  public:
    ShiftReg_7495(byte clockPin, byte dataPin); // Serial mode.
    ShiftReg_7495(byte clockPin, byte aPin, byte bPin, byte cPin); // Parallel mode.

    void setBit(byte bit, boolean state);
    void send();
    void sendPrime();
    void sendTrigger();
    void clear();

  private:
    DIO_SetPinFPtr _clockPin;
    DIO_SetPinFPtr _dataPin;
    
    DIO_SetPinFPtr _aPin;
    DIO_SetPinFPtr _bPin;
    DIO_SetPinFPtr _cPin;

    boolean _currBits[3];
    boolean _newBits[3];

    boolean _serialMode;

    void sendBit(boolean state);
};

#endif

ShiftReg_7495.cpp

/* Project: 7495 4-Bit Shift Register Interfacing.
 * File: Shift_7495.cpp
 * Description: Interface with a 7495 4-bit shift register in
 *     parallel or serial mode.
 *     Uses the DirectIO library.
 *
 * Copyright (C) 2014 Marc Symonds
 * All rights reserved.
 *
 * This software may be used and redistributed, with or without
 * modification, as long as it is understood that this software
 * is provided as-is without any explicit or implied warranties
 * of merchantability or fitness of purpose.
 */
 
#include "Arduino.h"
#include "ShiftReg_7495.h"
#include "DirectIO.h"

// Constructor for serial mode.

ShiftReg_7495::ShiftReg_7495(byte clockPin, byte dataPin)
{
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  _clockPin = DIO_GetSetPinFunction(clockPin);
  _dataPin = DIO_GetSetPinFunction(dataPin);

  _clockPin(true);

  _serialMode = true;

  clear();
}

// Constructor for parallel mode.

ShiftReg_7495::ShiftReg_7495(byte clockPin, byte aPin, byte bPin, byte cPin)
{
  pinMode(clockPin, OUTPUT);
  pinMode(aPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  pinMode(cPin, OUTPUT);

  _clockPin = DIO_GetSetPinFunction(clockPin);
  _aPin = DIO_GetSetPinFunction(aPin);
  _bPin = DIO_GetSetPinFunction(bPin);
  _cPin = DIO_GetSetPinFunction(cPin);

  _clockPin(true);

  _serialMode = false;

  clear();
}

void ShiftReg_7495::setBit(byte bit, boolean state)
{
  _newBits[bit] = state;
}

// Send the data to the 7495 - Serial or parallel.

void ShiftReg_7495::send()
{
  if (_serialMode)
  {
    if (_currBits[0] == _newBits[0] && _currBits[1] == _newBits[1] && _currBits[2] == _newBits[2])
      ; // Don't need to do anything, the register aleady contains the bits we are going to show.
    else
    {
      if (_newBits[2] == _currBits[1] && _newBits[1] == _currBits[0])
        sendBit(_newBits[0]);
      else
      {
        if (_newBits[2] != _currBits[0])
          sendBit(_newBits[2]);

        sendBit(_newBits[1]);
        sendBit(_newBits[0]);
      }

      _currBits[0] = _newBits[0];
      _currBits[1] = _newBits[1];
      _currBits[2] = _newBits[2];
    }
  }
  else
  {
    sendPrime();
    sendTrigger();
  }
}

void ShiftReg_7495::sendPrime()
{
  _clockPin(true);

  _aPin(_newBits[0]);
  _bPin(_newBits[1]);
  _cPin(_newBits[2]);
}

void ShiftReg_7495::sendTrigger()
{
  _clockPin(false);
}

void ShiftReg_7495::sendBit(boolean state)
{
  _clockPin(true);
  _dataPin(state);
  _clockPin(false);
}

void ShiftReg_7495::clear()
{
  for (byte i = 0 ; i < 3 ; i++)
  {
    _currBits[i] = true;
    _newBits[i] = false;
  }

  send();
}

keywords.txt

ShiftReg_7495	KEYWORD1

setBit	KEYWORD2
send	KEYWORD2
sendPrime	KEYWORD2
sendTrigger	KEYWORD2
clear	KEYWORD2

2 Comments

  1. Bharat R says:

    Hi

     

    Can you provide data sheet of SW7495J  Steward Brand pls

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Spam Blocker