#define SCLK PIN_PC1 // connected to SRCLK orange #define LATCH PIN_PB2 // connected to RCLK white #define MOSI PIN_PE3 // connected to SER green #define MISO PIN_PC0 // connected to last Q7S blue const uint8_t shiftregistercount = 4; //actual number of 8-channel shiftregisters const uint8_t channelcount = 18; //max shiftregistercount*8 const uint8_t channelassignment[channelcount] = { //starting from 0, max shiftregistercount*8-1 18, 19, 20, 21, 22, 23, 24, 25, 26, 13, 12, 11, 10, 9, 8, 7, 6, 5 }; uint8_t outputdata[shiftregistercount]; uint8_t routputdata[shiftregistercount]; uint8_t allon[shiftregistercount]; uint32_t n_output = 0; uint32_t n_output_incorrect = 0; void setup() { pinMode(SCLK, OUTPUT); pinMode(LATCH, OUTPUT); pinMode(MOSI, OUTPUT); pinMode(MISO, INPUT); digitalWrite(SCLK, LOW); digitalWrite(LATCH, LOW); digitalWrite(MOSI, LOW); //create all-on bitmask for (uint8_t i = 0; i < channelcount; i++) { uint8_t pointer = channelassignment[i]; allon[pointer >> 3] |= (1 << (pointer & 7)); } } void loop() { for (uint8_t i = 0; i < 18; i++) { setoutput(i); shiftoutdata(); delay(200); } for (uint8_t i = 0; i < 4; i++) { resetoutput(i * 2 + 1); shiftoutdata(); delay(200); } // Serial.print("Outputpattern: "); // for (uint8_t i=0; i<18; i++) { // Serial.print(readoutput(i)); // } // Serial.println(); resetalloutputs(); shiftoutdata(); delay(200); for (uint8_t i = 0; i < 2; i++) { setalloutputs(); shiftoutdata(); delay(200); resetalloutputs(); shiftoutdata(); delay(200); } // Serial.print("Failed datatransfers: "); // Serial.print(n_output_incorrect); // Serial.print("/"); // Serial.println(n_output); delay(200); } void shiftoutdata() { //setup digitalWrite(SCLK, LOW); digitalWrite(LATCH, LOW); digitalWrite(MOSI, LOW); //data_out for (int j = shiftregistercount - 1; j >= 0; j--) { for (int i = 7; i >= 0; i--) { digitalWrite(SCLK, LOW); digitalWrite(MOSI, (outputdata[j] >> i) & 1); digitalWrite(SCLK, HIGH); } } digitalWrite(SCLK, LOW); //latch digitalWrite(LATCH, HIGH); digitalWrite(LATCH, LOW); //data_in for (int j = shiftregistercount - 1; j >= 0; j--) { for (int i = 7; i >= 0; i--) { routputdata[j] = (routputdata[j] & ~(1 << i)) | (digitalRead(MISO) << i); digitalWrite(SCLK, HIGH); digitalWrite(SCLK, LOW); } } //compare data for (int i = 0; i < shiftregistercount; i++) { n_output_incorrect += (outputdata[i] != routputdata[i]); } n_output += shiftregistercount; } void setoutput(uint8_t pointer) { //channel number starting from 0 pointer = channelassignment[pointer]; outputdata[pointer >> 3] |= (1 << (pointer & 7)); } void resetoutput(uint8_t pointer) { //channel number starting from 0 pointer = channelassignment[pointer]; outputdata[pointer >> 3] &= ~(1 << (pointer & 7)); } bool readoutput(uint8_t pointer) { //channel number starting from 0 pointer = channelassignment[pointer]; return (outputdata[pointer >> 3] >> (pointer & 7)) & 1; } bool readsetoutput(uint8_t pointer) { //channel number starting from 0 pointer = channelassignment[pointer]; return (routputdata[pointer >> 3] >> (pointer & 7)) & 1; } void setalloutputs() { for (int i = 0; i < shiftregistercount; i++) { outputdata[i] = allon[i]; } } void resetalloutputs() { for (int i = 0; i < shiftregistercount; i++) { outputdata[i] = 0; } }