plcLib (Arduino): Shifting and Rotating Binary Data

Version 0.9 of the software introduces shift registers which may be used to shift binary data to the left or right, by one bit position at a time.

The above example shows a simple 8-bit shift register, which shifts data one position to the right on each rising edge of the Clock input. New data is shifted in at the left side, while data is discarded as it leaves at the right. The content of individual bits may be read from output connections at the top.

Considering the content of the shift register as a whole, it can be seen that shifting data to the right is equivalent to division by two, since the most significant bit is at the left. Conversely, shifting to the left would be equivalent to multiplication by two.

Creating and Using Shift Registers

The first step is to use the Shift command to create a shift register object, which has a fixed data width of 16-bits. The initial content of the register may optionally be set at this time. For example, the command Shift shift1(0x8888); creates a shift register object called shift1 with an initial hexadecimal content of 0x8888, which is equivalent to 1000 1000 1000 1000 in binary.

A number of other configuration tasks are also required, including definition of the serial data input, clock input (shifting data to the right in this case), reset input and any parallel output connections, as illustrated by the following function block symbol.

The actual coding of the shift register is given by the following sketch.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Shift register: Shift data to the right 

   Connections:
   Serial Input - switch connected to input X0 (Arduino pin A0)
   Clock Input - switch connected to input X1 (Arduino pin A1)
   Reset Input - switch connected to input X2 (Arduino pin A2 )
   Output - LED connected to output Y0 (Arduino pin 3)
   Output - LED connected to output Y1 (Arduino pin 5)
   Output - LED connected to output Y2 (Arduino pin 6)
   Output - LED connected to output Y3 (Arduino pin 9)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

Shift shift1(0x8888);      // Create a 16 bit shift register with initial value 0x8888

                           //        Bit      1 1 1 1    1 1
                           //     Positions   5 4 3 2    1 0 9 8    7 6 5 4    3 2 1 0
                           //                 | | | |    | | | |    | | | |    | | | |
                           //  X0 -> -> -> -> 1 0 0 0 -> 1 0 0 0 -> 1 0 0 0 -> 1 0 0 0 ->
                           //                 | | | |
                           //  X1 -> Clock    | | | |      
                           //                 | | | |
                           //  X2 -> Reset    | | | |
                           //
                           //                 Y Y Y Y
                           //      Outputs    3 2 1 0

unsigned long TIMER0 = 0;  // Define variable used for switch debounce

void setup() {
  setupPLC();              // Setup inputs and outputs
}

void loop() {
  
  in(X0);                  // Read input to shift register from X0
  shift1.inputBit();

  in(X1);                  // Shift Right on rising edge of input X1
  timerOn(TIMER0, 10);     // 10 ms switch debounce delay on X1
  shift1.shiftRight();

  in(X2);                  // Reset the shift register value to zero if X2 = 1
  shift1.reset();

  shift1.bitValue(15);      // Send bit 15 value to output Y3
  out(Y3);
  
  shift1.bitValue(14);      // Send bit 14 value to output Y2
  out(Y2);
  
  shift1.bitValue(13);      // Send bit 13 value to output Y1
  out(Y1);
  
  shift1.bitValue(12);      // Send bit 12 value to output Y0
  out(Y0);
}
Source location: File > Examples > plcLib > ShiftRotate > ShiftRight

Data is shifted right by the rising edge of the clock input taken from a switch connected to input X1, with new data shifted-in at the left taken from input switch X0. (Notice that the potential problem of contact bounce on the clock input is avoided by the use of a 10 ms switch debounce delay.) Input swicth X2 provides a reset input, clearing the shift register to zero when pressed. Parallel outputs are taken from bits 15–12, allowing any newly inputted serial data to be immediately visible on the outputs.

It is straightforward to modify the above sketch to shift data to the left, as shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Shift register: Shift data to the left 

   Connections:
   Serial Input - switch connected to input X0 (Arduino pin A0)
   Clock Input - switch connected to input X1 (Arduino pin A1)
   Reset Input - switch connected to input X2 (Arduino pin A2)
   Output - LED connected to output Y0 (Arduino pin 3)
   Output - LED connected to output Y1 (Arduino pin 5)
   Output - LED connected to output Y2 (Arduino pin 6)
   Output - LED connected to output Y3 (Arduino pin 9)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

Shift shift1(0x1111);      // Create a 16 bit shift register with initial value 0x1111

                           //      Bit      1 1 1 1    1 1
                           //   Positions   5 4 3 2    1 0 9 8    7 6 5 4    3 2 1 0
                           //               | | | |    | | | |    | | | |    | | | |
                           //            <- 0 0 0 1 <- 0 0 0 1 <- 0 0 0 1 <- 0 0 0 1 <- X0
                           //                                                | | | |
                           //  X1 -> Clock                                   | | | |
                           //                                                | | | |
                           //  X2 -> Reset                                   | | | |
                           //
                           //                                                Y Y Y Y
                           //                                     Outputs    3 2 1 0

unsigned long TIMER0 = 0;  // Define variable used for switch debounce

void setup() {
  setupPLC();              // Setup inputs and outputs
}

