Go Down

Topic: SPI script for shift register 74HC4094 (arduino UNO) (Read 2647 times) previous topic - next topic

Jun 12, 2011, 04:10 pm Last Edit: Jun 12, 2011, 04:36 pm by DasQuadrom Reason: 1
Hello Ladys and Gentlemen,

I'm a new guest here and I search for the arduino forum in german. there existed one before: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=deutsch
But I trie to ask my question in english. I want to drive my LEDs with the shift register "74HC4094". You find the data-sheet in attachment. Therefore I wrote the very simple code, but yet it doesn't work! The top of the shift register is on the left side in the photo. No LED lights up.
On the right photo you can see that two control lamps are shining. Is all right with my arduino? Wath's the problem?

PS: Can somebody show me how to use the hardware SPI of my arduino UNO? There's a library for SPI but I don't understand how it works.

Thank you for your help!



int seriellerDateneingang = 13;  //SER, here called "DATA"
int schiebetakt = 12;            //SCK, here called "CP"
int speichertakt = 11;           //RCK, here called "strobe"
int freigabeOE = 10;             //G, here called "OE"

void setup(){

 pinMode(seriellerDateneingang, OUTPUT);
 pinMode(schiebetakt, OUTPUT);
 pinMode(speichertakt, OUTPUT);
 pinMode(freigabeOE, OUTPUT);

 //Serial.begin(9600);
}

void loop () {

 //Alles auf LOW

 digitalWrite(seriellerDateneingang, LOW);
 digitalWrite(speichertakt, LOW);
 digitalWrite(freigabeOE, LOW);

 //Pin 8 - 1 im Schieberegister werden gefüllt

 digitalWrite(seriellerDateneingang, LOW); //Pin 8 LOW
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);
 
 digitalWrite(seriellerDateneingang, HIGH); //Pin 7 HIGH
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);

 digitalWrite(seriellerDateneingang, HIGH); //Pin 6 HIGH
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);

 digitalWrite(seriellerDateneingang, LOW); //Pin 5 LOW
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);

 digitalWrite(seriellerDateneingang, LOW); //Pin 4 LOW
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);

 digitalWrite(seriellerDateneingang, HIGH); //Pin 3 HIGH
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);

 digitalWrite(seriellerDateneingang, HIGH); //Pin 2 HIGH
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);

 digitalWrite(seriellerDateneingang, LOW); //Pin 1 LOW
 digitalWrite(schiebetakt, HIGH);
 digitalWrite(schiebetakt, LOW);

 ///////////////////////////////////////////////////////
 
 digitalWrite(speichertakt, HIGH); //Übernahme ins Ausgangsregister
 digitalWrite(speichertakt, LOW);

 //////////////////////////////////////////////////////

 pinMode(freigabeOE, LOW); //Freigabe Ausgangsregister

}

madworm

#1
Jun 12, 2011, 04:34 pm Last Edit: Jun 12, 2011, 04:40 pm by madworm Reason: 1
Code: [Select]

#define __spi_clock 13 // SCK - hardware SPI
#define __spi_data_in 12 // MISO - hardware SPI (unused)
#define __spi_data 11 // MOSI - hardware SPI
#define __spi_latch 10

#include <stdint.h>

void setup(void) {
 /* [...] */
 pinMode(...);
 /* [...] */
 setup_hardware_spi();
}


void setup_hardware_spi(void)
{
uint8_t clr;
// spi prescaler:
//
// SPCR: SPR1 SPR0
// SPSR: SPI2X
//
// SPI2X SPR1 SPR0
//   0     0     0    fosc/4
//   0     0     1    fosc/16
//   0     1     0    fosc/64
//   0     1     1    fosc/128
//   1     0     0    fosc/2
//   1     0     1    fosc/8
//   1     1     0    fosc/32
//   1     1     1    fosc/64

/* enable SPI as master */
SPCR |= (_BV(SPE) | _BV(MSTR));
/* clear registers */
clr = SPSR;
clr = SPDR;
/* set prescaler to fosc/2 */
SPCR &= ~(_BV(SPR1) | _BV(SPR0));
SPSR |= _BV(SPI2X);
}

uint8_t spi_transfer(uint8_t data)
{
SPDR = data; // Start the transmission
while (!(SPSR & _BV(SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte. (we don't need that here)
}

void loop(void) {
 /* latch low */
 spi_transfer(0xFF);
 /* latch high */

 delay(2000);

 /* latch low */
 spi_transfer(0x00);
 /* latch high */

delay(2000);
}


• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

TomS

madworm, I am not sure if this is going to help him if he doesn't understand the SPI library in the first place ;)

DasQuadrom:
There is a german subforum right here, just scroll down a bit further.
When posting code use the code tag (# button in editing window).

I don't think your last line of code is really what you meant it to be:
Code: [Select]
pinMode(freigabeOE, LOW); //Freigabe Ausgangsregister
Didn't you rather mean
Code: [Select]
digitalWrite(freigabeOE, LOW); //Freigabe Ausgangsregister


Check the playground for SPI examples and come back if you have any more questions after that.

Hope that helps.
Tom

madworm

Well, the thing is: using hardware SPI is so darn easy (sending at least), he doesn't need the library at all.  8)
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

#4
Jun 14, 2011, 02:34 pm Last Edit: Jun 14, 2011, 02:37 pm by DasQuadrom Reason: 1
Hallo again
I tried my best with madworm's code but it didn't work yet.

Didn't you rather mean
Code: [Select]
digitalWrite(freigabeOE, LOW); //Freigabe Ausgangsregister
Right - Thank you! But it still doesn't work. =(
Do you have an idea wath is wrong?

Check the playground for SPI examples and come back if you have any more questions after that.
Yes I'll do that! Do you know a good tutorial for the basics of SPI with arduino?
Thank you for your help!

nice wishes
Quadrom

Now I have tested my simple first code with an other IC: "M74HC595B1R". Why doesn't it work?

Grumpy_Mike

How have you wired it up?

That code does not use the SPI interface it just tries to bit bang the data. It is very poorly written code just having nearly the same thig over an over again.

When you post code you must select it and use the # icon otherwise the forum software can scramble bits of it.

How have you got these shift resisters wired up, pleas post a schematic along with your code.

Go Up