diff --git a/src/dio.cpp b/src/dio.cpp index eb17376..5acda8e 100644 --- a/src/dio.cpp +++ b/src/dio.cpp @@ -27,6 +27,9 @@ void initDio(bool restore) hspi = new SPIClass(HSPI); hspi->begin(H_CLK, H_MISO, H_MOSI); + hspi->setDataMode(SPI_MODE3); + hspi->setBitOrder(MSBFIRST); + hspi->setFrequency(1000000); dioRwInOut(); @@ -53,21 +56,24 @@ uint8_t dioRwInOut() { uint8_t receivedVal = 0; - //Eingänge ins Eingangsregister des HC165 laden (High-Flanke) + // PL Pin auf LOW digitalWrite(IO_DO_PL, LOW); - delayMicroseconds(5); - digitalWrite(IO_DO_PL, HIGH); - delayMicroseconds(5); - hspi->beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE2)); - receivedVal = hspi->transfer(doOutData); - hspi->endTransaction(); + //Ausgaänge ins HC595 schreiben + hspi->transfer(doOutData); - //Ausgänge setzen HC595 (High-Flanke) - digitalWrite(IO_DO_PL, LOW); - delayMicroseconds(5); + // SPI Mode wechseln + hspi->setDataMode(SPI_MODE2); + + // Ausgänge von HC595 übernehmen und HC165 einlesen (HIGH-Flanke) digitalWrite(IO_DO_PL, HIGH); + // Eingänge des HC165 lesen + receivedVal = hspi->transfer(0); + + // Spi Mode wieder zurück wechseln + hspi->setDataMode(SPI_MODE3); + return receivedVal; }