Go Down

Topic: Help controlling a 595 shift register (Read 1 time) previous topic - next topic

zomzilla

Hi, i am trying to control a pair of 74hc595n's. I did this successfully using python and the RPi, and having set the hardware up the same way i cannot work out how to output how i desire.
Code: [Select]
int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
int data_out[] = {0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,0,1};

void setup() {
  // put your setup code here, to run once:

pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);

}

void loop() {
 
  // put your main code here, to run repeatedly:
  digitalWrite(latchPin, LOW);
  for (int thisLed = 0; thisLed < 16; thisLed++)
 
    if (data_out[thisLed] != 0)
      digitalWrite(dataPin, HIGH);
    else
      digitalWrite(dataPin, LOW);
   
    digitalWrite(clockPin, HIGH);
    digitalWrite(clockPin, LOW);
  digitalWrite(latchPin, HIGH);
  delay(500);
}


when this is uploaded to my nano all the outputs are high! can anyone see what i have done wrong?

PaulRB

#1
Jul 28, 2013, 07:41 pm Last Edit: Jul 28, 2013, 07:47 pm by PaulRB Reason: 1
Hi, I think you would be better off using the shiftOut() function. Also, pack your data bits into bytes with "B00010011" instead of "{0,0,0,1,0,0,1,1}".

I think the reason  your code doesn't work is that there are some missing {}, so the compiler doesn't know which commands are inside the for loop. In Python, the line indentation tells the compiler what's in the loop, but C needs those brackets!

Paul

zomzilla

#2
Jul 28, 2013, 08:37 pm Last Edit: Jul 28, 2013, 08:50 pm by zomzilla Reason: 1
thanks for the reply

i knew there was a shift function, but i wanted to try and do it by hand to get used to arduino and C

i'll search out those curly brackets now :)

EDIT:
curly brackets fixed it. thank you

fungus


Hi, i am trying to control a pair of 74hc595n's. I did this successfully using python and the RPi, and having set the hardware up the same way i cannot work out how to output how i desire.
Code: [Select]
int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
int data_out[] = {0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,0,1};

void setup() {
  // put your setup code here, to run once:

pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);

}

void loop() {
 
  // put your main code here, to run repeatedly:
  digitalWrite(latchPin, LOW);
  for (int thisLed = 0; thisLed < 16; thisLed++)
 
    if (data_out[thisLed] != 0)
      digitalWrite(dataPin, HIGH);
    else
      digitalWrite(dataPin, LOW);
   
    digitalWrite(clockPin, HIGH);
    digitalWrite(clockPin, LOW);
  digitalWrite(latchPin, HIGH);
  delay(500);
}


when this is uploaded to my nano all the outputs are high! can anyone see what i have done wrong?


This isn't Python, you need to put in some { and } to show which statements are inside the loop.

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

cjdelphi

By the looks of it all you're doing is setting the data pin to high and then sending your data..


But the clock is not toggled back to low/high until your loop finishes

1ChicagoDave

Yes, use shiftOut. It's easiest for most shift register needs.  IF you want to really do it manually....  go find the shiftOut function in the "wiring_shift.c" library (somewhere in your Arduino folder) and dissect  the code it uses.

CrossRoads

Use SPI, way faster:
Code: [Select]

digitalWrite(ssPin, LOW);
SPI.transfer(your_data_byte);
digitalWrite (ssPin, HIGH);

and then replace the digitalWrite's with direct port manipulation, such as:
Code: [Select]

PORTB = PORTB & B11111011; // clear PB2 (SS pin, D10)
SPI.transfer(your_data_byte);
PORTB = PORTB | B00000100; // set PB2 (SS pin, D10)

Got fast hardware for shifting out data, learn to use it.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up