piacenza
Online
Sr. Member
Karma: 0
Posts: 494
prima della pazzia c'è la passione
|
 |
« on: February 18, 2012, 03:25:54 pm » |
ciao ho alcuni 74hc597 sono dei piso, ho seguito questa guida http://y-aji.com/?p=242funziona con un integrato, adesso vorrei aggiungerne uno, ho già fatto esperienze con successo, seguendo il playground dove si usa un cd4021, per questo ho collegato pin9 di uno con pin 14 dell'altro ma leggo solo una variabile e la seconda è uguale qualche suggerimento? grazie stefano //**************************************************************// // Name : shiftIn Example 2.1 // // Author : Carlyn Maw // // Date : 25 Jan, 2007 // // Version : 1.0 // // Notes : Code for using a CD74hc597 Shift Register // // : // //****************************************************************
//define where your pins are int latchPin = 8; //597 pin 12 int dataPin = 9; //597 pin 9 int clockPin = 7; //597 pin 11 int loadPin = 6;//597 pin 13
int count;
//Define variables to hold the data //for shift register. //starting with a non-zero numbers can help //troubleshoot byte switchVar1 = 72; //01001000 byte switchVar2 = 159; //10011111
void setup() { //start serial Serial.begin(9600);
//define pin modes pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, INPUT); pinMode(loadPin, OUTPUT); }
void loop() {
//Pulse the latch pin: digitalWrite(latchPin,1);//set it to 1 to collect parallel data delayMicroseconds(20); //set it to 1 to collect parallel data, wait digitalWrite(latchPin,0);//set it to 0 to transmit data serially
//while the shift register is in serial mode //collect each shift register into a byte //the register attached to the chip comes in first //switchVar1 = shiftIn(dataPin, clockPin); switchVar1 = shiftIn(dataPin, clockPin, loadPin); switchVar2 = shiftIn(dataPin, clockPin, loadPin); //Print out the results. //leading 0's at the top of the byte //(7, 6, 5, etc) will be dropped before //the first pin that has a high input //reading // Serial.println(switchVar1, BIN); for (count=0;count<8;count++) { Serial.print(count); Serial.print('\t'); Serial.print("switchVar1"); Serial.print('\t'); Serial.println(bitRead(switchVar2,count)); } Serial.println("-------------------"); for (count=0;count<8;count++) { Serial.print(count); Serial.print('\t'); Serial.print("switchVar2"); Serial.print('\t'); Serial.println(bitRead(switchVar1,count)); } //white space Serial.println("-------------------"); //delay so all these print satements can keep up. delay(1000);
}//------------------end main loop
////// --------------shiftIn function ///// just needs the location of the data pin and the clock pin ///// it returns a byte with each bit in the byte corresponding ///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0
//byte shiftIn(int myDataPin, int myClockPin) { byte shiftIn(int myDataPin, int myClockPin, int myLoadPin) { int i; int temp = 0; int pinState; byte myDataIn = 0;
pinMode(myDataPin, INPUT); pinMode(myClockPin, OUTPUT); pinMode(myLoadPin, OUTPUT); //we will be holding the clock pin high 8 times (0,..,7) at the //end of each time through the for loop
//at the begining of each loop when we set the clock low, it will //be doing the necessary low to high drop to cause the shift //register's DataPin to change state based on the value //of the next bit in its serial information flow. //The register transmits the information about the pins from pin 7 to pin 0 //so that is why our function counts down
digitalWrite(myLoadPin, 0); delayMicroseconds(0.2); digitalWrite(myLoadPin, 1);
for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); delayMicroseconds(0.2); temp = digitalRead(myDataPin); if (temp) { pinState = 1; //set the bit to 0 no matter what myDataIn = myDataIn | (1 << i); } else { //turn it off -- only necessary for debuging //print statement since myDataIn starts as 0 pinState = 0; }
//Debuging print statements //Serial.print(pinState); //Serial.print(" "); //Serial.println (dataIn, BIN);
digitalWrite(myClockPin, 1);
} //debuging print statements whitespace //Serial.println(); //Serial.println(myDataIn, BIN); return myDataIn; }
|
|
|
|
« Last Edit: February 20, 2012, 03:22:50 am by stefa24 »
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Offline
Brattain Member
Karma: 219
Posts: 16447
Don't know what I do
|
 |
« Reply #1 on: February 18, 2012, 03:34:15 pm » |
Hai collegato i pin 13/12/11 dei 2 registri tra di loro?
|
|
|
|
|
Logged
|
|
|
|
|
piacenza
Online
Sr. Member
Karma: 0
Posts: 494
prima della pazzia c'è la passione
|
 |
