shift register

Dependents:   Enrico_newproject_copy Enrico_copy Enrico_copy2 Project_ORBE

Fork of Shifter by marco valli

Shifter.cpp

Committer:
billycorgan123
Date:
2018-05-06
Revision:
2:7e9a01a1a84d
Parent:
1:b6a66e7e1947

File content as of revision 2:7e9a01a1a84d:

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

#define LOW 0
#define HIGH 1
DigitalOut SER_Pin(D4);         //pin 14 su 595
DigitalOut RCLK_Pin(D3);        //pin 11 su 595
DigitalOut SRCLK_Pin(D2);       //pin 12 su 595
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); }
}