Forum: Mikrocontroller und Digitale Elektronik Arduino Schieberegister mehrere Taster


von progger1 (Gast)


Lesenswert?

Hallo zusammen,

ich bin momentan dabei zwei Schieberegister an meinen Arduino 
anzuschließen und dann jeweils die Taster abzufragen.

Meine Frage ist:
Ich habe 16 Taster angeschlossen (an zwei Schieberegister MC14021). Der 
Test hat ganz gut funktioniert. Er erkennt immer wenn ich eine Taste 
drücke und gibt den Binärwert auch aus. Jetzt möchte ich aber, dass er 
statt des Binärwertes einen Buchstaben für jede Taste ausgibt.

Allerdings kommt immer eine Fehlermeldung und ich verstehe diese nicht:

exit status 1
too few arguments to function 'uint8_t shiftIn(uint8_t, uint8_t, 
uint8_t)'

1
//**************************************************************//
2
//  Name    : shiftIn Example 2.1                               //
3
//  Author  : Carlyn Maw                                        //
4
//  Date    : 25 Jan, 2007                                      //
5
//  Version : 1.0                                               //
6
//  Notes   : Code for using a CD4021B Shift Register       //
7
//          :                                                   //
8
//****************************************************************
9
10
//define where your pins are
11
int latchPin = 8;
12
int dataPin = 9;
13
int clockPin = 7;
14
15
//Define variables to hold the data
16
//for each shift register.
17
//starting with non-zero numbers can help
18
//troubleshoot
19
byte switchVar1 = 72;  //01001000
20
byte switchVar2 = 159; //10011111
21
22
char Taste[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
23
24
void setup() {
25
  //start serial
26
  Serial.begin(9600);
27
28
  //define pin modes
29
  pinMode(latchPin, OUTPUT);
30
  pinMode(clockPin, OUTPUT);
31
  pinMode(dataPin, INPUT);
32
33
}
34
35
void loop() {
36
37
  //Pulse the latch pin:
38
  //set it to 1 to collect parallel data
39
  digitalWrite(latchPin,1);
40
  //set it to 1 to collect parallel data, wait
41
  delayMicroseconds(20);
42
  //set it to 0 to transmit data serially  
43
  digitalWrite(latchPin,0);
44
45
  //while the shift register is in serial mode
46
  //collect each shift register into a byte
47
  //the register attached to the chip comes in first
48
  switchVar1 = shiftIn(dataPin, clockPin);
49
  switchVar2 = shiftIn(dataPin, clockPin);
50
51
  //Print out the results.
52
  //leading 0's at the top of the byte
53
  //(7, 6, 5, etc) will be dropped before
54
  //the first pin that has a high input
55
  //reading  
56
57
  for (int n=0; n<=7; n++)
58
  {
59
    //so, when n is 3, it compares the bits
60
    //in switchVar1 and the binary number 00001000
61
    //which will only return true if there is a
62
    //1 in that bit (ie that pin) from the shift
63
    //register.
64
    if (switchVar1 & (1 << n) ){
65
      //print the value of the array location
66
      Serial.println(Taste[n]);
67
    }
68
  }
69
70
71
72
73
74
//white space
75
Serial.println("-------------------");
76
//delay so all these print satements can keep up.
77
delay(500);
78
  }
79
}
80
81
//------------------------------------------------end main loop
82
83
////// ----------------------------------------shiftIn function
84
///// just needs the location of the data pin and the clock pin
85
///// it returns a byte with each bit in the byte corresponding
86
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0
87
88
byte shiftIn(int myDataPin, int myClockPin) {
89
  int i;
90
  int temp = 0;
91
  int pinState;
92
  byte myDataIn = 0;
93
94
  pinMode(myClockPin, OUTPUT);
95
  pinMode(myDataPin, INPUT);
96
97
//we will be holding the clock pin high 8 times (0,..,7) at the
98
//end of each time through the for loop
99
100
//at the begining of each loop when we set the clock low, it will
101
//be doing the necessary low to high drop to cause the shift
102
//register's DataPin to change state based on the value
103
//of the next bit in its serial information flow.
104
//The register transmits the information about the pins from pin 7 to pin 0
105
//so that is why our function counts down
106
  for (i=7; i>=0; i--)
107
  {
108
    digitalWrite(myClockPin, 0);
109
    delayMicroseconds(2);
110
    temp = digitalRead(myDataPin);
111
    if (temp) {
112
      pinState = 1;
113
      //set the bit to 0 no matter what
114
      myDataIn = myDataIn | (1 << i);
115
    }
116
    else {
117
      //turn it off -- only necessary for debuging
118
     //print statement since myDataIn starts as 0
119
      pinState = 0;
120
    }
121
122
    //Debuging print statements
123
    //Serial.print(pinState);
124
    //Serial.print("     ");
125
    //Serial.println (dataIn, BIN);
126
127
    digitalWrite(myClockPin, 1);
128
129
  }
130
  //debuging print statements whitespace
131
  //Serial.println();
132
  //Serial.println(myDataIn, BIN);
133
  return myDataIn;
134
}

