Hello everyone , i want to say first of all hello , im a new arduino programmer based in the UK , i have programmed before mainly in visual basic
im having a problem using shift registers but it also raises a question about how to do something which i can see is of fundamental importance
i am trying to control the outputs of two shift registers , the shift registers are connected ok and work ok with the arduino shift out demo code
i am trying to use an array to control the registers so its easy for me to port my applications over to the outputs on the shift register chips
now what i understand is that you send 16 bytes to the chips and the relevant outputs get switched on and off , i also realise that this is how you write to the outputs in the other microcontrollers using base level programming and not the arduino bootloader which makes it easier by being able to address each output , so how do you build your 16 bytes on the fly , say if i want to just change one of the outputs , i cant imagine that you have a lookup table of each hex code for any given combination of ons and offs it just wouldnt make sense to do it like that . im trying to use an array and a loop to build up the 16 bits and then send it out to the chips just im not having much success , i i have included the code ive written so far ,you can see im using an array and then just a couple of different changes with delays in between so i can see if my code is working .. the leds connected to the shift register are flickering although they do almost seem to be the correct ones that flicker but it might be just coincidence , what am i doing wrong??
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
//Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
// Pin Connected to Mreclear pin
const int MReclearPin = 2;
int SRout[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void setup() {
// put your setup code here, to run once:
pinMode(MReclearPin,OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(MReclearPin,HIGH);
}
// the function i wrote to build the binary string and send it out to the shift regs
void SRoutWrite (){
for (int ThisByte = 0; ThisByte < 15; ThisByte++){
registerWrite(ThisByte,SRout[ThisByte]);
}
}
void loop() {
// here are just some tests
// it should turn on the first led , then after a second the next and after a second the next
// while not turning any of them off even for a millisecond
SRout[0] = 1;
SRoutWrite();
delay (1000);
SRout[1] = 1;
SRoutWrite();
delay (1000);
SRout[2] = 1;
SRoutWrite();
delay (1000);
SRout[3] = 1;
SRoutWrite();
delay (1000);
}
// i took this void from the arduino shiftout examples
void registerWrite(int whichPin, int whichState) {
// the bits you want to send. Use an unsigned int,
// so you can use all 16 bits:
unsigned int bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);
// break the bits into two bytes, one for
// the first register and one for the second:
byte registerOne = highByte(bitsToSend);
byte registerTwo = lowByte(bitsToSend);
// shift the bytes out:
shiftOut(dataPin, clockPin, MSBFIRST, registerTwo);
shiftOut(dataPin, clockPin, MSBFIRST, registerOne);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}