1
1
//
2
2
// FILE: FastShiftIn.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.2.1
4
+ // VERSION: 0.2.2
5
5
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
6
6
// DATE: 2013-09-29
7
7
// URL: https://github.com/RobTillaart/FastShiftIn
8
8
//
9
9
10
10
#include " FastShiftIn.h"
11
11
12
+
12
13
FastShiftIn::FastShiftIn (const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
13
14
{
14
15
_bitorder = bitOrder;
15
-
16
+ _value = 0 ;
16
17
pinMode (datapin, INPUT);
17
18
pinMode (clockpin, OUTPUT);
19
+ // https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
20
+ digitalWrite (clockpin, LOW); // assume rising pulses from clock
18
21
19
22
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
20
23
@@ -32,7 +35,7 @@ FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const ui
32
35
33
36
#else // reference implementation
34
37
35
- // reuse these vars as pin to save some space
38
+ // reuse these local vars as pin to save some space
36
39
_databit = datapin;
37
40
_clockbit = clockpin;
38
41
@@ -48,19 +51,18 @@ int FastShiftIn::read()
48
51
return readMSBFIRST ();
49
52
}
50
53
51
- #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
52
-
53
54
int FastShiftIn::readLSBFIRST ()
54
55
{
55
- uint8_t value = 0 ;
56
+ #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
57
+ uint8_t value = 0 ;
56
58
uint8_t cbmask1 = _clockbit;
57
59
uint8_t cbmask2 = ~_clockbit;
58
- uint8_t dbmask = _databit;
60
+ uint8_t dbmask = _databit;
59
61
60
62
for (uint8_t m = 1 ; m > 0 ; m <<= 1 )
61
63
{
62
64
uint8_t oldSREG = SREG;
63
- cli ();
65
+ noInterrupts ();
64
66
*_clockin |= cbmask1;
65
67
if ((*_datain & dbmask) > 0 )
66
68
{
@@ -71,19 +73,26 @@ int FastShiftIn::readLSBFIRST()
71
73
}
72
74
_value = value;
73
75
return _value;
76
+
77
+ #else // reference implementation
78
+
79
+ _value = shiftIn (_databit, _clockbit, LSBFIRST);
80
+ return _value;
81
+ #endif
74
82
}
75
83
76
84
int FastShiftIn::readMSBFIRST ()
77
85
{
78
- uint8_t value = 0 ;
86
+ #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
87
+ uint8_t value = 0 ;
79
88
uint8_t cbmask1 = _clockbit;
80
89
uint8_t cbmask2 = ~cbmask1;
81
- uint8_t dbmask = _databit;
90
+ uint8_t dbmask = _databit;
82
91
83
92
for (uint8_t n = 128 ; n > 0 ; n >>= 1 )
84
93
{
85
94
uint8_t oldSREG = SREG;
86
- cli ();
95
+ noInterrupts ();
87
96
*_clockin |= cbmask1;
88
97
if ((*_datain & dbmask) > 0 )
89
98
{
@@ -94,20 +103,22 @@ int FastShiftIn::readMSBFIRST()
94
103
}
95
104
_value = value;
96
105
return _value;
97
- }
98
-
99
- #else // reference implementation - note this has no cli()
106
+
107
+ #else // reference implementation
100
108
101
- int FastShiftIn::readLSBFIRST ()
102
- {
103
- return shiftIn (_databit, _clockbit, LSBFIRST);
109
+ _value = shiftIn (_databit, _clockbit, MSBFIRST);
110
+ return _value;
111
+ # endif
104
112
}
105
113
106
- int FastShiftIn::readMSBFIRST ( )
114
+ bool FastShiftIn::setBitOrder ( const uint8_t bitOrder )
107
115
{
108
- return shiftIn (_databit, _clockbit, MSBFIRST);
116
+ if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
117
+ {
118
+ _bitorder = bitOrder;
119
+ return true ;
120
+ };
121
+ return false ;
109
122
}
110
123
111
- #endif
112
-
113
124
// -- END OF FILE --
0 commit comments