150 lines
2.8 KiB
C++
Raw Normal View History

//
// FILE: FastShiftIn.cpp
// AUTHOR: Rob Tillaart
2022-11-05 18:50:25 +01:00
// VERSION: 0.3.0
2020-11-27 11:16:22 +01:00
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
// DATE: 2013-09-29
2020-11-27 11:16:22 +01:00
// URL: https://github.com/RobTillaart/FastShiftIn
2022-11-05 12:41:07 +01:00
//
// HISTORY: see changelog.md
2021-01-29 12:31:58 +01:00
#include "FastShiftIn.h"
2021-01-29 12:31:58 +01:00
2022-11-05 18:50:25 +01:00
FastShiftIn::FastShiftIn(uint8_t dataIn, uint8_t clockPin, uint8_t bitOrder)
{
2022-11-05 18:50:25 +01:00
_bitOrder = bitOrder;
2021-01-29 12:31:58 +01:00
_value = 0;
2022-11-05 18:50:25 +01:00
pinMode(dataIn, INPUT);
pinMode(clockPin, OUTPUT);
2022-11-05 12:41:07 +01:00
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
2022-11-05 18:50:25 +01:00
digitalWrite(clockPin, LOW); // assume rising pulses from clock
2020-11-27 11:16:22 +01:00
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
2022-11-05 18:50:25 +01:00
uint8_t _port = digitalPinToPort(dataIn);
_dataInRegister = portInputRegister(_port);
_dataInBit = digitalPinToBitMask(dataIn);
2020-11-27 11:16:22 +01:00
2022-11-05 18:50:25 +01:00
_port = digitalPinToPort(clockPin);
_clockRegister = portOutputRegister(_port);
_clockBit = digitalPinToBitMask(clockPin);
2020-11-27 11:16:22 +01:00
2021-01-29 12:31:58 +01:00
#else
2022-11-05 18:50:25 +01:00
_dataPinIn = dataIn;
_clockPin = clockPin;
2020-11-27 11:16:22 +01:00
#endif
2022-11-05 18:50:25 +01:00
2020-11-27 11:16:22 +01:00
}
2021-12-17 15:14:55 +01:00
int FastShiftIn::read()
{
2022-11-05 18:50:25 +01:00
if (_bitOrder == LSBFIRST)
2020-11-27 11:16:22 +01:00
{
return readLSBFIRST();
}
return readMSBFIRST();
}
2021-12-17 15:14:55 +01:00
2020-11-27 11:16:22 +01:00
int FastShiftIn::readLSBFIRST()
{
2021-01-29 12:31:58 +01:00
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
2022-11-05 18:50:25 +01:00
uint8_t rv = 0;
uint8_t cbmask1 = _clockBit;
uint8_t cbmask2 = ~_clockBit;
uint8_t inmask1 = _dataInBit;
2020-11-27 11:16:22 +01:00
for (uint8_t m = 1; m > 0; m <<= 1)
{
uint8_t oldSREG = SREG;
2021-01-29 12:31:58 +01:00
noInterrupts();
2022-11-16 16:07:44 +01:00
// clock pulse HIGH
2022-11-05 18:50:25 +01:00
*_clockRegister |= cbmask1;
// read one bit
if ((*_dataInRegister & inmask1) > 0) rv |= m;
2022-11-16 16:07:44 +01:00
// clock pulse LOW
2022-11-05 18:50:25 +01:00
*_clockRegister &= cbmask2;
2020-11-27 11:16:22 +01:00
SREG = oldSREG;
}
2022-11-05 18:50:25 +01:00
_value = rv;
return rv;
2021-01-29 12:31:58 +01:00
#else
2022-11-05 18:50:25 +01:00
2021-01-29 12:31:58 +01:00
// reference implementation
2022-11-05 18:50:25 +01:00
_value = shiftIn(_dataPinIn, _clockPin, LSBFIRST);
2021-01-29 12:31:58 +01:00
return _value;
2022-11-05 18:50:25 +01:00
2021-01-29 12:31:58 +01:00
#endif
2020-11-27 11:16:22 +01:00
}
2021-12-17 15:14:55 +01:00
2020-11-27 11:16:22 +01:00
int FastShiftIn::readMSBFIRST()
{
2021-01-29 12:31:58 +01:00
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
2022-11-05 18:50:25 +01:00
uint8_t rv = 0;
uint8_t cbmask1 = _clockBit;
uint8_t cbmask2 = ~_clockBit;
uint8_t inmask1 = _dataInBit;
2022-11-05 18:50:25 +01:00
for (uint8_t m = 0x80; m > 0; m >>= 1)
2020-11-27 11:16:22 +01:00
{
uint8_t oldSREG = SREG;
2021-01-29 12:31:58 +01:00
noInterrupts();
2022-11-16 16:07:44 +01:00
// clock pulse HIGH
2022-11-05 18:50:25 +01:00
*_clockRegister |= cbmask1;
// read one bit
if ((*_dataInRegister & inmask1) > 0) rv |= m;
2022-11-16 16:07:44 +01:00
// clock pulse LOW
2022-11-05 18:50:25 +01:00
*_clockRegister &= cbmask2;
2020-11-27 11:16:22 +01:00
SREG = oldSREG;
}
2022-11-05 18:50:25 +01:00
_value = rv;
return rv;
2020-11-27 11:16:22 +01:00
2021-01-29 12:31:58 +01:00
#else
2022-11-05 18:50:25 +01:00
// reference implementation
_value = shiftIn(_dataPinIn, _clockPin, MSBFIRST);
2021-01-29 12:31:58 +01:00
return _value;
2022-11-05 18:50:25 +01:00
2021-01-29 12:31:58 +01:00
#endif
2022-11-05 18:50:25 +01:00
}
2020-11-27 11:16:22 +01:00
2021-12-17 15:14:55 +01:00
2022-11-05 18:50:25 +01:00
int FastShiftIn::lastRead(void)
{
return _value;
};
2021-01-29 12:31:58 +01:00
bool FastShiftIn::setBitOrder(const uint8_t bitOrder)
2020-11-27 11:16:22 +01:00
{
2021-01-29 12:31:58 +01:00
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
{
2022-11-05 18:50:25 +01:00
_bitOrder = bitOrder;
2021-01-29 12:31:58 +01:00
return true;
};
return false;
2020-11-27 11:16:22 +01:00
}
2021-12-17 15:14:55 +01:00
2022-11-05 18:50:25 +01:00
uint8_t FastShiftIn::getBitOrder(void)
{
return _bitOrder;
};
2020-11-27 11:16:22 +01:00
// -- END OF FILE --
2022-11-05 18:50:25 +01:00