Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - Shiftregister

Deutsch · English

Shift Registers

The sample code is for an ATtiny44 and the shift registers used are the 74HC165 for reading and the 74HC595 for writing.

Warning: The read routine is not compatible with Arduino shiftIn (...! The Arduino read function is for other shift registers. The clock must be controlled differently.

If you want to swap MSB to LSB, you simply have to swap downto to to.

Port access

For how the shift register commands access the ports, see here: GPIO output.

Do not forget to set the outputs as outputs (DDRx).

Port constants

The pins used for the ATtiny44 and the shift register. With the AVR you can use any pins. I have declared the ports, except the data port, globally, so access from the shift registers is a little faster. If you declared them locally, everything would be more complicated because we use direct port access. Arduino does this differently, but it is about 10 times slower.

<thead>

</thead> <tbody>

</tbody>

A variable is declared for the port, this has the advantage that you do not have to change all ports in the shift register functions.

pascal
  var
   shiftPORT : byte absolute PORTA;
   shiftDDR  : byte absolute DDRA;
   shiftPIN  : byte absolute PINA;
 const
   dataOutPin = 0;
   dataInPin = 1;
   latchPin = 2;
   clockPin = 3;

Shift register write 74HC595

pascal
procedure shiftOut595(val : byte);
  var
    i : byte;
begin
  for i : = 7 downto 0 do // MSB FIRST
    begin 
      if(val and (1 shl i)) <> 0 then 
        begin
          shiftPORT := shiftPORT or (1 shl dataOutPin);
        end 
      else 
        begin
          shiftPORT := shiftPORT and not (1 shl dataOutPin);
        end;

      shiftPORT := shiftPORT or (1 shl clockPin);
      shiftPORT := shiftPORT and not (1 shl clockPin);
    end;
end;

Shift register read 74HC165

pascal
function ShiftIn165 : byte;
 var
   i : byte;
 begin
   Result := 0;

   for i := 0 to 7 do  // LSB FIRST
     begin 
       Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin);
       shiftPORT := shiftPORT or (1 shl clockPin);
       shiftPORT := shiftPORT and not (1 shl clockPin);
     end;
 end ;

Arduino shift register

This version corresponds to Arduino's shiftIn.

pascal
function ShiftIn : byte;
  var
    i : byte;
begin
  Result := 0;

  for i := 0 to 7 do  // LSB FIRST 
    begin 
      shiftPORT := shiftPORT or (1 shl clockPin);
      Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin);
      shiftPORT := shiftPORT and not (1 shl clockPin);
    end;
end;

Shift register examples

Read from a shift register

Before the shift register can be read out, the port status must be written to the latch register, this is done via the LatchPin.

Example for a 74HC165

pascal
 var
   b : byte;
 begin

   // Latch data
   shiftPORT := shiftPORT and not (1 shl latchPin);
   shiftPORT := shiftPORT or (1 shl latchPin);

   // Read shift register
   b := ShiftIn74HC165;

Example for cascaded 74HC165

pascal
 var
   b0, bn : byte;
 begin

   // Latch data
   shiftPORT := shiftPORT and not (1 shl latchPin);
   shiftPORT := shiftPORT or (1 shl latchPin);

   // Read shift register
   b0 := ShiftIn74HC165;

   // ...

   bn := ShiftIn74HC165;

Write to a shift register

If the data is written into the shift register, you have to add the latch clock that the current outputs become active.

Example for a 74HC595

pascal
 var
   b : byte;
 begin
   b := 123;

   // Write shift register
   shiftOut595(b);

   // Latch data
   shiftPORT := shiftPORT and not (1 shl latchPin);
   shiftPORT := shiftPORT or (1 shl latchPin);

Example for cascaded 74HC595

pascal
 var
   b0, bn : byte;
 begin
   b0 := 123;

   // ...

   bn := 789;

   // Write shift register
   shiftOut595(b0);
   
   // ...
   
   shiftOut595(bn);

   // Latch data
   shiftPORT := shiftPORT and not (1 shl latchPin);
   shiftPORT := shiftPORT or (1 shl latchPin);

See also


From the Free Pascal wiki, page "AVR Embedded Tutorial - Shiftregister", as it stood on 2023-08-02 after a change by Zave2023, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.