Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | // Get the common arduino functions |
2 | #if defined(ARDUINO) && ARDUINO >= 100 |
||
3 | #include "Arduino.h" |
||
4 | #else |
||
5 | #include "wiring.h" |
||
6 | #endif |
||
7 | #include "Spi.h" |
||
8 | |||
9 | //---------- constructor ---------------------------------------------------- |
||
10 | |||
11 | SPI::SPI() |
||
12 | { |
||
13 | // initialize the SPI pins |
||
14 | pinMode(SCK_PIN, OUTPUT); |
||
15 | pinMode(MOSI_PIN, OUTPUT); |
||
16 | pinMode(MISO_PIN, INPUT); |
||
17 | pinMode(SS_PIN, OUTPUT); // <------- !!! (Remember! This pin will select USB host chip Max3421) |
||
18 | |||
19 | // enable SPI Master, MSB, SPI mode 0, FOSC/4 |
||
20 | mode(0); |
||
21 | } |
||
22 | |||
23 | //------------------ mode --------------------------------------------------- |
||
24 | |||
25 | void SPI::mode(byte config) |
||
26 | { |
||
27 | byte tmp; |
||
28 | |||
29 | // enable SPI master with configuration byte specified |
||
30 | SPCR = 0; |
||
31 | SPCR = (config & 0x7F) | (1<<SPE) | (1<<MSTR); |
||
32 | tmp = SPSR; |
||
33 | tmp = SPDR; |
||
34 | } |
||
35 | |||
36 | //------------------ transfer ----------------------------------------------- |
||
37 | |||
38 | byte SPI::transfer(byte value) |
||
39 | { |
||
40 | SPDR = value; |
||
41 | while (!(SPSR & (1<<SPIF))) ; |
||
42 | return SPDR; |
||
43 | } |
||
44 | |||
45 | byte SPI::transfer(byte value, byte period) |
||
46 | { |
||
47 | SPDR = value; |
||
48 | if (period > 0) delayMicroseconds(period); |
||
49 | while (!(SPSR & (1<<SPIF))) ; |
||
50 | return SPDR; |
||
51 | } |
||
52 | |||
53 | |||
54 | //---------- preinstantiate SPI object -------------------------------------- |
||
55 | |||
56 | SPI Spi = SPI(); |