极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 28574|回复: 21

贡献一个开源的“基于硬件SPI的LCD5110库”

[复制链接]
发表于 2012-9-26 15:00:58 | 显示全部楼层 |阅读模式
本帖最后由 test01 于 2012-11-24 00:37 编辑
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

  4. #include <pins_arduino.h>
  5. #include <SPI.h>

  6. const bool LCD5110_MODE_COMMAND = LOW;
  7. const bool LCD5110_MODE_DATA = HIGH;
  8. const uint_fast8_t LCD5110_TRANSMISSION_UNIT_LENGTH_BITS = 8;

  9. const uint_fast8_t LCD5110_RAM_WIDTH_BITS = 8;
  10. const uint_fast8_t LCD5110_RAM_MAX_X = 84, LCD5110_RAM_MAX_Y = 6;  //RAM X is 0-83, Y is 0-5
  11. const uint_fast8_t LCD5110_MAX_ROW = 6, LCD5110_MAX_COLUMN = 12;
  12. const uint_fast8_t LCD5110_MAXIMUM_CHARACTERS = LCD5110_MAX_ROW * LCD5110_MAX_COLUMN;
  13. const uint_fast8_t ASCII_TOTAL_NUMBER = 128;
  14. const uint_fast8_t ASCII_FONT_WIDTH = 7;
  15. const unsigned char ASCII_BITMAP_FONT [ASCII_TOTAL_NUMBER] [ASCII_FONT_WIDTH] PROGMEM =  //GCC 4.7.1 DEL PROGMEM
  16. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 blank
  17. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 01 blank
  18. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 02 blank
  19. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 03 blank
  20. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 04 blank
  21. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 05 blank
  22. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 06 blank
  23. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 07 blank
  24. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 08 blank
  25. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 09 blank
  26. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0a blank
  27. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0b blank
  28. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0c blank
  29. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0d blank
  30. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0e blank
  31. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 0f blank
  32. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 10 blank
  33. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 11 blank
  34. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 12 blank
  35. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 13 blank
  36. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 14 blank
  37. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 15 blank
  38. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 16 blank
  39. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 17 blank
  40. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 18 blank
  41. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 19 blank
  42. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 1a blank
  43. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 1b blank
  44. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 1c blank
  45. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 1d blank
  46. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 1e blank
  47. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 1f blank
  48. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 20  
  49. ,{0x00, 0x00, 0x00, 0x5f, 0x00, 0x00, 0x00} // 21 !
  50. ,{0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00} // 22 "
  51. ,{0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14, 0x00} // 23 #
  52. ,{0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12, 0x00} // 24 $
  53. ,{0x00, 0x23, 0x13, 0x08, 0x64, 0x62, 0x00} // 25 %
  54. ,{0x00, 0x36, 0x49, 0x55, 0x22, 0x50, 0x00} // 26 &
  55. ,{0x00, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00} // 27 '
  56. ,{0x00, 0x00, 0x1c, 0x22, 0x41, 0x00, 0x00} // 28 (
  57. ,{0x00, 0x00, 0x41, 0x22, 0x1c, 0x00, 0x00} // 29 )
  58. ,{0x00, 0x14, 0x08, 0x3e, 0x08, 0x14, 0x00} // 2a *
  59. ,{0x00, 0x08, 0x08, 0x3e, 0x08, 0x08, 0x00} // 2b +
  60. ,{0x00, 0x00, 0x50, 0x30, 0x00, 0x00, 0x00} // 2c ,
  61. ,{0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00} // 2d -
  62. ,{0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00} // 2e .
  63. ,{0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00} // 2f /
  64. ,{0x00, 0x3e, 0x51, 0x49, 0x45, 0x3e, 0x00} // 30 0
  65. ,{0x00, 0x00, 0x42, 0x7f, 0x40, 0x00, 0x00} // 31 1
  66. ,{0x00, 0x42, 0x61, 0x51, 0x49, 0x46, 0x00} // 32 2
  67. ,{0x00, 0x21, 0x41, 0x45, 0x4b, 0x31, 0x00} // 33 3
  68. ,{0x00, 0x18, 0x14, 0x12, 0x7f, 0x10, 0x00} // 34 4
  69. ,{0x00, 0x27, 0x45, 0x45, 0x45, 0x39, 0x00} // 35 5
  70. ,{0x00, 0x3c, 0x4a, 0x49, 0x49, 0x30, 0x00} // 36 6
  71. ,{0x00, 0x01, 0x71, 0x09, 0x05, 0x03, 0x00} // 37 7
  72. ,{0x00, 0x36, 0x49, 0x49, 0x49, 0x36, 0x00} // 38 8
  73. ,{0x00, 0x06, 0x49, 0x49, 0x29, 0x1e, 0x00} // 39 9
  74. ,{0x00, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00} // 3a :
  75. ,{0x00, 0x00, 0x56, 0x36, 0x00, 0x00, 0x00} // 3b ;
  76. ,{0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00} // 3c <
  77. ,{0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00} // 3d =
  78. ,{0x00, 0x00, 0x41, 0x22, 0x14, 0x08, 0x00} // 3e >
  79. ,{0x00, 0x02, 0x01, 0x51, 0x09, 0x06, 0x00} // 3f ?
  80. ,{0x00, 0x32, 0x49, 0x79, 0x41, 0x3e, 0x00} // 40 @
  81. ,{0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e, 0x00} // 41 A
  82. ,{0x00, 0x7f, 0x49, 0x49, 0x49, 0x36, 0x00} // 42 B
  83. ,{0x00, 0x3e, 0x41, 0x41, 0x41, 0x22, 0x00} // 43 C
  84. ,{0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c, 0x00} // 44 D
  85. ,{0x00, 0x7f, 0x49, 0x49, 0x49, 0x41, 0x00} // 45 E
  86. ,{0x00, 0x7f, 0x09, 0x09, 0x09, 0x01, 0x00} // 46 F
  87. ,{0x00, 0x3e, 0x41, 0x49, 0x49, 0x7a, 0x00} // 47 G
  88. ,{0x00, 0x7f, 0x08, 0x08, 0x08, 0x7f, 0x00} // 48 H
  89. ,{0x00, 0x00, 0x41, 0x7f, 0x41, 0x00, 0x00} // 49 I
  90. ,{0x00, 0x20, 0x40, 0x41, 0x3f, 0x01, 0x00} // 4a J
  91. ,{0x00, 0x7f, 0x08, 0x14, 0x22, 0x41, 0x00} // 4b K
  92. ,{0x00, 0x7f, 0x40, 0x40, 0x40, 0x40, 0x00} // 4c L
  93. ,{0x00, 0x7f, 0x02, 0x0c, 0x02, 0x7f, 0x00} // 4d M
  94. ,{0x00, 0x7f, 0x04, 0x08, 0x10, 0x7f, 0x00} // 4e N
  95. ,{0x00, 0x3e, 0x41, 0x41, 0x41, 0x3e, 0x00} // 4f O
  96. ,{0x00, 0x7f, 0x09, 0x09, 0x09, 0x06, 0x00} // 50 P
  97. ,{0x00, 0x3e, 0x41, 0x51, 0x21, 0x5e, 0x00} // 51 Q
  98. ,{0x00, 0x7f, 0x09, 0x19, 0x29, 0x46, 0x00} // 52 R
  99. ,{0x00, 0x46, 0x49, 0x49, 0x49, 0x31, 0x00} // 53 S
  100. ,{0x00, 0x01, 0x01, 0x7f, 0x01, 0x01, 0x00} // 54 T
  101. ,{0x00, 0x3f, 0x40, 0x40, 0x40, 0x3f, 0x00} // 55 U
  102. ,{0x00, 0x1f, 0x20, 0x40, 0x20, 0x1f, 0x00} // 56 V
  103. ,{0x00, 0x3f, 0x40, 0x38, 0x40, 0x3f, 0x00} // 57 W
  104. ,{0x00, 0x63, 0x14, 0x08, 0x14, 0x63, 0x00} // 58 X
  105. ,{0x00, 0x07, 0x08, 0x70, 0x08, 0x07, 0x00} // 59 Y
  106. ,{0x00, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00} // 5a Z
  107. ,{0x00, 0x00, 0x7f, 0x41, 0x41, 0x00, 0x00} // 5b [
  108. ,{0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00} // 5c \
  109. ,{0x00, 0x00, 0x41, 0x41, 0x7f, 0x00, 0x00} // 5d ]
  110. ,{0x00, 0x04, 0x02, 0x01, 0x02, 0x04, 0x00} // 5e ^
  111. ,{0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00} // 5f _
  112. ,{0x00, 0x00, 0x01, 0x02, 0x04, 0x00, 0x00} // 60 `
  113. ,{0x00, 0x20, 0x54, 0x54, 0x54, 0x78, 0x00} // 61 a
  114. ,{0x00, 0x7f, 0x48, 0x44, 0x44, 0x38, 0x00} // 62 b
  115. ,{0x00, 0x38, 0x44, 0x44, 0x44, 0x20, 0x00} // 63 c
  116. ,{0x00, 0x38, 0x44, 0x44, 0x48, 0x7f, 0x00} // 64 d
  117. ,{0x00, 0x38, 0x54, 0x54, 0x54, 0x18, 0x00} // 65 e
  118. ,{0x00, 0x08, 0x7e, 0x09, 0x01, 0x02, 0x00} // 66 f
  119. ,{0x00, 0x0c, 0x52, 0x52, 0x52, 0x3e, 0x00} // 67 g
  120. ,{0x00, 0x7f, 0x08, 0x04, 0x04, 0x78, 0x00} // 68 h
  121. ,{0x00, 0x00, 0x44, 0x7d, 0x40, 0x00, 0x00} // 69 i
  122. ,{0x00, 0x20, 0x40, 0x44, 0x3d, 0x00, 0x00} // 6a j
  123. ,{0x00, 0x7f, 0x10, 0x28, 0x44, 0x00, 0x00} // 6b k
  124. ,{0x00, 0x00, 0x41, 0x7f, 0x40, 0x00, 0x00} // 6c l
  125. ,{0x00, 0x7c, 0x04, 0x18, 0x04, 0x78, 0x00} // 6d m
  126. ,{0x00, 0x7c, 0x08, 0x04, 0x04, 0x78, 0x00} // 6e n
  127. ,{0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00} // 6f o
  128. ,{0x00, 0x7c, 0x14, 0x14, 0x14, 0x08, 0x00} // 70 p
  129. ,{0x00, 0x08, 0x14, 0x14, 0x18, 0x7c, 0x00} // 71 q
  130. ,{0x00, 0x7c, 0x08, 0x04, 0x04, 0x08, 0x00} // 72 r
  131. ,{0x00, 0x48, 0x54, 0x54, 0x54, 0x20, 0x00} // 73 s
  132. ,{0x00, 0x04, 0x3f, 0x44, 0x40, 0x20, 0x00} // 74 t
  133. ,{0x00, 0x3c, 0x40, 0x40, 0x20, 0x7c, 0x00} // 75 u
  134. ,{0x00, 0x1c, 0x20, 0x40, 0x20, 0x1c, 0x00} // 76 v
  135. ,{0x00, 0x3c, 0x40, 0x30, 0x40, 0x3c, 0x00} // 77 w
  136. ,{0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00} // 78 x
  137. ,{0x00, 0x0c, 0x50, 0x50, 0x50, 0x3c, 0x00} // 79 y
  138. ,{0x00, 0x44, 0x64, 0x54, 0x4c, 0x44, 0x00} // 7a z
  139. ,{0x00, 0x00, 0x08, 0x36, 0x41, 0x00, 0x00} // 7b {
  140. ,{0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00} // 7c |
  141. ,{0x00, 0x00, 0x41, 0x36, 0x08, 0x00, 0x00} // 7d }
  142. ,{0x00, 0x10, 0x08, 0x08, 0x10, 0x08, 0x00} // 7e ~
  143. ,{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 7f blank
  144. };
  145. const uint_fast8_t LCD5110_MAXIMUM_PIXEL_X = 84, LCD5110_MAXIMUM_PIXEL_Y = 48;  //LCD5110 max pixel X is 0-83, max pixel Y is 0-47
  146. const bool LCD5110_PIXEL_DRAW = true, LCD5110_PIXEL_CLEAR = false;

  147. const uint_fast8_t LCD5110_NO_ERROR = 0;
  148. const uint_fast8_t LCD5110_INPUT_TEXT_IS_NULL_ERROR = 1;
  149. const uint_fast8_t LCD5110_TEXT_LEN_IS_ZERO_ERROR = 2;
  150. const uint_fast8_t LCD5110_HAPPEN_BEYOND_SCREEN_RANGE_ERROR = 3;
  151. const uint_fast8_t LCD5110_TEXT_OVERLENGTH_ERROR = 4;
  152. const uint_fast8_t LCD5110_NON_ASCII_CHARACTERS_ERROR = 5;
  153. const uint_fast8_t LCD5110_NOT_IS_GRAPH_CHARACTERS_ERROR = 6;

  154. volatile static unsigned char display_ram [LCD5110_RAM_MAX_Y] [LCD5110_RAM_MAX_X];  //Automatic initialization display_ram[][] is 0
  155. volatile static uint_fast8_t initial_write_ram_x, initial_write_ram_y;  //Automatic initialization initial_write_ram_x and initial_write_ram_y is 0

  156. static uint_fast8_t dc_pin = 4;  //default pin 4 is DC

  157. inline void lcd5110_spi_configuration (void)  //Arduino with the statement
  158. {
  159.   SPI.setBitOrder(MSBFIRST);
  160.   SPI.setDataMode(SPI_MODE0);
  161.   SPI.setClockDivider(SPI_CLOCK_DIV2);
  162. }

  163. inline void lcd5110_start_write (void)  //This is needed before lcd5110_spi_configuration. Arduino with the statement
  164. {
  165.   SPI.begin();
  166.   digitalWrite(SS, LOW);  //Low is CHIP enable
  167. }

  168. inline void lcd5110_finish_write (void)  //Arduino with the statement
  169. {
  170.   digitalWrite(SS, HIGH);  //High is CHIP disable
  171.   SPI.end();
  172. }

  173. inline void lcd5110_write_byte (bool mode_select, unsigned char data)  //This is needed before lcd5110_start_write(), then need to lcd5110_finish_write(). Arduino with the statement
  174. {
  175.   digitalWrite(dc_pin, mode_select);
  176.   SPI.transfer(data);
  177. }

  178. void lcd5110_activate (void)  //This is needed before lcd5110_initialise
  179. {
  180.   {
  181.     lcd5110_start_write();
  182.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B100000);  // Set(chip is in active; horizontal addressing; use basic instruction set)
  183.     lcd5110_finish_write();
  184.   }
  185. }

  186. //The following is a public function: lcd5110_clear, lcd5110_initialise, lcd5110_display, lcd5110_blank, lcd5110_sleep, lcd5110_write_text, lcd5110_write_blink_character, lcd5110_put_pixel

  187. void lcd5110_clear (void)  //a public function. This is needed before lcd5110_spi_configuration.(In lcd5110_initialise has been running after)
  188. {
  189.   {
  190.     initial_write_ram_x = 0;
  191.     initial_write_ram_y = 0;
  192.   }
  193.   
  194.   for (int_fast8_t y_index = 0; y_index < LCD5110_RAM_MAX_Y; ++y_index)
  195.   {
  196.     for (int_fast8_t x_index = 0; x_index < LCD5110_RAM_MAX_X; ++x_index)
  197.     {
  198.       display_ram[y_index][x_index] = 0;
  199.     }
  200.   }
  201.   
  202.   {
  203.     lcd5110_start_write();
  204.     for (int_fast16_t index = 0; index < (uint_fast16_t)LCD5110_RAM_MAX_X * LCD5110_RAM_MAX_Y; ++index)
  205.     {
  206.       lcd5110_write_byte(LCD5110_MODE_DATA, 0x00);  //To Lcd5110 RAM write data 0
  207.     }
  208.     lcd5110_finish_write();
  209.   }
  210. }

  211. void lcd5110_initialise (void)  //a public function
  212. {
  213.   {  //del
  214.   const uint_fast8_t PIN_RESET = 6;
  215.   pinMode(PIN_RESET, OUTPUT);
  216.   digitalWrite(PIN_RESET, LOW);
  217.   digitalWrite(PIN_RESET, HIGH);
  218.   }
  219.   
  220.   lcd5110_spi_configuration();
  221.   pinMode(dc_pin, OUTPUT);
  222.   lcd5110_clear();  // The Lcd5110 RAM to clear
  223.   {
  224.     lcd5110_start_write();
  225.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B100101);  // Set(chip is in Power-down mode; horizontal addressing; use extended instruction set)
  226.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B11000011);  // Set Vop value for 1000011
  227.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B110);  // Set Temp coefficent for TC2
  228.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B10011);  // Set Bias mode to 1:48
  229.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B100100);  // Set(chip is in Power-down mode; horizontal addressing; use basic instruction set)
  230.     lcd5110_finish_write();
  231.   }
  232. }

  233. void lcd5110_display (void)  // Set the display. a public function. This is needed before lcd5110_initialise.
  234. {
  235.   {
  236.     lcd5110_start_write();
  237.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B1000000);  // Setting the initial write LCD5110 RAM Y=0
  238.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B10000000);  // Setting the initial write LCD5110 RAM X=0
  239.     for (int_fast8_t y_index = 0; y_index < LCD5110_RAM_MAX_Y; ++y_index)
  240.     {
  241.       for (int_fast8_t x_index = 0; x_index < LCD5110_RAM_MAX_X; ++x_index)
  242.       {
  243.         lcd5110_write_byte(LCD5110_MODE_DATA, display_ram[y_index][x_index]);
  244.       }
  245.     }
  246.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B1100);  // Set the display mode to normal mode.
  247.     lcd5110_finish_write();
  248.   }
  249. }

  250. void lcd5110_blank (void)  // Set the display blank. a public function. This is needed before lcd5110_initialise.
  251. {
  252.   {
  253.     lcd5110_start_write();
  254.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B1000);  // Set the display blank.
  255.     lcd5110_finish_write();
  256.   }
  257. }

  258. void lcd5110_sleep (void)  //a public function. This is needed before lcd5110_initialise
  259. {
  260.   lcd5110_clear();  // The Lcd5110 RAM to clear
  261.   {
  262.     lcd5110_start_write();
  263.     lcd5110_write_byte(LCD5110_MODE_COMMAND, B100100);  // Set(chip is in Power-down mode; horizontal addressing; use basic instruction set)
  264.     lcd5110_finish_write();
  265.   }
  266. }

  267. void lcd5110_write_text (char input_text[], uint_fast8_t text_len, uint_fast8_t initial_row, uint_fast8_t initial_column, uint_fast8_t error_code)
  268. /*a public function. This is needed before lcd5110_initialise.
  269.   text_len value must match the length of input_text[]!
  270.   input_text[] up to LCD5110_MAXIMUM_CHARACTERS characters, initial_row is 0 to LCD5110_MAX_ROW, initial_column is 0 to LCD5110_MAX_COLUMN.
  271.   initial_row if 0 remain initial_write_ram_y values, initial_column if 0 remain initial_write_ram_x values. */
  272. {
  273.   char internal_text [LCD5110_MAXIMUM_CHARACTERS + 1];
  274.   int_fast8_t str_index = -1;

  275.   lcd5110_blank();
  276.   
  277.   if (input_text == NULL)
  278.   {
  279.     error_code = LCD5110_INPUT_TEXT_IS_NULL_ERROR;  //"Pointer is empty error! "
  280.     return;
  281.   }
  282.   if (text_len == 0)
  283.   {
  284.     error_code = LCD5110_TEXT_LEN_IS_ZERO_ERROR;  //"text_len = 0 error! "
  285.     return;
  286.   }
  287.   if ((initial_row > LCD5110_MAX_ROW) or (initial_column > LCD5110_MAX_COLUMN))
  288.   {
  289.     error_code = LCD5110_HAPPEN_BEYOND_SCREEN_RANGE_ERROR;  //"Happen beyond screen range error! "
  290.     return;
  291.   }
  292.   do
  293.   {
  294.     ++str_index;
  295.     if (str_index > LCD5110_MAXIMUM_CHARACTERS)
  296.     {
  297.       error_code = LCD5110_TEXT_OVERLENGTH_ERROR;  //"Text overlength error! "
  298.       return;
  299.     }
  300.     if (input_text[str_index] < '\x0')
  301.     {
  302.       error_code = LCD5110_NON_ASCII_CHARACTERS_ERROR;  //"Non-ASCII Characters error! "
  303.       return;
  304.     }
  305.     internal_text[str_index] = input_text[str_index];
  306.   }while(not(str_index == text_len) and not(internal_text[str_index] == '\x0'));
  307.   
  308.   if (initial_row > 0)
  309.   {
  310.     initial_write_ram_y = initial_row - 1;
  311.   }
  312.   if (initial_column > 0)
  313.   {
  314.     initial_write_ram_x = ASCII_FONT_WIDTH * (initial_column - 1);
  315.   }
  316.   uint_fast8_t y_index = initial_write_ram_y;
  317.   uint_fast8_t x_index = initial_write_ram_x;
  318.   uint_fast8_t character_code;
  319.   for (int_fast8_t n = 0; n < str_index; ++n)
  320.   {
  321.     character_code = (uint_fast8_t)internal_text[n];
  322.     for (int_fast8_t font_index = 0; font_index < ASCII_FONT_WIDTH; ++font_index)
  323.     {
  324.       display_ram[y_index][x_index] = pgm_read_byte(&ASCII_BITMAP_FONT[character_code][font_index]);  //GCC 4.7.1 DEL pgm_read_byte(&)
  325.       x_index = (x_index + 1) % LCD5110_RAM_MAX_X;
  326.       if (x_index == 0)
  327.       {
  328.         y_index = (y_index + 1) % LCD5110_RAM_MAX_Y;
  329.       }
  330.     }
  331.   }
  332.   
  333.   lcd5110_display();
  334.   lcd5110_activate();
  335.   
  336.   error_code = LCD5110_NO_ERROR;
  337.   return;
  338. }

  339. void lcd5110_write_blink_character (char input_character, uint_fast8_t initial_row, uint_fast8_t initial_column, uint_fast8_t error_code)
  340. /*a public function. This is needed before lcd5110_initialise.
  341.   initial_row is 0 to LCD5110_MAX_ROW, initial_column is 0 to LCD5110_MAX_COLUMN.
  342.   initial_row if 0 remain initial_write_ram_y values, initial_column if 0 remain initial_write_ram_x values. */
  343. {
  344.   char internal_character_string [] = {'\x0', '\x0'};

  345.   internal_character_string[0] = input_character;
  346.   if (internal_character_string[0] < '\x0')
  347.   {
  348.     error_code = LCD5110_NON_ASCII_CHARACTERS_ERROR;
  349.     return;
  350.   }
  351.   if (not(isgraph(internal_character_string[0])))
  352.   {
  353.     error_code =  LCD5110_NOT_IS_GRAPH_CHARACTERS_ERROR;
  354.     return;
  355.   }
  356.   if ((initial_row > LCD5110_MAX_ROW) or (initial_column > LCD5110_MAX_COLUMN))
  357.   {
  358.     error_code = LCD5110_HAPPEN_BEYOND_SCREEN_RANGE_ERROR;
  359.     return;
  360.   }
  361.   for (int_fast8_t n = 0; n < 3; ++n)
  362.   {
  363.     uint_fast8_t lcd5110_write_text_of_error_code;
  364.     lcd5110_write_text(internal_character_string, 2, initial_row, initial_column, lcd5110_write_text_of_error_code);
  365.     delay(1000);
  366.     lcd5110_write_text(" ", 2, initial_row, initial_column, lcd5110_write_text_of_error_code);
  367.     delay(1000);
  368.   }
  369.   error_code = LCD5110_NO_ERROR;
  370.   return;
  371. }

  372. void lcd5110_put_pixel (uint_fast8_t pixel_x, uint_fast8_t pixel_y, bool pixel_mode)  //a public function. This is needed before lcd5110_initialise. pixel_x is 0 to LCD5110_MAXIMUM_PIXEL_X-1, pixel_y is 0 to LCD5110_MAXIMUM_PIXEL_X-1.
  373. {
  374.   if ((pixel_x < LCD5110_MAXIMUM_PIXEL_X) and (pixel_y < LCD5110_MAXIMUM_PIXEL_Y))
  375.   {
  376.     div_t pixel_y_position = div(pixel_y, LCD5110_TRANSMISSION_UNIT_LENGTH_BITS);
  377.     if (pixel_mode == LCD5110_PIXEL_DRAW)
  378.     {
  379.       display_ram[pixel_y_position.quot][pixel_x] = (unsigned char)write_bit(display_ram[pixel_y_position.quot][pixel_x], LCD5110_RAM_WIDTH_BITS, pixel_y_position.rem, 1);
  380.     }
  381.     else
  382.     {
  383.       display_ram[pixel_y_position.quot][pixel_x] = (unsigned char)write_bit(display_ram[pixel_y_position.quot][pixel_x], LCD5110_RAM_WIDTH_BITS, pixel_y_position.rem, 0);
  384.     }
  385.   }
  386.   else
  387.   {  //Beyond the scope of the screen doing nothing
  388.   }
  389. }
