Pages: [1]   Go Down
Author Topic: Help controlling a 595 shift register  (Read 858 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 43
Posts: 1335
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 28, 2013, 12:47:41 pm by PaulRB » Logged

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

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 smiley

EDIT:
curly brackets fixed it. thank you
« Last Edit: July 28, 2013, 01:50:23 pm by zomzilla » Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 146
Posts: 5469
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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

Offline Offline
Edison Member
*
Karma: 29
Posts: 2443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Chicago
Offline Offline
Sr. Member
****
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26969
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use SPI, way faster:
Code:
digitalWrite(ssPin, LOW);
SPI.transfer(your_data_byte);
digitalWrite (ssPin, HIGH);
and then replace the digitalWrite's with direct port manipulation, such as:
Code:
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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: