78 | | |
79 | | == MAX6675.pde == |
80 | | {{{ |
81 | | #!c |
82 | | #include "SPI.h" |
83 | | |
84 | | #define VCC 8 |
85 | | #define GND 9 |
86 | | #define SLAVE 10 |
87 | | |
88 | | void |
89 | | setup() |
90 | | { |
91 | | #ifdef GND |
92 | | pinMode(GND, OUTPUT); |
93 | | digitalWrite(GND, LOW); |
94 | | #endif |
95 | | #ifdef VCC |
96 | | pinMode(VCC, OUTPUT); |
97 | | digitalWrite(VCC, HIGH); |
98 | | #endif |
99 | | |
100 | | Serial.begin(9600); |
101 | | SPI_Master.begin(SLAVE); |
102 | | } |
103 | | |
104 | | void |
105 | | loop() |
106 | | { |
107 | | int value; |
108 | | |
109 | | delay(500); |
110 | | SPI_Master.enable(SLAVE); |
111 | | value = SPI_Master.read() << 8; |
112 | | value |= SPI_Master.read(); |
113 | | SPI_Master.disable(); |
114 | | |
115 | | if ((value & 0x0004) != 0) |
116 | | Serial.println("Error"); |
117 | | else |
118 | | Serial.println((value >> 3) * 0.25); |
119 | | } |
120 | | }}} |
121 | | |
122 | | == SPI.cpp == |
123 | | {{{ |
124 | | #!c |
125 | | #include "SPI.h" |
126 | | |
127 | | boolean SPI_Master_Class::initialized_ = false; |
128 | | int SPI_Master_Class::enabled_ = -1; |
129 | | |
130 | | void |
131 | | SPI_Master_Class::begin(int slaveselecter) { |
132 | | if (!initialized_) { |
133 | | initialized_ = true; |
134 | | enabled_ = -1; |
135 | | pinMode(SS, OUTPUT); // Must be set as OUTPUT before SPE is asserted. |
136 | | pinMode(MOSI, OUTPUT); |
137 | | pinMode(MISO, INPUT); |
138 | | digitalWrite(MISO, HIGH); // Pull-up |
139 | | pinMode(SCK, OUTPUT); |
140 | | SPCR = (1<<SPE)|(1<<MSTR); // SPE: SPI Enable; MSTR: Master |
141 | | byte garbage; |
142 | | garbage = SPSR; |
143 | | garbage = SPDR; |
144 | | } |
145 | | |
146 | | if (slaveselecter != SS) |
147 | | pinMode(slaveselecter, OUTPUT); |
148 | | digitalWrite(slaveselecter, HIGH); // Disable |
149 | | } |
150 | | |
151 | | void |
152 | | SPI_Master_Class::enable(int slaveselecter) { |
153 | | disable(); |
154 | | digitalWrite(slaveselecter, LOW); |
155 | | enabled_ = slaveselecter; |
156 | | } |
157 | | |
158 | | void |
159 | | SPI_Master_Class::disable() { |
160 | | if (enabled_ >= 0) { |
161 | | digitalWrite(enabled_, HIGH); |
162 | | enabled_ = -1; |
163 | | } |
164 | | } |
165 | | |
166 | | byte |
167 | | SPI_Master_Class::write_and_read(byte data) const { |
168 | | SPDR = data; |
169 | | while (!(SPSR & (1<<SPIF))) |
170 | | ; |
171 | | return SPDR; |
172 | | } |
173 | | |
174 | | void |
175 | | SPI_Master_Class::write(byte data) const { |
176 | | write_and_read(data); |
177 | | } |
178 | | |
179 | | byte |
180 | | SPI_Master_Class::read() const { |
181 | | return write_and_read(0x00); |
182 | | } |
183 | | |
184 | | SPI_Master_Class SPI_Master; |
185 | | }}} |
186 | | |
187 | | == SPI.h == |
188 | | {{{ |
189 | | #!c |
190 | | #ifndef __SPI_H__ |
191 | | #define __SPI_H__ |
192 | | |
193 | | #include "WProgram.h" |
194 | | |
195 | | class SPI_Master_Class { |
196 | | public: |
197 | | static void begin(int slaveselecter); |
198 | | void enable(int slaveselecter); |
199 | | void disable(); |
200 | | byte write_and_read(byte data) const; |
201 | | void write(byte data) const; |
202 | | byte read() const; |
203 | | |
204 | | private: |
205 | | static boolean initialized_; |
206 | | static const int SS = 10; |
207 | | static const int MOSI = 11; |
208 | | static const int MISO = 12; |
209 | | static const int SCK = 13; |
210 | | static int enabled_; |
211 | | }; |
212 | | |
213 | | extern SPI_Master_Class SPI_Master; |
214 | | |
215 | | #endif //__SPI_H__ |
216 | | }}} |