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 14
|
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 | for(int i = 7; i >= 0; i--)
|
33 | {
|
34 | pio.OutputLevelLow(CLK);
|
35 | Wait_us(10);
|
36 | if (pio.GetInputLevel(MISO))
|
37 | {
|
38 | ret |= (1<<i);
|
39 | }
|
40 | pio.OutputLevelHigh(CLK);
|
41 | Wait_us(10);
|
42 | }
|
43 | return ret;
|
44 | }
|
45 |
|
46 | void CSPI::Test()
|
47 | {
|
48 | pio.OutputDriverEnable(CS);
|
49 | pio.OutputDriverEnable(HOLD);
|
50 | pio.OutputDriverEnable(WP);
|
51 | pio.OutputDriverEnable(CLK);
|
52 | pio.OutputDriverEnable(MOSI);
|
53 | pio.OutputDriverDisable(MISO);
|
54 | pio.PullupDisable(MISO);
|
55 |
|
56 |
|
57 |
|
58 | //Hold und Write setzen... sodass der Chip beschreibbar ist.
|
59 | pio.OutputLevelLow(CS);
|
60 | pio.OutputLevelLow(CLK); //Die veränderung der
|
61 | //Flanken soll nur passieren, wenn CLK low ist.
|
62 | pio.OutputLevelHigh(WP);
|
63 | pio.OutputLevelHigh(HOLD);
|
64 | Wait(1);
|
65 | pio.OutputLevelHigh(CS);
|
66 | Wait(10);
|
67 |
|
68 | //Erstmal Statusregister lesen...
|
69 | pio.OutputLevelLow(CS);
|
70 | SPI_WRITE(RDSR);
|
71 | int sr = SPI_READ();
|
72 | pio.OutputLevelHigh(CS);
|
73 | Wait(1);
|
74 | TFT.Out(100, 100, sr);
|
75 |
|
76 | //WriteEnbaleLatch
|
77 | pio.OutputLevelLow(CS);
|
78 | SPI_WRITE(WREN);
|
79 | pio.OutputLevelHigh(CS);
|
80 | Wait(1);
|
81 |
|
82 | pio.OutputLevelLow(CS);
|
83 | SPI_WRITE(WRITE);
|
84 | SPI_WRITE(0); //Adresse
|
85 | SPI_WRITE(50); //
|
86 |
|
87 | SPI_WRITE(0x55);//Daten
|
88 | SPI_WRITE(0xAA);
|
89 | pio.OutputLevelHigh(CS);
|
90 | Wait(1);
|
91 |
|
92 | pio.OutputLevelLow(CS);
|
93 | SPI_WRITE(WRDI);
|
94 | Wait(1);
|
95 | pio.OutputLevelHigh(CS);
|
96 | Wait(100);
|
97 |
|
98 | pio.OutputLevelLow(CS);
|
99 | SPI_WRITE(READ);
|
100 | SPI_WRITE(0); //Adresse
|
101 | SPI_WRITE(50); //
|
102 | int x = SPI_READ();
|
103 | int y = SPI_READ();
|
104 | pio.OutputLevelHigh(CS);
|
105 | Wait(1);
|
106 |
|
107 | TFT.Out(30, 30, x);
|
108 | TFT.Out(30, 50, y);
|
109 |
|
110 |
|
111 | }
|