11//
22// FILE: FastShiftIn.cpp
33// AUTHOR: Rob Tillaart
4- // VERSION: 0.2.1
4+ // VERSION: 0.2.2
55// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
66// DATE: 2013-09-29
77// URL: https://github.com/RobTillaart/FastShiftIn
88//
99
1010#include " FastShiftIn.h"
1111
12+
1213FastShiftIn::FastShiftIn (const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
1314{
1415 _bitorder = bitOrder;
15-
16+ _value = 0 ;
1617 pinMode (datapin, INPUT);
1718 pinMode (clockpin, OUTPUT);
19+ // https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
20+ digitalWrite (clockpin, LOW); // assume rising pulses from clock
1821
1922#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
2023
@@ -32,7 +35,7 @@ FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const ui
3235
3336#else // reference implementation
3437
35- // reuse these vars as pin to save some space
38+ // reuse these local vars as pin to save some space
3639 _databit = datapin;
3740 _clockbit = clockpin;
3841
@@ -48,19 +51,18 @@ int FastShiftIn::read()
4851 return readMSBFIRST ();
4952}
5053
51- #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
52-
5354int FastShiftIn::readLSBFIRST ()
5455{
55- uint8_t value = 0 ;
56+ #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
57+ uint8_t value = 0 ;
5658 uint8_t cbmask1 = _clockbit;
5759 uint8_t cbmask2 = ~_clockbit;
58- uint8_t dbmask = _databit;
60+ uint8_t dbmask = _databit;
5961
6062 for (uint8_t m = 1 ; m > 0 ; m <<= 1 )
6163 {
6264 uint8_t oldSREG = SREG;
63- cli ();
65+ noInterrupts ();
6466 *_clockin |= cbmask1;
6567 if ((*_datain & dbmask) > 0 )
6668 {
@@ -71,19 +73,26 @@ int FastShiftIn::readLSBFIRST()
7173 }
7274 _value = value;
7375 return _value;
76+
77+ #else // reference implementation
78+
79+ _value = shiftIn (_databit, _clockbit, LSBFIRST);
80+ return _value;
81+ #endif
7482}
7583
7684int FastShiftIn::readMSBFIRST ()
7785{
78- uint8_t value = 0 ;
86+ #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
87+ uint8_t value = 0 ;
7988 uint8_t cbmask1 = _clockbit;
8089 uint8_t cbmask2 = ~cbmask1;
81- uint8_t dbmask = _databit;
90+ uint8_t dbmask = _databit;
8291
8392 for (uint8_t n = 128 ; n > 0 ; n >>= 1 )
8493 {
8594 uint8_t oldSREG = SREG;
86- cli ();
95+ noInterrupts ();
8796 *_clockin |= cbmask1;
8897 if ((*_datain & dbmask) > 0 )
8998 {
@@ -94,20 +103,22 @@ int FastShiftIn::readMSBFIRST()
94103 }
95104 _value = value;
96105 return _value;
97- }
98-
99- #else // reference implementation - note this has no cli()
106+
107+ #else // reference implementation
100108
101- int FastShiftIn::readLSBFIRST ()
102- {
103- return shiftIn (_databit, _clockbit, LSBFIRST);
109+ _value = shiftIn (_databit, _clockbit, MSBFIRST);
110+ return _value;
111+ # endif
104112}
105113
106- int FastShiftIn::readMSBFIRST ( )
114+ bool FastShiftIn::setBitOrder ( const uint8_t bitOrder )
107115{
108- return shiftIn (_databit, _clockbit, MSBFIRST);
116+ if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
117+ {
118+ _bitorder = bitOrder;
119+ return true ;
120+ };
121+ return false ;
109122}
110123
111- #endif
112-
113124// -- END OF FILE --
0 commit comments