Zwei Slave programmieren -> Arduino

OP #5004527
Lesenswert?

Hallo,

nach langem arbeiten habe ich endlich meine Maus mithilfe SPI 
angesteuert.
Nun will ich eine zweite Maus dazu hängen, och irgendwie will das nicht 
funktionieren. Die Beschaltung bleibt gleich, dass ich jedoch noch eine 
zweite Leitung für mein Chip-Select habe( Maus2 ) siehe Anhang..

Wie kann ich das am besten im Progamm umsetzen? Habe schonmal versucht, 
einen zweiten Reset reinzumachen, wo ich den zweiten NCS-Leitung auf LOW 
lege bzw anspreche. Doch so gut funktioniert das nich. Mein aktueller 
Code:
1
 
2
#include <LiquidCrystal.h>
3
LiquidCrystal lcd(28, 30 , 5, 4, 3, 2);
4

5
#define SCLK 52 // Yellow
6
#define SDIO 51 // Red
7
#define NCS 53 // Yellow + tied
8
#define NCS2 49
9
#define NRESET 48 // Black
10

11
#define REG_PRODUCT_ID 0x00
12
#define REG_REVISION_ID 0x01
13
#define REG_MOTION 0x02
14
#define REG_DELTA_X 0x03
15
#define REG_DELTA_Y 0x04
16
#define REG_SQUAL 0x05
17
#define REG_BURST_MODE 0x63
18
#define REG_PIXEL_GRAB 0x0b
19

20
#define FRAME_LENGTH 225
21

22
byte frame[256];
23

24
void setup() {
25
  Serial.begin(9600);
26
  lcd.begin(16,2);
27
}
28

29
void loop() {
30

31
    reset();
32
    byte productId = readRegister(REG_PRODUCT_ID);
33
    byte revisionId = readRegister(REG_REVISION_ID);
34
    Serial.println("Found productId ");
35
    Serial.print(productId, HEX);
36
    Serial.print(", rev. ");
37
    Serial.println(revisionId, HEX); 
38
    dumpDelta();
39
    //dumpFrame();  
40
    delay(900); 
41

42
    reset2();
43
    byte productId1 = readRegister(REG_PRODUCT_ID);
44
    byte revisionId1 = readRegister(REG_REVISION_ID);
45
    Serial.println("Found productId ");
46
    Serial.print(productId1, HEX);
47
    Serial.print(", rev. ");
48
    Serial.println(revisionId1, HEX); 
49
    dumpDelta1();
50
    //dumpFrame();  
51
    lcd.clear();
52
    delay(900);
53
}
54

55

56
void dumpDelta() {
57

58
  char squal = readRegister(0x05);
59
  char motion = readRegister(REG_MOTION); // Freezes DX and DY until they are read or MOTION is read again.
60
  char dx = readRegister(REG_DELTA_X);
61
  char dy = readRegister(REG_DELTA_Y);
62

63
  String string1 = String(motion,BIN);
64

65
  Serial.println("Squal: " + String(squal,DEC));
66
  Serial.println("Motion:    " + String(motion, BIN));
67
  Serial.println("dx:     :" + String(dx, DEC));
68
  lcd.setCursor(0,0);
69
  lcd.print("dx:");
70
  lcd.setCursor(4,0);
71
  lcd.print(dx,DEC);
72
  lcd.setCursor(0,1);
73
  lcd.print("dy:");
74
  lcd.setCursor(4 ,1);
75
  lcd.print(dy,DEC);
76
  Serial.println("dy:     :" + String(dy, DEC)); 
77
  Serial.println("     ");
78
  
79
}
80

81
void dumpDelta1() {
82

83
  char squal = readRegister(0x05);
84
  char motion = readRegister(REG_MOTION); // Freezes DX and DY until they are read or MOTION is read again.
85
  char dx = readRegister(REG_DELTA_X);
86
  char dy = readRegister(REG_DELTA_Y);
87

88
  String string1 = String(motion,BIN);
89

90
  Serial.println("Squal: " + String(squal,DEC));
91
  Serial.println("Motion:    " + String(motion, BIN));
92
  Serial.println("dx:     :" + String(dx, DEC));
93
  lcd.setCursor(9,0);
94
  lcd.print("dx:");
95
  lcd.setCursor(11,0);
96
  lcd.print(dx,DEC);
97
  lcd.setCursor(9,1);
98
  lcd.print("dy:");
99
  lcd.setCursor(11 ,1);
100
  lcd.print(dy,DEC);
101
  Serial.println("dy:     :" + String(dy, DEC)); 
102
  Serial.println("     ");
103
  
104
}
105

