Arduino Uno and CD4094 [Solved]

I´m having trouble with Arduino Uno and a 16-bit shift register made out of two CD4094´s.
The program appears to be working, but the leds connected on the outputs of the shift registers do nothing. What am I doing wrong ?

#include <avr/pgmspace.h>  
#include <SPI.h> 

const int latchPin = 10;  //Pin connected to STR(pin 1) of HEF4094
//clock = 13, mosi = 11
const int eprom[12] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800};

int address = 0;
byte i = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(latchPin, OUTPUT);
  digitalWrite(latchPin, LOW);
  SPI.beginTransaction(SPISettings(500000, LSBFIRST, SPI_MODE0)); // initialize SPI
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  address = eprom[i];
  Serial.print(i);
  Serial.print(" ");
  Serial.println(address, HEX);
  Serial.println(" ");
  addressOut(address % 256, address / 256);
  delay(1000);
  i++;
  if(i > 11) i = 0;
}

void addressOut(byte addressLow, byte addressHigh)
{
  digitalWrite(latchPin, HIGH);
  SPI.transfer(addressLow);
  SPI.transfer(addressHigh);
  //delay(1);
  digitalWrite(latchPin, LOW);
}

Did you try the code with just one shift register, first? Do you KNOW that the LEDs are connected correctly?

Hi

I got it working by replacing

SPI.beginTransaction(SPISettings(500000, LSBFIRST, SPI_MODE0));

with

SPI.begin();
SPI.setBitOrder(LSBFIRST);

And apparently Arduino needs to be connected to USB for the program to work at all (?)
(It would be interesting to know tough why the version above didn´t work.)

Hi,
Run perfectly with two HEF4094.
Thank You.

#include <SPI.h>

const int latchPin = 10;  // Pin connected to STR(pin 1) of HEF4094
//clock = 13, mosi = 11
const int eprom[12] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800};

int address = 0;
byte i = 0;

void setup()
{
  pinMode(latchPin, OUTPUT);
  digitalWrite(latchPin, LOW);
  SPI.beginTransaction(SPISettings(500000, LSBFIRST, SPI_MODE0));             // initialize SPI  
  SPI.begin(); 
  Serial.begin(9600);
}

void addressOut(byte addressLow, byte addressHigh)
{
  digitalWrite(latchPin, HIGH);
  SPI.transfer(addressLow);
  SPI.transfer(addressHigh);
  delay(1);
  digitalWrite(latchPin, LOW);
}

void loop()
{
  address = eprom[i];
  Serial.print(i);
  Serial.print(" ");
  Serial.println(address, HEX);
  Serial.println(" ");
  addressOut(address % 256, address / 256);
  delay(500);
  i++;
  if (i > 11) i = 0;
}

Running in FAST mode, use SPIMODE1.
Best without spurious pulses.

#include <SPI.h>

//clock = D13, mosi = D11
const int latchPin = 10;  // Pin connected to STR(pin 1) of HEF4094
const int eprom[12] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800};

int address = 0;
byte i = 0;

void setup()
{
  pinMode(latchPin, OUTPUT);
  digitalWrite(latchPin, LOW);
  SPI.beginTransaction(SPISettings(500000, LSBFIRST, SPI_MODE1));       //  initialize SPI  500KHz
  SPI.begin();   
}

void addressOut(byte addressLow, byte addressHigh)
{
  SPI.transfer(addressLow);
  SPI.transfer(addressHigh);
  digitalWrite(latchPin, HIGH); 
  digitalWrite(latchPin, LOW);
}

void loop()
{
  address = eprom[i];  
  addressOut(address % 256, address / 256); 
  i++;
  if (i > 11) i = 0;
}

Just found and solved a problem using daisy chained 4094 shift registers with Arduino SPI_MODE0 that I will add here for future reference.

Use the Q'S output (pin 10), not QS (pin 9)!

Data is shifted out of QS on the rising clock edge, just before it is clocked into the next device. It works sometimes but as the number of devices on the daisy chain increases so does the chance of errors.

The output on Q'S changes on the falling clock edge, so it's always ready to be clocked in on the next rising edge.