« Reply #2 on: February 19, 2012, 04:17:05 am » |
ciao @leo si ho collegato insieme i pin indicati vorrei utilizzarli perchè ne ho un pò e sono simili alla piedinatura del 74hc595, giusto per non diversificare troppo, inoltre non vorrei darmi per vinto
stefano
|
|
|
|
« Last Edit: February 19, 2012, 04:19:10 am by stefa24 »
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Offline
Brattain Member
Karma: 219
Posts: 16447
Don't know what I do
|
 |
« Reply #3 on: February 19, 2012, 04:58:33 am » |
Metti per favore lo schema esatto, vediamo se si trova un errore di collegamento.
|
|
|
|
|
Logged
|
|
|
|
|
piacenza
Online
Sr. Member
Karma: 0
Posts: 494
prima della pazzia c'è la passione
|
 |
« Reply #4 on: February 19, 2012, 06:48:30 am » |
ciao allego lo schema e un esempio di output mettendo a gnd il pin A di un integrato 0 switchVar1 0 1 switchVar1 1 2 switchVar1 1 3 switchVar1 1 4 switchVar1 1 5 switchVar1 1 6 switchVar1 1 7 switchVar1 1 ------------------- 0 switchVar2 0 1 switchVar2 1 2 switchVar2 1 3 switchVar2 1 4 switchVar2 1 5 switchVar2 1 6 switchVar2 1 7 switchVar2 1 -------------------
grazie stefano
|
|
|
|
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Offline
Brattain Member
Karma: 219
Posts: 16447
Don't know what I do
|
 |
« Reply #5 on: February 19, 2012, 10:07:56 am » |
Mi sono accorto di una cosa osservando il listato ed il sito da cui hai preso il codice. Il tuo sketch (che è quello del sito) dichiara una funzione shiftIn per leggere i dati dal registro ma c'è un però: Arduino già dichiara una propria funzione shiftIn, che è diversa da questa. Visto che l'articolo è di 2 anni fa, non vorrei che fosse stato scritto per versioni vecchie dell'IDE che non avevano ancora quella funzione (che ho visto è stata introdotta in una versione rilasciata poco prima di quell'articolo).
Prova intanto a ridenominare la funzione magari da shiftIn a dataIn, per esempio. Per vedere se cambia qualcosa.
|
|
|
|
|
Logged
|
|
|
|
|
piacenza
Online
Sr. Member
Karma: 0
Posts: 494
prima della pazzia c'è la passione
|
 |
« Reply #6 on: February 19, 2012, 11:01:15 am » |
ciao @leo lo sketch di partenza è quello del playground, e funziona con il 4021, è stato modificato facendo le aggiunte del link per il 597, uso la versione di arduino 0022, comunque ho fatto e non ci sono cambiamenti
stefano
|
|
|
|
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Offline
Brattain Member
Karma: 219
Posts: 16447
Don't know what I do
|
 |
« Reply #7 on: February 19, 2012, 11:34:16 am » |
Ed usando quello della guida per il 597? Hai provato?
|
|
|
|
|
Logged
|
|
|
|
|
piacenza
Online
Sr. Member
Karma: 0
Posts: 494
prima della pazzia c'è la passione
|
 |
« Reply #8 on: February 19, 2012, 11:42:29 am » |
ciao i due sketch sono uguali ho fatto solo le aggiunte segnate in rosso, e con uno solo funziona
grazie stefano
|
|
|
|
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Offline
Brattain Member
Karma: 219
Posts: 16447
Don't know what I do
|
 |
« Reply #9 on: February 19, 2012, 01:55:38 pm » |
Ho finito le idee. Ricontrolla i collegamenti, magari c'è qualcosa che non va.
|
|
|
|
|
Logged
|
|
|
|
|
piacenza
Online
Sr. Member
Karma: 0
Posts: 494
prima della pazzia c'è la passione
|
 |