复制代码

==============================
此库特点除了硬件SPI,还有在RAM开了一个显存缓冲,再有查屏幕上有什么的时候就好办了
初始化重启屏幕用的PIN 6,建议只调试时候用,实际应用可做硬件重启,去掉相关段
库内有注释,还有应用问题就发帖吧
回复

使用道具 举报

发表于 2012-9-26 15:36:18 | 显示全部楼层
硬件SPI操作,可做硬件重启这点都差不多吧?我记得很多NOKIA 5110都是SPI控制的,沙发,顶你一个!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-9-26 16:20:17 | 显示全部楼层
Randy 发表于 2012-9-26 15:36
硬件SPI操作,可做硬件重启这点都差不多吧?我记得很多NOKIA 5110都是SPI控制的,沙发,顶你一个!


现有的5110库都未用spi.h,都是软件编程用任意io模拟spi协议的
回复 支持 反对

使用道具 举报

发表于 2012-9-26 16:37:09 | 显示全部楼层
test01 发表于 2012-9-26 16:20
现有的5110库都未用spi.h,都是软件编程用任意io模拟spi协议的

是的,哈哈,是软件模拟的!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-13 02:43:50 | 显示全部楼层
为更方便大家应用,变更了开源许可证
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-20 00:27:42 | 显示全部楼层
有人反映说看不懂
好吧,我用中文解释下
这个库的公共函数有:
lcd5110_clear        清空显存
lcd5110_initialise   屏上电后要首先调用下这个,初始化
lcd5110_display    让屏显示出显存里的字
lcd5110_blank      让屏全屏空白,但不影响显存内容
lcd5110_sleep      让屏休眠
lcd5110_write_text   往屏上输出字串
lcd5110_write_blink_character  往屏上输出闪烁的单字母
lcd5110_put_pixel   往屏上画点
回复 支持 反对

