hmpf.c


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