« Reply #10 on: February 20, 2012, 03:22:26 am » |
ciao @leo grazie per la pazienza stamattina ho risolto posto lo schema funzionante // Date : 20.02.2012 // // Notes : Code for using a 2 CD74hc597 Shift Register // // : // //****************************************************************
//define where your pins are int latchPin = 8; //597 pin 12 int dataPin = 9; //597 pin 9 int clockPin = 7; //597 pin 11 int loadPin = 6;//597 pin 13
int count;
//Define variables to hold the data //for shift register. //starting with a non-zero numbers can help //troubleshoot byte switchVar1 = 72; //01001000 byte switchVar2 = 159; //10011111
void setup() { Serial.begin(9600); //start serial
pinMode(latchPin, OUTPUT); //define pin modes pinMode(clockPin, OUTPUT); //define pin modes pinMode(dataPin, INPUT); //define pin modes pinMode(loadPin, OUTPUT); //define pin modes }
void loop() {
//Pulse the latch pin: digitalWrite(latchPin,1);//set it to 1 to collect parallel data delayMicroseconds(20); //set it to 1 to collect parallel data, wait digitalWrite(latchPin,0);//set it to 0 to transmit data serially
digitalWrite(loadPin, 0); delayMicroseconds(20); digitalWrite(loadPin, 1);
//while the shift register is in serial mode //collect each shift register into a byte //the register attached to the chip comes in first //switchVar1 = shiftIn(dataPin, clockPin); switchVar1 = shiftIn(dataPin, clockPin); switchVar2 = shiftIn(dataPin, clockPin); //Print out the results. //leading 0's at the top of the byte //(7, 6, 5, etc) will be dropped before //the first pin that has a high input //reading // Serial.println(switchVar1, BIN); for (count=0;count<8;count++) { Serial.print(count); Serial.print('\t'); Serial.print("switchVar1"); Serial.print('\t'); Serial.println(bitRead(switchVar2,count)); } Serial.println("-------------------"); for (count=0;count<8;count++) { Serial.print(count); Serial.print('\t'); Serial.print("switchVar2"); Serial.print('\t'); Serial.println(bitRead(switchVar1,count)); } //white space Serial.println("-------------------"); //delay so all these print satements can keep up. delay(2000);
}//------------------end main loop
////// --------------shiftIn function ///// just needs the location of the data pin and the clock pin ///// it returns a byte with each bit in the byte corresponding ///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0
//byte shiftIn(int myDataPin, int myClockPin) { byte shiftIn(int myDataPin, int myClockPin) { int i; int temp = 0; int pinState; byte myDataIn = 0;
pinMode(myDataPin, INPUT); pinMode(myClockPin, OUTPUT); //we will be holding the clock pin high 8 times (0,..,7) at the //end of each time through the for loop
//at the begining of each loop when we set the clock low, it will //be doing the necessary low to high drop to cause the shift //register's DataPin to change state based on the value //of the next bit in its serial information flow. //The register transmits the information about the pins from pin 7 to pin 0 //so that is why our function counts down
for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); delayMicroseconds(20); temp = digitalRead(myDataPin); if (temp) { pinState = 1; myDataIn = myDataIn | (1 << i); } else { //turn it off -- only necessary for debuging //print statement since myDataIn starts as 0 pinState = 0; } digitalWrite(myClockPin, 1); } return myDataIn; } in pratica bisogna portare fuori dalla funzione shiftin questa parte digitalWrite(loadPin, 0); delayMicroseconds(20); digitalWrite(loadPin, 1); grazie stefano
|
|
|
|
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Offline
Brattain Member
Karma: 219
Posts: 16447
Don't know what I do
|
 |
« Reply #11 on: February 20, 2012, 04:03:58 am » |
Quindi in pratica tenere il pin LOAD alto per tutto il tempo in cui tu esegui le letture sui registri. E' logico, a ripensarci ed a rileggere il datasheet.
Il datasheet dice che il pin LOAD serve per caricare i valori dei pin in un registro intermedio che è posto PRIMA del registro a scorrimento interno su cui viaggiano i dati. Quindi dare il segnale alto su quel pin nella seconda lettura faceva ricaricare nuovamente i dati dei pin al 1° chip, cancellando quindi i bit che erano arrivati dal 2° chip, quello a valle.
|
|
|
|
|
Logged
|
|
|
|
|
piacenza
Online
Sr. Member
Karma: 0
Posts: 494
prima della pazzia c'è la passione
|
 |
« Reply #12 on: February 20, 2012, 04:08:49 am » |
ciao esatto con questa parte del codice digitalWrite(latchPin,1);//set it to 1 to collect parallel data delayMicroseconds(20); //set it to 1 to collect parallel data, wait digitalWrite(latchPin,0);//set it to 0 to transmit data serially
carica lo stato dei pin con questa parte del codice digitalWrite(loadPin, 0); delayMicroseconds(20); digitalWrite(loadPin, 1);
trasferiamo le letture nel registro da qui procediamo con il solito sketch grazie stefano
|
|
|
|
|
Logged
|
|
|
|
|
|