Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 325 lines (273 sloc) 7.801 kb
2b7f6371 » ladyada
2012-04-24 comments, readme, etc
1 /***************************************************
2 This is a library for the Adafruit 2.2" SPI display.
3 This library works with the Adafruit 2.2" TFT Breakout w/SD card
4 ----> http://www.adafruit.com/products/797
5
6 Check out the links above for our tutorials and wiring diagrams
7 These displays use SPI to communicate, 3 or 4 pins are required to
8 interface (RST is optional)
9 Adafruit invests time and resources providing this open source code,
10 please support Adafruit and open-source hardware by purchasing
11 products from Adafruit!
12
13 Written by Limor Fried/Ladyada for Adafruit Industries.
14 MIT license, all text above must be included in any redistribution
15 ****************************************************/
16
1408b768 » Kevin
2012-03-20 First commit
17 #include <avr/pgmspace.h>
18 #include <util/delay.h>
19 #include <stdlib.h>
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
20 #include <SPI.h>
1408b768 » Kevin
2012-03-20 First commit
21
22 #include "Adafruit_GFX.h"
23 #include "Adafruit_HX8340B.h"
24
3a1ed094 » ladyada
2012-04-17 sped up BMP drawing
25 // use bitbang SPI (not suggested)
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
26 Adafruit_HX8340B::Adafruit_HX8340B(int8_t SID, int8_t SCLK, int8_t RST, int8_t CS) {
27 sid = SID;
28 sclk = SCLK;
29 rst = RST;
30 cs = CS;
31 hwSPI = false;
32 }
33
3a1ed094 » ladyada
2012-04-17 sped up BMP drawing
34 // use hardware SPI
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
35 Adafruit_HX8340B::Adafruit_HX8340B(int8_t RST, int8_t CS) {
36 sid = -1;
37 sclk = -1;
38 rst = RST;
39 cs = CS;
40 hwSPI = true;
41 }
42
1408b768 » Kevin
2012-03-20 First commit
43
44 void Adafruit_HX8340B::begin() {
7890b82e » Kevin
2012-03-21 Debugging away ...
45 // Constructor for underlying GFX library
3a1ed094 » ladyada
2012-04-17 sped up BMP drawing
46 constructor(HX8340B_LCDWIDTH, HX8340B_LCDHEIGHT);
1408b768 » Kevin
2012-03-20 First commit
47
48 // set pin directions
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
49 if (! hwSPI) {
50 pinMode(sid, OUTPUT);
51 pinMode(sclk, OUTPUT);
52 }
1408b768 » Kevin
2012-03-20 First commit
53 pinMode(rst, OUTPUT);
54 pinMode(cs, OUTPUT);
55
7890b82e » Kevin
2012-03-21 Debugging away ...
56 // Set pins low by default (except reset)
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
57 if (! hwSPI) {
58 digitalWrite(sid, LOW);
59 digitalWrite(sclk, LOW);
60 }
7890b82e » Kevin
2012-03-21 Debugging away ...
61 digitalWrite(cs, LOW);
62 digitalWrite(rst, HIGH);
1408b768 » Kevin
2012-03-20 First commit
63
64 // Reset the LCD
65 digitalWrite(rst, HIGH);
66 delay(100);
67 digitalWrite(rst, LOW);
68 delay(50);
69 digitalWrite(rst, HIGH);
70 delay(50);
71
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
72
73 csport = portOutputRegister(digitalPinToPort(cs));
74 cspinmask = digitalPinToBitMask(cs);
75
76 if (! hwSPI) {
77 clkport = portOutputRegister(digitalPinToPort(sclk));
78 clkpinmask = digitalPinToBitMask(sclk);
79 dataport = portOutputRegister(digitalPinToPort(sid));
80 datapinmask = digitalPinToBitMask(sid);
81 } else {
82 clkport = portOutputRegister(digitalPinToPort(13));
83 clkpinmask = digitalPinToBitMask(13);
84 dataport = portOutputRegister(digitalPinToPort(11));
85 datapinmask = digitalPinToBitMask(11);
86 SPI.begin();
87 SPI.setClockDivider(SPI_CLOCK_DIV8); // 4 MHz (half speed)
88 SPI.setBitOrder(MSBFIRST);
89 SPI.setDataMode(SPI_MODE0);
90 pinMode(13, OUTPUT);
91 pinMode(11, OUTPUT);
92 }
93
94 *csport &= ~cspinmask;
1408b768 » Kevin
2012-03-20 First commit
95 HX8340B_command(HX8340B_N_SETEXTCMD);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
96 writeData(0xFF);
97 writeData(0x83);
98 writeData(0x40);
1408b768 » Kevin
2012-03-20 First commit
99
100 HX8340B_command(HX8340B_N_SPLOUT);
101 delay(100);
102
103 HX8340B_command(0xCA); // Undocumented register?
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
104 writeData(0x70);
105 writeData(0x00);
106 writeData(0xD9);
107 writeData(0x01);
108 writeData(0x11);
1408b768 » Kevin
2012-03-20 First commit
109
110 HX8340B_command(0xC9); // Undocumented register?
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
111 writeData(0x90);
112 writeData(0x49);
113 writeData(0x10);
114 writeData(0x28);
115 writeData(0x28);
116 writeData(0x10);
117 writeData(0x00);
118 writeData(0x06);
1408b768 » Kevin
2012-03-20 First commit
119 delay(20);
120
121 HX8340B_command(HX8340B_N_SETGAMMAP);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
122 writeData(0x60);
123 writeData(0x71);
124 writeData(0x01);
125 writeData(0x0E);
126 writeData(0x05);
127 writeData(0x02);
128 writeData(0x09);
129 writeData(0x31);
130 writeData(0x0A);
1408b768 » Kevin
2012-03-20 First commit
131
132 HX8340B_command(HX8340B_N_SETGAMMAN);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
133 writeData(0x67);
134 writeData(0x30);
135 writeData(0x61);
136 writeData(0x17);
137 writeData(0x48);
138 writeData(0x07);
139 writeData(0x05);
140 writeData(0x33);
1408b768 » Kevin
2012-03-20 First commit
141 delay(10);
142
143 HX8340B_command(HX8340B_N_SETPWCTR5);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
144 writeData(0x35);
145 writeData(0x20);
146 writeData(0x45);
1408b768 » Kevin
2012-03-20 First commit
147
148 HX8340B_command(HX8340B_N_SETPWCTR4);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
149 writeData(0x33);
150 writeData(0x25);
151 writeData(0x4c);
1408b768 » Kevin
2012-03-20 First commit
152 delay(10);
153
154 HX8340B_command(HX8340B_N_COLMOD); // Color Mode
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
155 writeData(0x05); // 0x05 = 16bpp, 0x06 = 18bpp
1408b768 » Kevin
2012-03-20 First commit
156
157 HX8340B_command(HX8340B_N_DISPON);
158 delay(10);
159
160 HX8340B_command(HX8340B_N_CASET);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
161 writeData(0x00);
162 writeData(0x00);
163 writeData(0x00);
164 writeData(0xaf); // 175
1408b768 » Kevin
2012-03-20 First commit
165
166 HX8340B_command(HX8340B_N_PASET);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
167 writeData(0x00);
168 writeData(0x00);
169 writeData(0x00);
170 writeData(0xdb); // 219
1408b768 » Kevin
2012-03-20 First commit
171
7890b82e » Kevin
2012-03-21 Debugging away ...
172 HX8340B_command(HX8340B_N_RAMWR);
173
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
174 *csport |= cspinmask;
175 //clearDisplay();
1408b768 » Kevin
2012-03-20 First commit
176 }
177
178 // clear everything
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
179 void Adafruit_HX8340B::fillDisplay(uint16_t c) {
180 fillRect(0, 0, HX8340B_LCDWIDTH, HX8340B_LCDHEIGHT, c);
1408b768 » Kevin
2012-03-20 First commit
181 }
182
183 void Adafruit_HX8340B::invertDisplay(uint8_t i) {
184 }
185
3a1ed094 » ladyada
2012-04-17 sped up BMP drawing
186 void Adafruit_HX8340B::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
187 setWindow(x, y, x+w-1, y+h-1);
188
189 *csport &= ~cspinmask;
190
3a1ed094 » ladyada
2012-04-17 sped up BMP drawing
191 uint32_t i = w;
192 i *= h;
193
194 while (i--)
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
195 {
3a1ed094 » ladyada
2012-04-17 sped up BMP drawing
196 writeData((color>>8) & 0xFF);
197 writeData(color & 0xFF);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
198 }
199
200 *csport |= cspinmask;
201 }
202
3a1ed094 » ladyada
2012-04-17 sped up BMP drawing
203 void Adafruit_HX8340B::pushColor(uint16_t color) {
204 *csport &= ~cspinmask;
205 writeData((color>>8) & 0xFF);
206 writeData(color & 0xFF);
207 *csport |= cspinmask;
208 }
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
209
210 // the most basic function, set a single pixel
84db24d0 » PaintYourDragon
2012-04-09 Tweaks for Adafruit_GFX
211 void Adafruit_HX8340B::drawPixel(int16_t x, int16_t y, uint16_t color) {
212 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
213 return;
214
215 setWindow(x, y, x+1, y+1);
216 *csport &= ~cspinmask;
217 writeData((color>>8) & 0xFF);
218 writeData(color & 0xFF);
219 *csport |= cspinmask;
220 }
221
222
1408b768 » Kevin
2012-03-20 First commit
223 void Adafruit_HX8340B::HX8340B_command(uint8_t c) {
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
224 // Prepend leading bit instead of D/C pin
1408b768 » Kevin
2012-03-20 First commit
225
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
226 if (hwSPI) {
227 uint8_t saved_spimode = SPCR;
228 SPCR = 0;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
229
230 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined (__AVR_ATmega328__) || defined(__AVR_ATmega8__)
231 PORTB &= ~_BV(3); // PB3 = MOSI
232 PORTB |= _BV(5); // PB5 = SCLK
233 PORTB &= ~_BV(5);
234 // also do mega next eh?
235 #else
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
236 *dataport &= ~datapinmask;
237 *clkport |= clkpinmask;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
238 *clkport &= ~clkpinmask;
239 #endif
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
240
241 SPCR = saved_spimode;
242
243 SPDR = c;
244 while(!(SPSR & _BV(SPIF)));
245 } else {
246 *dataport &= ~datapinmask;
247 *clkport |= clkpinmask;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
248 *clkport &= ~clkpinmask;
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
249 for(uint8_t bit = 0x80; bit; bit >>= 1) {
250 if(c & bit) *dataport |= datapinmask;
251 else *dataport &= ~datapinmask;
252 *clkport |= clkpinmask;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
253 *clkport &= ~clkpinmask;
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
254 }
255 }
1408b768 » Kevin
2012-03-20 First commit
256 }
257
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
258
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
259 void Adafruit_HX8340B::writeData(uint8_t c) {
260 // Prepend leading bit instead of D/C pin
261 if (hwSPI) {
262 uint8_t saved_spimode = SPCR;
263 SPCR = 0;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
264 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined (__AVR_ATmega328__) || defined(__AVR_ATmega8__)
265 PORTB |= _BV(3); // PB3 = MOSI
266 PORTB |= _BV(5); // PB5 = SCLK
267 PORTB &= ~_BV(5);
268 // also do mega next eh?
269 #else
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
270 *dataport |= datapinmask;
271 *clkport |= clkpinmask;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
272 *clkport &= ~clkpinmask;
273 #endif
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
274 SPCR = saved_spimode;
275 SPDR = c;
276 while(!(SPSR & _BV(SPIF)));
277 } else {
278 *dataport |= datapinmask;
279 *clkport |= clkpinmask;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
280 *clkport &= ~clkpinmask;
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
281 for(uint8_t bit = 0x80; bit; bit >>= 1) {
282 if(c & bit) *dataport |= datapinmask;
283 else *dataport &= ~datapinmask;
284 *clkport |= clkpinmask;
8336ce3c » ladyada
2012-03-21 high speed drawing working - still would be nice to optimize drawing …
285 *clkport &= ~clkpinmask;
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
286 }
287 }
1408b768 » Kevin
2012-03-20 First commit
288 }
289
290 void Adafruit_HX8340B::writereg(uint8_t reg, uint8_t value) {
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
291 *csport &= ~cspinmask;
1408b768 » Kevin
2012-03-20 First commit
292 HX8340B_command(reg);
293 HX8340B_command(value);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
294 *csport |= cspinmask;
1408b768 » Kevin
2012-03-20 First commit
295 }
296
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
297 void Adafruit_HX8340B::setWindow(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
1408b768 » Kevin
2012-03-20 First commit
298 {
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
299 *csport &= ~cspinmask;
1408b768 » Kevin
2012-03-20 First commit
300 HX8340B_command(HX8340B_N_CASET);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
301 writeData(x0>>8);
302 writeData(x0);
303 writeData(x1>>8);
304 writeData(x1);
1408b768 » Kevin
2012-03-20 First commit
305
306 HX8340B_command(HX8340B_N_PASET);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
307 writeData(y0>>8);
308 writeData(y0);
309 writeData(y1>>8);
310 writeData(y1);
1408b768 » Kevin
2012-03-20 First commit
311
312 HX8340B_command(HX8340B_N_RAMWR);
91bbfc93 » ladyada
2012-03-21 works in slow mode, now trying to get it working in hw SPI
313 *csport |= cspinmask;
1408b768 » Kevin
2012-03-20 First commit
314 }
d3ccbf97 » ladyada
2012-03-21 working (slow) BMP drawing code - need optimization later
315
316 uint16_t Adafruit_HX8340B::Color565(uint8_t r, uint8_t g, uint8_t b) {
317 uint16_t c;
318 c = r >> 3;
319 c <<= 6;
320 c |= g >> 2;
321 c <<= 5;
322 c |= b >> 3;
323
324 return c;
325 }
Something went wrong with that request. Please try again.