1 | #define WREN 0x06
|
2 | #define WRDI 0x04
|
3 | #define RDSR 0x05
|
4 | #define WRSR 0x01
|
5 | #define READ 0x03
|
6 | #define WRITE 0x02
|
7 |
|
8 | #define CS 16
|
9 | #define CLK 15
|
10 | #define MISO 25
|
11 | #define MOSI 15
|
12 | #define WP 32
|
13 | #define HOLD 8
|
14 |
|
15 |
|
16 | void SPI_WRITE(char x)
|
17 | {
|
18 | for(int i = 7; i >= 0; i--)
|
19 | {
|
20 | pio.OutputLevelLow(CLK);
|
21 | if (x & (1<<i)) pio.OutputLevelHigh(MOSI);
|
22 | else pio.OutputLevelLow(MOSI);
|
23 | Wait_us(10);
|
24 | pio.OutputLevelHigh(CLK);
|
25 | Wait_us(10);
|
26 | }
|
27 | }
|
28 |
|
29 | char SPI_READ()
|
30 | {
|
31 | char ret = 0;
|
32 | pio.OutputLevelLow(CLK);
|
33 | for(int i = 7; i >= 0; i--)
|
34 | {
|
35 | pio.OutputLevelHigh(CLK);
|
36 | Wait_us(10);
|
37 | if (pio.GetInputLevel(MISO))
|
38 | {
|
39 | ret |= (1<<i);
|
40 | }
|
41 | pio.OutputLevelLow(CLK);
|
42 | Wait_us(10);
|
43 | }
|
44 | return ret;
|
45 | }
|
46 |
|
47 | void CSPI::Test()
|
48 | {
|
49 | pio.OutputDriverEnable(CS);
|
50 | pio.OutputDriverEnable(HOLD);
|
51 | pio.OutputDriverEnable(WP);
|
52 | pio.OutputDriverEnable(CLK);
|
53 | pio.OutputDriverEnable(MOSI);
|
54 | pio.OutputDriverDisable(MISO);
|
55 |
|
56 |
|
57 | //Hold und Write setzen... sodass der Chip beschreibbar ist.
|
58 | pio.OutputLevelLow(CS);
|
59 | pio.OutputLevelLow(CLK); //Die veränderung der
|
60 | //Flanken soll nur passieren, wenn CLK low ist.
|
61 | pio.OutputLevelHigh(WP);
|
62 | pio.OutputLevelHigh(HOLD);
|
63 | Wait(1);
|
64 | pio.OutputLevelHigh(CS);
|
65 | Wait(10);
|
66 |
|
67 | //Erstmal Statusregister lesen...
|
68 | pio.OutputLevelLow(CS);
|
69 | SPI_WRITE(RDSR);
|
70 | int sr = SPI_READ();
|
71 | pio.OutputLevelHigh(CS);
|
72 | Wait(1);
|
73 | TFT.Out(100, 100, sr);
|
74 |
|
75 | //WriteEnbaleLatch
|
76 | pio.OutputLevelLow(CS);
|
77 | SPI_WRITE(WREN);
|
78 | pio.OutputLevelHigh(CS);
|
79 | Wait(1);
|
80 |
|
81 | pio.OutputLevelLow(CS);
|
82 | SPI_WRITE(WRITE);
|
83 | SPI_WRITE(0); //Adresse
|
84 | SPI_WRITE(50); //
|
85 |
|
86 | SPI_WRITE(0x55);//Daten
|
87 | SPI_WRITE(0xAA);
|
88 | pio.OutputLevelHigh(CS);
|
89 | Wait(1);
|
90 |
|
91 | pio.OutputLevelLow(CS);
|
92 | SPI_WRITE(WRDI);
|
93 | Wait(1);
|
94 | pio.OutputLevelHigh(CS);
|
95 | Wait(100);
|
96 |
|
97 | pio.OutputLevelLow(CS);
|
98 | SPI_WRITE(READ);
|
99 | SPI_WRITE(0); //Adresse
|
100 | SPI_WRITE(50); //
|
101 | int x = SPI_READ();
|
102 | int y = SPI_READ();
|
103 | pio.OutputLevelHigh(CS);
|
104 | Wait(1);
|
105 |
|
106 | TFT.Out(30, 30, x);
|
107 | TFT.Out(30, 50, y);
|
108 |
|
109 |
|
110 | }
|