Pages: [1]   Go Down
Author Topic: SPI script for shift register 74HC4094 (arduino UNO)  (Read 2416 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}


* arduino.jpg (96.11 KB, 1000x375 - viewed 53 times.)

* Bildschirmfoto 2011-06-12 um 15.40.13.png (20.77 KB, 224x247 - viewed 59 times.)
« Last Edit: June 12, 2011, 09:36:58 am by DasQuadrom » Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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);
}

« Last Edit: June 12, 2011, 09:40:49 am by madworm » Logged

• 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!

Offline Offline
Full Member
***
Karma: 0
Posts: 110
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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:
pinMode(freigabeOE, LOW); //Freigabe Ausgangsregister
Didn't you rather mean
Code:
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
Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, the thing is: using hardware SPI is so darn easy (sending at least), he doesn't need the library at all.  smiley-cool
Logged

• 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!

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hallo again
I tried my best with madworm's code but it didn't work yet.
Didn't you rather mean
Code:
digitalWrite(freigabeOE, LOW); //Freigabe Ausgangsregister
Right - Thank you! But it still doesn't work. smiley-cry
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
« Last Edit: June 14, 2011, 07:37:37 am by DasQuadrom » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: