Add TasShiftIn function

Add TasShiftIn function for future use.
This commit is contained in:
Theo Arends 2022-04-03 18:20:07 +02:00
parent a80a100efb
commit e047c90228
2 changed files with 54 additions and 27 deletions

View File

@ -1975,6 +1975,53 @@ uint8_t Dec2Bcd(uint8_t n) {
return n + 6 * (n / 10);
}
/*********************************************************************************************/
uint8_t TasShiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
uint8_t value = 0;
for (uint32_t i = 0; i < 8; ++i) {
digitalWrite(clockPin, HIGH);
#ifdef ESP32
delayMicroseconds(1);
#endif
if(bitOrder == LSBFIRST) {
value |= digitalRead(dataPin) << i;
} else {
value |= digitalRead(dataPin) << (7 - i);
}
#ifdef ESP32
delayMicroseconds(1);
#endif
digitalWrite(clockPin, LOW);
#ifdef ESP32
delayMicroseconds(1);
#endif
}
return value;
}
void TasShiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) {
for (uint32_t i = 0; i < 8; i++) {
if(bitOrder == LSBFIRST) {
digitalWrite(dataPin, !!(val & (1 << i)));
} else {
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
}
#ifdef ESP32
delayMicroseconds(1);
#endif
digitalWrite(clockPin, HIGH);
#ifdef ESP32
delayMicroseconds(1);
#endif
digitalWrite(clockPin, LOW);
#ifdef ESP32
delayMicroseconds(1);
#endif
}
}
/*********************************************************************************************\
* Sleep aware time scheduler functions borrowed from ESPEasy
\*********************************************************************************************/

View File

@ -87,26 +87,6 @@ struct HX {
/*********************************************************************************************/
uint8_t HxShiftIn(void) {
uint8_t value = 0;
for (uint32_t i = 0; i < 8; ++i) {
digitalWrite(Hx.pin_sck, HIGH);
#ifdef ESP32
delayMicroseconds(1); // could be required for faster mcu's
#endif
value |= digitalRead(Hx.pin_dout) << (7 - i);
#ifdef ESP32
delayMicroseconds(1); // could be required for faster mcu's
#endif
digitalWrite(Hx.pin_sck, LOW);
#ifdef ESP32
delayMicroseconds(1); // could be required for faster mcu's
#endif
}
return value;
}
bool HxIsReady(uint16_t timeout)
{
// A reading can take up to 100 mS or 600mS after power on
@ -125,20 +105,20 @@ long HxRead(void)
uint8_t filler = 0x00;
// pulse the clock pin 24 times to read the data
// data[2] = shiftIn(Hx.pin_dout, Hx.pin_sck, MSBFIRST);
// data[1] = shiftIn(Hx.pin_dout, Hx.pin_sck, MSBFIRST);
// data[0] = shiftIn(Hx.pin_dout, Hx.pin_sck, MSBFIRST);
data[2] = HxShiftIn();
data[1] = HxShiftIn();
data[0] = HxShiftIn();
data[2] = TasShiftIn(Hx.pin_dout, Hx.pin_sck, MSBFIRST);
data[1] = TasShiftIn(Hx.pin_dout, Hx.pin_sck, MSBFIRST);
data[0] = TasShiftIn(Hx.pin_dout, Hx.pin_sck, MSBFIRST);
// set the channel and the gain factor for the next reading using the clock pin
for (unsigned int i = 0; i < HX_GAIN_128; i++) {
digitalWrite(Hx.pin_sck, HIGH);
#ifdef ESP32
delayMicroseconds(1); // could be required for faster mcu's
delayMicroseconds(1);
#endif
digitalWrite(Hx.pin_sck, LOW);
#ifdef ESP32
delayMicroseconds(1);
#endif
}
// Replicate the most significant bit to pad out a 32-bit signed integer