Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad-dyno1-ebz/src/ST7565R.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ST7565R.c
3 * @brief Implementation of ST7565R Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43 #include "ST7565R.h" // ST7565R definitions
44 #include "Communication.h" // Communication definitions
45 #include "Time.h" // Time definitions
46
47 /******************************************************************************/
48 /************************* Constants Definitions ******************************/
49 /******************************************************************************/
50 const unsigned char st7565rFont[95][5] =
51 {
52 {0x00, 0x00, 0x00, 0x00, 0x00}, // sp
53 {0x00, 0x00, 0x2f, 0x00, 0x00}, // !
54 {0x00, 0x07, 0x00, 0x07, 0x00}, // "
55 {0x14, 0x7f, 0x14, 0x7f, 0x14}, // #
56 {0x24, 0x2a, 0x7f, 0x2a, 0x12}, // $
57 {0xc4, 0xc8, 0x10, 0x26, 0x46}, // %
58 {0x36, 0x49, 0x55, 0x22, 0x50}, // &
59 {0x00, 0x05, 0x03, 0x00, 0x00}, // '
60 {0x00, 0x1c, 0x22, 0x41, 0x00}, // (
61 {0x00, 0x41, 0x22, 0x1c, 0x00}, // )
62 {0x14, 0x08, 0x3E, 0x08, 0x14}, // *
63 {0x08, 0x08, 0x3E, 0x08, 0x08}, // +
64 {0x00, 0x00, 0x50, 0x30, 0x00}, // ,
65 {0x10, 0x10, 0x10, 0x10, 0x10}, // -
66 {0x00, 0x60, 0x60, 0x00, 0x00}, // .
67 {0x20, 0x10, 0x08, 0x04, 0x02}, // /
68 {0x3E, 0x51, 0x49, 0x45, 0x3E}, // 0
69 {0x00, 0x42, 0x7F, 0x40, 0x00}, // 1
70 {0x42, 0x61, 0x51, 0x49, 0x46}, // 2
71 {0x21, 0x41, 0x45, 0x4B, 0x31}, // 3
72 {0x18, 0x14, 0x12, 0x7F, 0x10}, // 4
73 {0x27, 0x45, 0x45, 0x45, 0x39}, // 5
74 {0x3C, 0x4A, 0x49, 0x49, 0x30}, // 6
75 {0x01, 0x71, 0x09, 0x05, 0x03}, // 7
76 {0x36, 0x49, 0x49, 0x49, 0x36}, // 8
77 {0x06, 0x49, 0x49, 0x29, 0x1E}, // 9
78 {0x00, 0x36, 0x36, 0x00, 0x00}, // :
79 {0x00, 0x56, 0x36, 0x00, 0x00}, // ;
80 {0x08, 0x14, 0x22, 0x41, 0x00}, // <
81 {0x14, 0x14, 0x14, 0x14, 0x14}, // =
82 {0x00, 0x41, 0x22, 0x14, 0x08}, // >
83 {0x02, 0x01, 0x51, 0x09, 0x06}, // ?
84 {0x32, 0x49, 0x59, 0x51, 0x3E}, // @
85 {0x7E, 0x11, 0x11, 0x11, 0x7E}, // A
86 {0x7F, 0x49, 0x49, 0x49, 0x36}, // B
87 {0x3E, 0x41, 0x41, 0x41, 0x22}, // C
88 {0x7F, 0x41, 0x41, 0x22, 0x1C}, // D
89 {0x7F, 0x49, 0x49, 0x49, 0x41}, // E
90 {0x7F, 0x09, 0x09, 0x09, 0x01}, // F
91 {0x3E, 0x41, 0x49, 0x49, 0x7A}, // G
92 {0x7F, 0x08, 0x08, 0x08, 0x7F}, // H
93 {0x00, 0x41, 0x7F, 0x41, 0x00}, // I
94 {0x20, 0x40, 0x41, 0x3F, 0x01}, // J
95 {0x7F, 0x08, 0x14, 0x22, 0x41}, // K
96 {0x7F, 0x40, 0x40, 0x40, 0x40}, // L
97 {0x7F, 0x02, 0x0C, 0x02, 0x7F}, // M
98 {0x7F, 0x04, 0x08, 0x10, 0x7F}, // N
99 {0x3E, 0x41, 0x41, 0x41, 0x3E}, // O
100 {0x7F, 0x09, 0x09, 0x09, 0x06}, // P
101 {0x3E, 0x41, 0x51, 0x21, 0x5E}, // Q
102 {0x7F, 0x09, 0x19, 0x29, 0x46}, // R
103 {0x46, 0x49, 0x49, 0x49, 0x31}, // S
104 {0x01, 0x01, 0x7F, 0x01, 0x01}, // T
105 {0x3F, 0x40, 0x40, 0x40, 0x3F}, // U
106 {0x1F, 0x20, 0x40, 0x20, 0x1F}, // V
107 {0x3F, 0x40, 0x38, 0x40, 0x3F}, // W
108 {0x63, 0x14, 0x08, 0x14, 0x63}, // X
109 {0x07, 0x08, 0x70, 0x08, 0x07}, // Y
110 {0x61, 0x51, 0x49, 0x45, 0x43}, // Z
111 {0x00, 0x7F, 0x41, 0x41, 0x00}, // [
112 {0x55, 0x2A, 0x55, 0x2A, 0x55}, // /
113 {0x00, 0x41, 0x41, 0x7F, 0x00}, // ]
114 {0x04, 0x02, 0x01, 0x02, 0x04}, // ^
115 {0x40, 0x40, 0x40, 0x40, 0x40}, // _
116 {0x00, 0x01, 0x02, 0x04, 0x00}, // '
117 {0x20, 0x54, 0x54, 0x54, 0x78}, // a
118 {0x7F, 0x48, 0x44, 0x44, 0x38}, // b
119 {0x38, 0x44, 0x44, 0x44, 0x20}, // c
120 {0x38, 0x44, 0x44, 0x48, 0x7F}, // d
121 {0x38, 0x54, 0x54, 0x54, 0x18}, // e
122 {0x08, 0x7E, 0x09, 0x01, 0x02}, // f
123 {0x0C, 0x52, 0x52, 0x52, 0x3E}, // g
124 {0x7F, 0x08, 0x04, 0x04, 0x78}, // h
125 {0x00, 0x44, 0x7D, 0x40, 0x00}, // i
126 {0x20, 0x40, 0x44, 0x3D, 0x00}, // j
127 {0x7F, 0x10, 0x28, 0x44, 0x00}, // k
128 {0x00, 0x41, 0x7F, 0x40, 0x00}, // l
129 {0x7C, 0x04, 0x18, 0x04, 0x78}, // m
130 {0x7C, 0x08, 0x04, 0x04, 0x78}, // n
131 {0x38, 0x44, 0x44, 0x44, 0x38}, // o
132 {0x7C, 0x14, 0x14, 0x14, 0x08}, // p
133 {0x08, 0x14, 0x14, 0x18, 0x7C}, // q
134 {0x7C, 0x08, 0x04, 0x04, 0x08}, // r
135 {0x48, 0x54, 0x54, 0x54, 0x20}, // s
136 {0x04, 0x3F, 0x44, 0x40, 0x20}, // t
137 {0x3C, 0x40, 0x40, 0x20, 0x7C}, // u
138 {0x1C, 0x20, 0x40, 0x20, 0x1C}, // v
139 {0x3C, 0x40, 0x30, 0x40, 0x3C}, // w
140 {0x44, 0x28, 0x10, 0x28, 0x44}, // x
141 {0x0C, 0x50, 0x50, 0x50, 0x3C}, // y
142 {0x44, 0x64, 0x54, 0x4C, 0x44}, // z
143 {0x08, 0x3e, 0x41, 0x41, 0x00}, // {
144 {0x00, 0x00, 0x77, 0x00, 0x00}, // |
145 {0x00, 0x41, 0x41, 0x3e, 0x08}, // }
146 {0x02, 0x01, 0x02, 0x01, 0x00} // ~
147 };
148
149 /***************************************************************************//**
150 * @brief Transmits 8 bits of command to ST7565R controller.
151 *
152 * @param command - Command to transmit.
153 *
154 * @return None.
155 *******************************************************************************/
156 void ST7565R_SetCommand(unsigned char command)
157 {
158 int delay;
159 ST7565R_A0_LOW;
160 SPI_Write(ST7565R_SLAVE_ID, // The ID of the selected slave device.
161 &command, // Write buffer.
162 1); // Number of bytes to write.
163 for(delay = 0; delay < 20; delay++)
164 __nop();
165 }
166
167 /***************************************************************************//**
168 * @brief Transmits 8 bits of data to ST7565R controller.
169 *
170 * @param data - Data to transmit.
171 *
172 * @return None.
173 *******************************************************************************/
174 void ST7565R_SetData(unsigned char data)
175 {
176 int delay;
177 ST7565R_A0_HIGH;
178 SPI_Write(ST7565R_SLAVE_ID, // The ID of the selected slave device.
179 &data, // Write buffer.
180 1); // Number of bytes to write.
181 for(delay = 0; delay < 20; delay++)
182 __nop();
183
184 }
185
186 /***************************************************************************//**
187 * @brief Sets page and column adresses.
188 *
189 * @param pageAddr - page address.
190 * @param columnAddr - column address.
191 *
192 * @return None.
193 *******************************************************************************/
194 void ST7565R_SetPageColumn(unsigned char pageAddr,
195 unsigned char columnAddr)
196 {
197 columnAddr += 4;
198 ST7565R_SetCommand(ST7565R_PAGE_ADDR(pageAddr));
199 ST7565R_SetCommand(ST7565R_COLUMN_ADDR_H(((columnAddr & 0xF0) >> 4)));
200 ST7565R_SetCommand(ST7565R_COLUMN_ADDR_L(((columnAddr & 0x0F) >> 0)));
201 }
202
203 /***************************************************************************//**
204 * @brief Initializes the ST7565R controller.
205 *
206 * @return status - Result of the initialization procedure.
207 * Example: 0 - if initialization was successful;
208 * -1 - if initialization was unsuccessful.
209 *******************************************************************************/
210 char ST7565R_Init(void)
211 {
212 char status = 0;
213 unsigned char page = 0;
214 unsigned char column = 0;
215
216 status = SPI_Init(0, // Transfer format.
217 1000000, // SPI clock frequency.
218 1, // SPI clock polarity.
219 0); // SPI clock edge.
220 if(status != 0)
221 {
222 return status;
223 }
224
225 ST7565R_A0_LOW;
226 ST7565R_RST_LOW;
227 ST7565R_A0_PIN_OUT;
228 ST7565R_RST_PIN_OUT;
229
230 TIME_DelayMs(2); // Wait for power to be stable.
231 ST7565R_RST_HIGH;
232 TIME_DelayMs(1); // Wait for reset circuit finished.
233
234 ST7565R_SetCommand(0x2F); // Booster circuit: ON;
235 // Voltage regulator circuit: ON;
236 // Voltage follower circuit: ON.
237 ST7565R_SetCommand(0x21); // Voltage regulator internal resistor ratio 1.
238 #if 0
239 ST7565R_SetCommand(0xC8); // Select COM output scan direction: reverse direction.
240 ST7565R_SetCommand(0xA0); // Set the display RAM address SEG output correspondence: normal.
241 #else
242 ST7565R_SetCommand(0xC0); // Select COM output scan direction: normal direction.
243 ST7565R_SetCommand(0xA1); // Set the display RAM address SEG output correspondence: reverse.
244
245 #endif
246 ST7565R_SetCommand(0xAF); // LCD display ON.
247
248 for(page = 0; page <= 8; page++)
249 {
250 /* Set the page and column addresses. */
251 ST7565R_SetPageColumn(page, 0);
252 for(column = 0; column <= 131; column++)
253 {
254 ST7565R_SetData(0x00);
255 }
256 }
257
258 return status;
259 }
260
261 /***************************************************************************//**
262 * @brief Sends a character to ST7565R controller.
263 *
264 * @param pageAddr - page address.
265 * @param columnAddr - column address.
266 * @param character - The character.
267 * @param bigFont - Big font selection.
268 *
269 * @return None.
270 *******************************************************************************/
271 void ST7565R_Char(unsigned char pageAddr,
272 unsigned char columnAddr,
273 char character,
274 unsigned char bigFont)
275 {
276 unsigned char column = 0;
277 unsigned char data = 0;
278
279 if(bigFont)
280 {
281 /* Set the page and column addresses. */
282 ST7565R_SetPageColumn(pageAddr, columnAddr);
283 /* Send character data. */
284 for(column = 0; column < 5; column++)
285 {
286 data = st7565rFont[character - 32][column];
287 data = ((data & 0x01) ? 0x03 : 0) |
288 ((data & 0x02) ? 0x0C : 0) |
289 ((data & 0x04) ? 0x30 : 0) |
290 ((data & 0x08) ? 0xC0 : 0);
291 ST7565R_SetData(data);
292 ST7565R_SetData(data);
293 ST7565R_SetData(data);
294 }
295 ST7565R_SetData(0);
296 ST7565R_SetData(0);
297 ST7565R_SetData(0);
298 pageAddr++;
299 /* Set the page and column addresses. */
300 ST7565R_SetPageColumn(pageAddr, columnAddr);
301 /* Send character data. */
302 for(column = 0; column < 5; column++)
303 {
304 data = st7565rFont[character - 32][column];
305 data = ((data & 0x10) ? 0x03 : 0) |
306 ((data & 0x20) ? 0x0C : 0) |
307 ((data & 0x40) ? 0x30 : 0) |
308 ((data & 0x80) ? 0xC0 : 0);
309 ST7565R_SetData(data);
310 ST7565R_SetData(data);
311 ST7565R_SetData(data);
312 }
313 ST7565R_SetData(0);
314 ST7565R_SetData(0);
315 ST7565R_SetData(0);
316 }
317 else
318 {
319 /* Set the page and column addresses. */
320 ST7565R_SetPageColumn(pageAddr, columnAddr);
321 /* Send character data. */
322 for(column = 0; column < 5; column++)
323 {
324 ST7565R_SetData(st7565rFont[character - 32][column] << 1);
325 }
326 ST7565R_SetData(0x00);
327 }
328 }
329
330 /***************************************************************************//**
331 * @brief Sends a string to ST7565R controller.
332 *
333 * @param pageAddr - page address.
334 * @param columnAddr - column address.
335 * @param string - The string.
336 * @param bigFont - Big font selection.
337 *
338 * @return None.
339 *******************************************************************************/
340 void ST7565R_String(unsigned char pageAddr,
341 unsigned char columnAddr,
342 char* string,
343 unsigned char bigFont)
344 {
345 while(*string)
346 {
347 /* Send each character of the string. */
348 ST7565R_Char(pageAddr, columnAddr, *string++, bigFont);
349 columnAddr += 6 + (bigFont * 12);
350 }
351 }
352
353 /***************************************************************************//**
354 * @brief Sends an image array to ST7565R controller.
355 *
356 * @param pageAddr - page address.
357 * @param columnAddr - column address.
358 * @param image - The image array.
359 * @param width - The width of the image (pixels).
360 * @param height - The height of the image (pixels).
361 *
362 * @return None.
363 *******************************************************************************/
364 void ST7565R_Image(unsigned char pageAddr,
365 unsigned char columnAddr,
366 const unsigned char image[],
367 unsigned char width,
368 unsigned char height)
369 {
370 unsigned short index = 0;
371
372 /* Set the page and column addresses. */
373 ST7565R_SetPageColumn(pageAddr, columnAddr);
374 for(index = 0; index < (width * height / 8); index++)
375 {
376 if(!(index % width))
377 {
378 /* Set the page and column addresses. */
379 ST7565R_SetPageColumn(pageAddr, columnAddr);
380 pageAddr++;
381 }
382 ST7565R_SetData(image[index]);
383 }
384 }
385 /***************************************************************************//**
386 * @brief Sends an integer number to ST7565R controller.
387 *
388 * @param pageAddr - page address.
389 * @param columnAddr - column address.
390 * @param number - The number.
391 * @param bigFont - Big font selection.
392 // * @param nrDig - Number of digits
393 *
394 * @return None.
395 *******************************************************************************/
396 void ST7565R_Number(unsigned char pageAddr,
397 unsigned char columnAddr,
398 long number,
399 unsigned char bigFont)
400 {
401 unsigned long mask = 1000000000;
402 unsigned char chNumber = 10;
403 unsigned char chIndex = 0;
404
405 if(number < 0)
406 {
407 ST7565R_Char(pageAddr, columnAddr, '-', bigFont);
408 columnAddr += (bigFont * 12);
409 number *= -1;
410 }
411 else
412 {
413 if(number == 0)
414 {
415 ST7565R_String(pageAddr, columnAddr, "0", bigFont);
416 }
417 // else
418 // {
419 // ST7565R_Char(pageAddr, columnAddr, '+', bigFont);
420 // columnAddr += 6 + (bigFont * 12);
421 // }
422 }
423 while(mask > number)
424 {
425 mask /= 10;
426 chNumber--;
427 }
428 for(chIndex = 0; chIndex < chNumber; chIndex++)
429 {
430 ST7565R_Char(pageAddr, columnAddr, ((number / mask) % 10) + 0x30, bigFont);
431 mask /= 10;
432 columnAddr += 6 + (bigFont * 12);
433 }
434 }
435 /***************************************************************************//**
436 * @brief Clears the screen.
437 *
438 * @param None.
439 *
440 * @return None.
441 *******************************************************************************/
442 void ClearScreen(void)
443 {
444 ST7565R_String(0, 0, " ", 0);
445 ST7565R_String(1, 0, " ", 0);
446 ST7565R_String(2, 0, " ", 0);
447 ST7565R_String(3, 0, " ", 0);
448 }
449 /***************************************************************************//**
450 * @brief Sends a float number to ST7579 controller.
451 *
452 * @param pageAddr - Y address of RAM.
453 * @param columnAddr - X address of RAM.
454 * @param number - The number.
455 * @param resolution - Float resolution.
456 * @param bigFont - Big font selection.
457 *
458 * @return None.
459 *******************************************************************************/
460 void ST7565R_FloatNumber(unsigned char pageAddr, // y
461 unsigned char columnAddr, // x
462 float number,
463 unsigned char resolution,
464 unsigned char bigFont)
465
466 {
467 unsigned long mask = 1000000000;
468 unsigned char chNumber = 10;
469 unsigned char index = 0;
470 long display = 1;
471 long multiplication = 1;
472
473
474 for(index = 0; index < resolution; index++)
475 {
476 multiplication *= 10;
477 }
478 display = (long)(number * multiplication);
479 if(display < 0)
480 {
481 ST7565R_Char(pageAddr, columnAddr, '-', bigFont);
482 columnAddr += 6 + (bigFont * 12);
483 display *= -1;
484 }
485 else
486 {
487 if(display == 0)
488 {
489 columnAddr += 6 + (bigFont * 12);
490 ST7565R_Char(pageAddr, columnAddr, '0', bigFont);
491 columnAddr += 6 + (bigFont * 12);
492 if(resolution)
493 {
494 ST7565R_Char(pageAddr, columnAddr, '.', bigFont);
495 columnAddr += 6 + (bigFont * 12);
496 }
497 for(index = 0; index < resolution; index++)
498 {
499 ST7565R_Char(pageAddr, columnAddr, '0', bigFont);
500 columnAddr += 6 + (bigFont * 12);
501 }
502 return;
503 }
504 }
505 while((mask > display) && (chNumber > resolution))
506 {
507 mask /= 10;
508 chNumber--;
509 }
510 if((display > 0) && (display < multiplication))
511 {
512 ST7565R_String(pageAddr, columnAddr, "0.", bigFont);
513 columnAddr += 12 + (bigFont * 24);
514 }
515 for(index = 0; index < chNumber; index++)
516 {
517 ST7565R_Char(pageAddr, columnAddr, ((display / mask) % 10) + 0x30, bigFont);
518 mask /= 10;
519 columnAddr += 6 + (bigFont * 12);
520 if((multiplication != 1) && (mask == (multiplication / 10)))
521 {
522 ST7565R_Char(pageAddr, columnAddr, '.', bigFont);
523 columnAddr += 6 + (bigFont * 12);
524 }
525 }
526 }