106
void reset() {
107
  pinMode(SCLK, OUTPUT);
108
  pinMode(SDIO, INPUT);
109
  pinMode(NCS, OUTPUT);
110
  pinMode(NRESET, OUTPUT);
111
    
112
  digitalWrite(SCLK, LOW);
113
  digitalWrite(NCS, LOW);
114
  digitalWrite(NRESET, HIGH);
115
  delayMicroseconds(100); 
116
  
117
  // Initiate chip reset
118
  digitalWrite(NRESET, LOW);
119
  pushByte(0xfa);
120
  pushByte(0x5a);
121
  digitalWrite(NRESET, HIGH);
122
  
123
  // Set 1000cpi resolution
124
  digitalWrite(NRESET, LOW);
125
  pushByte(0x0d);
126
  //pushByte(0x01); // 1000cpi
127
  pushByte(0x00); // 500cpi
128
  digitalWrite(NRESET, HIGH);
129
}
130

131
void reset2() {
132
  pinMode(SCLK, OUTPUT);
133
  pinMode(SDIO, INPUT);
134
  pinMode(NCS2, OUTPUT);
135
  pinMode(NRESET, OUTPUT);
136
    
137
  digitalWrite(SCLK, LOW);
138
  digitalWrite(NCS2, LOW);
139
  digitalWrite(NRESET, HIGH);
140
  delayMicroseconds(100);
141
  
142
  // Initiate chip reset
143
  digitalWrite(NRESET, LOW);
144
  pushByte(0xfa);
145
  pushByte(0x5a);
146
  digitalWrite(NRESET, HIGH);
147
  
148
  // Set 1000cpi resolution
149
  digitalWrite(NRESET, LOW);
150
  pushByte(0x0d);
151
  //pushByte(0x01); // 1000cpi
152
  pushByte(0x00); // 500cpi
153
  digitalWrite(NRESET, HIGH);
154
}
155

156

157

158
byte pullByte() {
159
  pinMode (SDIO, INPUT);
160

161
  delayMicroseconds(100); // tHOLD = 100us min.
162
  
163
  byte res = 0;
164
  for (byte i=128; i >0 ; i >>= 1) {
165
    digitalWrite (SCLK, LOW);
166
    res |= i * digitalRead (SDIO);
167
    delayMicroseconds(100);
168
    digitalWrite (SCLK, HIGH);
169
  }
170

171
  return res;
172
}
173

174
void pushByte(byte data){
175
  pinMode (SDIO, OUTPUT);
176
  
177
  delayMicroseconds(100); // tHOLD = 100us min.
178
  
179
  for (byte i=128; i >0 ; i >>= 1) {
180
    digitalWrite (SCLK, LOW);
181
    digitalWrite (SDIO, (data & i) != 0 ? HIGH : LOW);
182
    delayMicroseconds(100);
183
    digitalWrite (SCLK, HIGH);
184
    
185
    //Serial.print((data & i) != 0 ? HIGH : LOW, BIN);
186
  }
187
  //Serial.println("");
188
}
189

190
byte readRegister(byte address) {
191
  address &= 0x7F; // MSB indicates read mode: 0
192
  
193
  pushByte(address);
194
  
195
  byte data = pullByte();
196
  
197
  return data;  
198
}
199

200
void writeRegister(byte address, byte data) {
201
  address |= 0x80; // MSB indicates write mode: 1
202
  
203
  pushByte(address);
204
  
205
  delayMicroseconds(100);
206
  
207
  pushByte(data);
208

209
  delayMicroseconds(100); // tSWW, tSWR = 100us min.
210
}
Angehängte Dateien:
#5005472
Lesenswert?

Ich vermute mal, die beiden Ausgänge "treiben aufeinander", da ich keine 
Stelle gesehen habe im Programm, wo NCS/2 wieder HIGH geschaltet wird.

Lt. Datenblatt: " To  improve  communication  reliability,  all  serial
transactions  should  be  framed  by  NCS. "

Solange wie nur ein Chip sendet, macht das nix..

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren