I made a sample using two of them using Arduino UNO.
So I did that like this:
Arduino with 74HC595
(There aren't any resistor on that image, but there are 220ohm resistor on every LEDs.)
And uploaded this code.
//1.6us+62.5ns
#include <SPI.h>
#define sbi(port, bit) (port) |= (1 << (bit))
#define cbi(port, bit) (port) &= ~(1 << (bit))
int latchPin = 12;
int clockPin = 13;
int dataPin = 11;
byte data[]={ 0b10101010,0b11001100 };
void _595_out() {
cbi(PORTB, 4);
SPI.transfer(data[0] );
SPI.transfer( data[1] );
sbi(PORTB, 4);
}
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.begin();
}
void loop() {
_595_out();
}
And this worked as I expected. But, when I changed sketch like this,
//1.6us+62.5ns
#include <SPI.h>
#define sbi(port, bit) (port) |= (1 << (bit))
#define cbi(port, bit) (port) &= ~(1 << (bit))
#define CHIPNO 2
int latchPin = 12;
int clockPin = 13;
int dataPin = 11;
byte data[]={ 0b10101010,0b11001100 };
void _595_out() {
cbi(PORTB, 4);
SPI.transfer(data[0]);
SPI.transfer(data[1]);
sbi(PORTB, 4);
}
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.begin();
Serial.begin(9600);
}
void loop() {
for(unsigned int z = 0; z < 65535; z++) {
data[0] = z & 255;
data[1] = (z & 65280) >> 8;
Serial.println("A");
Serial.print(data[0]);
Serial.print("/");
Serial.println(data[1]);
Serial.println("B");
_595_out();
Serial.println("C");
delay(500);
}
}
74HC595s aren't working. Any LEDs aren't on.
Printing Serial works well. Only 74HC595s aren't working.
And Removing all Serial-related thing doesn't solved this problem.
Where did I do wrong?
1 Answer 1
int latchPin = 12;
You use pin 12 as the latch pin. Once you activate SPI the hardware takes over control of the pins MOSI, MISO and SCK. Pin 12 is MISO and thus attempting to change it in your code will have no effect.
You should use some other pin as the latch pin (pin 10 is often used).
And shiftOut using SPI is working when I use first code
shiftOut
is a software-based method of clocking out bits. It doesn't use hardware and thus it doesn't take control of pin 12.
Note: Answered originally on the Arduino Forum
Serial.begin()
? Please post the entire sketch that doesn't work.Serial.begin(9600);
ath the end of the setup function. Everything except these is same as the first code, so that's why I didn't upload the entire sketch... And shiftOut using SPI is working when I use first code, but isn't working when I change the code as I said.