Short update:
I rearranged some parameters and rewrote the shift-register library from Nick Gammon to better understand whats happening.
For some reason it is working now.
I now can toggle the individual pins using the numbers 0-7 and ‘c’ to clear the board. I tested it with the standard configuration, but then the SPI would be blocked as constant OUTPUT.
main.h
#include <Arduino.h>
#include <SPI.h>
#include "SN74HC595.h"
// SN74HC595 SHIFTREGISTER = SN74HC595(11,10,13);
SN74HC595 SHIFTREGISTER = SN74HC595(10);
void setup() {
SHIFTREGISTER.initialize();
SPI.begin();
Serial.begin(9600);
Serial.println("--- Setup ---");
}
void normalMode() {
byte x = Serial.read();
if(x=='x') {
Serial.println(SHIFTREGISTER.SLAVESELECT);
} else if(x=='c') {
Serial.println("clear");
SHIFTREGISTER.writeData(0);
SHIFTREGISTER.update();
}
x = x - '0';
if(x>=0 && x<8) {
Serial.println(x);
// onoff = !onoff;
SHIFTREGISTER.writeData(x, OR);
SHIFTREGISTER.update();
}
delay(100);
}
void spiMode() {
byte x = Serial.read();
if(x=='c') {
SHIFTREGISTER.writeData(0);
SHIFTREGISTER.updateSpi();
}
x = x-'0';
if(x>=0 && x<8) {
SHIFTREGISTER.writeData(x, OR);
SHIFTREGISTER.updateSpi();
}
}
void loop() {
spiMode();
}
The library contains a normal operation mode (for test purposes) and the communication using SPI
SN74HC595.h
#ifndef SN74HC595_H
#define SN74HC595_H
#include <Arduino.h>
#include <SPI.h>
class SN74HC595 {
public:
SN74HC595(uint8_t datapin, uint8_t latchpin, uint8_t clockpin) {
DATAPIN=datapin;
SLAVESELECT=latchpin;
CLOCKPIN=clockpin;
}
SN74HC595(uint8_t slaveselect) {
SLAVESELECT=slaveselect;
// pinMode(SLAVESELECT, OUTPUT);
// pinMode(DATAPIN, OUTPUT);
// pinMode(CLOCKPIN, OUTPUT);
}
uint8_t DATAPIN = 0;
uint8_t SLAVESELECT = 0;
uint8_t CLOCKPIN = 0;
uint8_t STATUS = 0;
void initialize() {
// pinMode(DATAPIN, OUTPUT);
// pinMode(CLOCKPIN, OUTPUT);
pinMode(SLAVESELECT, OUTPUT);
delay(25);
// update();
}
void update() {
digitalWrite(SLAVESELECT,LOW);
shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, STATUS);
digitalWrite(SLAVESELECT,HIGH);
}
void updateSpi() {
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(STATUS);
digitalWrite(SLAVESELECT,HIGH);
}
void writeData(uint8_t data, uint8_t operation) {
uint8_t temp = 0;
bitSet(temp,data);
if(operation==AND) {
STATUS = STATUS & temp;
} else if(operation==OR) {
STATUS = STATUS | temp;
}
Serial.println(STATUS,BIN);
}
void writeData(uint8_t data) {
STATUS = data;
Serial.println(STATUS,BIN);
}
};
#endif
I did not change the hardware setup! The only difference in the codes is that I now initialize the SS-Pin before I begin the SPI communication and at last the Serial communication for debugging reasons (will be removed in the final revision).
Does the initialization starting position influence the communication? Maybe someone can explain this.
Or is it something I am completely missing?