shift register

Dependents:   Enrico_newproject

Shifter.cpp

Committer:
billycorgan123
Date:
2018-02-21
Revision:
0:7af9b977e6e9

File content as of revision 0:7af9b977e6e9:

// Include the standard types
#include "Shifter.h"

#define LOW 0
#define HIGH 1
DigitalOut SER_Pin(D4);
DigitalOut RCLK_Pin(D3);
DigitalOut SRCLK_Pin(D2);
int Number_of_Registers=2;

void Shifter::write()
{
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
RCLK_Pin.write(LOW);
  
  //iterate through the registers
  for(int i = Number_of_Registers - 1; i >=  0; i--)
  {
    //iterate through the bits in each registers
    for(int j = 8 - 1; j >=  0; j--)
    {
      SRCLK_Pin.write(LOW);   
      wait_us(1);
      int val = shiftRegisters[i] & (1 << j);
      SER_Pin.write(val);
      wait_us(1);
      SRCLK_Pin.write(HIGH);
    }
   
  }
  RCLK_Pin.write(HIGH);
}
    
void Shifter::setPin(int index, bool val)
{
    int byteIndex = index/8;
    int bitIndex = index % 8;
    char current = shiftRegisters[byteIndex];
    current &= ~(1 << bitIndex); //clear the bit
    current |= val << bitIndex; //set the bit
    shiftRegisters[byteIndex] = current; //set the value
}

void Shifter::setAll(bool val)
{
//set all register pins to LOW  
  for(int i = Number_of_Registers * 8 - 1; i >=  0; i--){  setPin(i, val); }
}

void Shifter::clear()
{
//set all register pins to LOW  
  for(int i = Number_of_Registers * 8 - 1; i >=  0; i--){ setPin(i, LOW); }
}