Last active
April 8, 2023 09:31
-
-
Save mamemomonga/47be991598f67df06247f6c0a12671bc to your computer and use it in GitHub Desktop.
74HC165をつかったDIPスイッチの状態を読み込み
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SRR_LD 21 | |
#include <SPI.h> | |
// ビルトインLEDは LED_BUILTIN で定義されているが | |
// SCKと同じD13なので使わないこと | |
uint8_t pv; | |
void srrStart() { | |
SPI.begin(); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setClockDivider(SPI_CLOCK_DIV2); | |
SPI.setDataMode(SPI_MODE2); | |
pinMode(SRR_LD, OUTPUT); | |
digitalWrite(SRR_LD, HIGH); | |
} | |
uint8_t srrRead() { | |
digitalWrite(SRR_LD,LOW); | |
delayMicroseconds(1); | |
digitalWrite(SRR_LD,HIGH); | |
delayMicroseconds(1); | |
uint8_t rv=SPI.transfer(0x00); | |
delayMicroseconds(1); | |
return rv; | |
} | |
uint8_t dipSwRead() { | |
uint8_t cv=srrRead(); | |
if(pv == cv) return; | |
Serial.print("Value:"); | |
Serial.println(cv); | |
for(uint8_t i=0; i<8; i++) { | |
bool rb = (cv >> i) & 1; | |
Serial.print(i); | |
Serial.print(": "); | |
Serial.println( rb ? "0" : "1"); | |
} | |
Serial.println(""); | |
pv=cv; | |
} | |
void setup() { | |
Serial.begin(115200); | |
srrStart(); | |
pv=0; | |
} | |
void loop() { | |
dipSwRead(); | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment