|
14 | 14 | SPIClass SPI;
|
15 | 15 |
|
16 | 16 | void SPIClass::begin() {
|
17 |
| - // Set direction register for SCK and MOSI pin. |
18 |
| - // MISO pin automatically overrides to INPUT. |
| 17 | + |
| 18 | + // Set SS to high because many users use this as chip-select |
| 19 | + // and most chips "select" when SS is high. If your chip |
| 20 | + // does not, you can set it back to LOW after begin() is called. |
| 21 | + digitalWrite(SS, HIGH); |
| 22 | + |
19 | 23 | // When the SS pin is set as OUTPUT, it can be used as
|
20 | 24 | // a general purpose output port (it doesn't influence
|
21 | 25 | // SPI operations).
|
22 |
| - |
23 |
| - pinMode(SCK, OUTPUT); |
24 |
| - pinMode(MOSI, OUTPUT); |
25 | 26 | pinMode(SS, OUTPUT);
|
26 |
| - |
27 |
| - digitalWrite(SCK, LOW); |
28 |
| - digitalWrite(MOSI, LOW); |
29 |
| - digitalWrite(SS, HIGH); |
30 | 27 |
|
31 |
| - // Warning: if the SS pin ever becomes a LOW INPUT then SPI |
32 |
| - // automatically switches to Slave, so the data direction of |
| 28 | + // Warning: if the SS pin ever becomes a LOW INPUT then SPI |
| 29 | + // automatically switches to Slave, so the data direction of |
33 | 30 | // the SS pin MUST be kept as OUTPUT.
|
34 | 31 | SPCR |= _BV(MSTR);
|
35 | 32 | SPCR |= _BV(SPE);
|
| 33 | + |
| 34 | + // Set direction register for SCK and MOSI pin. |
| 35 | + // MISO pin automatically overrides to INPUT. |
| 36 | + // By doing this AFTER enabling SPI, we avoid accidentally |
| 37 | + // clocking in a single bit since the lines go directly |
| 38 | + // from "input" to SPI control. |
| 39 | + // http://code.google.com/p/arduino/issues/detail?id=888 |
| 40 | + pinMode(SCK, OUTPUT); |
| 41 | + pinMode(MOSI, OUTPUT); |
36 | 42 | }
|
37 | 43 |
|
| 44 | + |
38 | 45 | void SPIClass::end() {
|
39 | 46 | SPCR &= ~_BV(SPE);
|
40 | 47 | }
|
|
0 commit comments