von progger1 (Gast)


Lesenswert?

Hat jemand eine Idee, was da falsch ist??
Vielen Dank
LG

von chris (Gast)


Lesenswert?

Die Funktion shiftIn gibts schon, deshalb solltest du deine anders 
nennen.
https://www.arduino.cc/en/Reference/ShiftIn

von progger1 (Gast)


Lesenswert?

chris schrieb:
> Die Funktion shiftIn gibts schon, deshalb solltest du deine anders
> nennen.
> https://www.arduino.cc/en/Reference/ShiftIn

Ja diese will ich doch auch verwenden... Oder bin ich gerade blöd

von chris (Gast)


Lesenswert?

Warum schreibst du dann selber eine, die andere Argumente möchte? (Bzw. 
warum kopierst du dann von irgendwo eine andere rein?)
1
////// ----------------------------------------shiftIn function
2
///// just needs the location of the data pin and the clock pin
3
///// it returns a byte with each bit in the byte corresponding
4
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0
5
6
byte shiftIn(int myDataPin, int myClockPin) {
7
  int i;
8
  int temp = 0;
9
  int pinState;
10
  byte myDataIn = 0;
11
12
  pinMode(myClockPin, OUTPUT);
13
  pinMode(myDataPin, INPUT);
14
15
//we will be holding the clock pin high 8 times (0,..,7) at the
16
//end of each time through the for loop
17
18
//at the begining of each loop when we set the clock low, it will
19
//be doing the necessary low to high drop to cause the shift
20
//register's DataPin to change state based on the value
21
//of the next bit in its serial information flow.
22
//The register transmits the information about the pins from pin 7 to pin 0
23
//so that is why our function counts down
24
  for (i=7; i>=0; i--)
25
  {
26
    digitalWrite(myClockPin, 0);
27
    delayMicroseconds(2);
28
    temp = digitalRead(myDataPin);
29
    if (temp) {
30
      pinState = 1;
31
      //set the bit to 0 no matter what
32
      myDataIn = myDataIn | (1 << i);
33
    }
34
    else {
35
      //turn it off -- only necessary for debuging
36
     //print statement since myDataIn starts as 0
37
      pinState = 0;
38
    }
39
40
    //Debuging print statements
41
    //Serial.print(pinState);
42
    //Serial.print("     ");
43
    //Serial.println (dataIn, BIN);
44
45
    digitalWrite(myClockPin, 1);
46
47
  }
48
  //debuging print statements whitespace
49
  //Serial.println();
50
  //Serial.println(myDataIn, BIN);
51
  return myDataIn;
52
}

von progger1 (Gast)


Lesenswert?

Oje. Sorry hatte gerade einen totalen Denkfehler!

Läuft jetzt alles!
Vielen Dank!
LG

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.