void loop() {
  
  in(X0);                  // Read input to shift register from X0
  shift1.inputBit();

  in(X1);                  // Shift Left on rising edge of input X1
  timerOn(TIMER0, 10);     // 10 ms switch debounce delay on X1
  shift1.shiftLeft();

  in(X2);                  // Reset the shift register value to zero if X2 = 1
  shift1.reset();

  shift1.bitValue(3);      // Send bit 3 value to output Y3
  out(Y3);
  
  shift1.bitValue(2);      // Send bit 2 value to output Y2
  out(Y2);
  
  shift1.bitValue(1);      // Send bit 1 value to output Y1
  out(Y1);
  
  shift1.bitValue(0);      // Send bit 0 value to output Y0
  out(Y0);
}
Source location: File > Examples > plcLib > ShiftRotate > ShiftLeft

The main differences are firstly the connection of the clock (switch X1) to the shiftLeft input, and secondly the connection of parallel outputs to bits 3–0 of the shift register. (The latter allows data shifted-in at the right to be immediately visible on outputs, as it moves to the left).

Rotating Data

Adding a feedback connection to our shift register from output to input allows data to be rotated in a continuous loop, as shown below.

In fact, the feedback connection may be taken from any convenient output bit, hence creating a circulating bit pattern of any desired width. As an example, the following sketch uses shift register bit 12 as the feedback connection to the serial input (i.e. the 4th bit from the left), to rotate a 4-bit data word to the right.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Shift register: Rotate data to the right 

   Connections:
   Clock Input - switch connected to input X0 (Arduino pin A0)
   Reset Input - switch connected to input X1 (Arduino pin A1)
   Output - LED connected to output Y0 (Arduino pin 3)
   Output - LED connected to output Y1 (Arduino pin 5)
   Output - LED connected to output Y2 (Arduino pin 6)
   Output - LED connected to output Y3 (Arduino pin 9)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

Shift shift1(0x8000);      // Create a shift register with initial value 0x8000
                           // (Leftmost 4 bits are rotated to the right)

                           //    Outputs         Y Y Y Y
                           //                    3 2 1 0
                           //
                           //      Bit           1 1 1 1
                           //   Positions        5 4 3 2
                           //                    | | | |
                           //                 -> 1 0 0 0 ->
                           //                 |           |
                           //                 |           |
                           //  X0 -> Clock    -<--<--<--<--
                           //
                           //  X1 -> Reset

unsigned long TIMER0 = 0;  // Define variable used for switch debounce

void setup() {
  setupPLC();              // Setup inputs and outputs
}

void loop() {
  
  shift1.bitValue(12);     // Read input to shift register from bit 12 at RHS
  shift1.inputBit();

  in(X0);                  // Rotate Right on rising edge of input X0
  timerOn(TIMER0, 10);     // 10 ms switch debounce delay on X0
  shift1.shiftRight();

  in(X1);                  // Reset the shift register value to zero if X1 = 1
  shift1.reset();

  shift1.bitValue(15);      // Send bit 15 value to output Y3
  out(Y3);
  
  shift1.bitValue(14);      // Send bit 14 value to output Y2
  out(Y2);
  
  shift1.bitValue(13);      // Send bit 13 value to output Y1
  out(Y1);
  
  shift1.bitValue(12);      // Send bit 12 value to output Y0
  out(Y0);
}
Source location: File > Examples > plcLib > ShiftRotate > RotateRight

An equivalent sketch to rotate a 4-bit data word to the left would use bit 3 (the 4th bit from the right) as the feedback connection, as shown in the following example.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Shift register: Rotate data to the left 

   Connections:
   Clock Input - switch connected to input X0 (Arduino pin A0)
   Reset Input - switch connected to input X1 (Arduino pin A1)
   Output - LED connected to output Y0 (Arduino pin 3)
   Output - LED connected to output Y1 (Arduino pin 5)
   Output - LED connected to output Y2 (Arduino pin 6)
   Output - LED connected to output Y3 (Arduino pin 9)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

Shift shift1(0x0001);      // Create a shift register with initial value 0x0001
                           // (Rightmost 4 bits are rotated to the left)

                           //    Outputs         Y Y Y Y
                           //                    3 2 1 0
                           //      Bit
                           //   Positions        3 2 1 0
                           //                    | | | |
                           //                 <- 0 0 0 1 <-
                           //                 |           |
                           //                 |           |
                           //  X0 -> Clock    ->-->-->-->--
                           //
                           //  X1 -> Reset

unsigned long TIMER0 = 0;  // Define variable used for switch debounce

void setup() {
  setupPLC();              // Setup inputs and outputs
}

void loop() {
  
  shift1.bitValue(3);       // 'Feedback' input to shift register from bit 3 at LHS
  shift1.inputBit();

  in(X0);                  // Rotate Left on rising edge of input X0
  timerOn(TIMER0, 10);     // 10 ms switch debounce delay on X0
  shift1.shiftLeft();

  in(X1);                  // Reset the shift register value to zero if X1 = 1
  shift1.reset();

  shift1.bitValue(3);      // Send bit 3 value to output Y3
  out(Y3);
  
  shift1.bitValue(2);      // Send bit 2 value to output Y2
  out(Y2);
  
  shift1.bitValue(1);      // Send bit 1 value to output Y1
  out(Y1);
  
  shift1.bitValue(0);      // Send bit 0 value to output Y0
  out(Y0);
}
Source location: File > Examples > plcLib > ShiftRotate > RotateLeft

Related Topics

top