使用道具 举报

发表于 2012-10-20 20:33:59 | 显示全部楼层
如何制成库文件调用,还是加到程序中。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-20 23:14:18 | 显示全部楼层
xpaul 发表于 2012-10-20 20:33
如何制成库文件调用,还是加到程序中。

可以直接复制到自己的程序里
也可以按精华区那篇做库的方法做成库调用
回复 支持 反对

使用道具 举报

发表于 2012-10-21 21:52:53 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-24 00:45:40 | 显示全部楼层
lcd5110_write_text  和 lcd5110_write_blink_character 函数的调用参数有改变
回复 支持 反对

使用道具 举报

发表于 2012-11-24 17:48:34 | 显示全部楼层
这代码长度。。。看来我是用不了,现在板子里空间寸土寸金的……
回复 支持 反对

使用道具 举报

发表于 2013-1-23 12:11:17 | 显示全部楼层
'write_bit' was not declared in this scope
找不到write_bit函数
回复 支持 反对

使用道具 举报

发表于 2013-1-23 13:16:29 | 显示全部楼层

貌似

mark一下
貌似没有太多作图的功能
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-1-24 12:43:20 | 显示全部楼层
本帖最后由 test01 于 2013-1-24 12:48 编辑
ttyp 发表于 2013-1-23 13:16
mark一下
貌似没有太多作图的功能

:lol 有基础的画点函数,更高级的画直线曲线填充可以调用画点函数编写
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-1-24 12:47:24 | 显示全部楼层
zjhyhky 发表于 2013-1-23 12:11
'write_bit' was not declared in this scope
找不到write_bit函数

这个函数在另一个帖子里
http://www.geek-workshop.com/thread-1988-1-1.html
当然你也可以用Arduino里自带的写bit宏替换
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 22:02 , Processed in 0.047152 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表