u8g_dev_ht1632.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. u8g_dev_ht1632.c
  3. 1-Bit (BW) Driver for HT1632 controller
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2013, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. U8G_PIN_NONE can be used as argument
  28. uint8_t u8g_InitSPI(u8g_t *u8g, u8g_dev_t *dev, uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset)
  29. {
  30. ...
  31. u8g->pin_list[U8G_PI_SCK] = sck;
  32. u8g->pin_list[U8G_PI_MOSI] = mosi;
  33. u8g->pin_list[U8G_PI_CS] = cs;
  34. u8g->pin_list[U8G_PI_A0] = a0;
  35. u8g->pin_list[U8G_PI_RESET] = reset;
  36. mapping
  37. #define DATA_PIN --> U8G_PI_MOSI
  38. #define WR_PIN --> U8G_PI_SCK
  39. #define CS_PIN --> U8G_PI_CS
  40. U8G_PI_A0 --> not used
  41. U8G_PI_RESET --> not used
  42. Usage:
  43. u8g_InitSPI(&u8g, &u8g_dev_ht1632_24x16, WR_PIN, DATA_IN, CS_PIN, U8G_PIN_NONE, U8G_PIN_NONE)
  44. */
  45. #include "u8g.h"
  46. #define WIDTH 24
  47. #define HEIGHT 16
  48. #define PAGE_HEIGHT 16
  49. /* http://forum.arduino.cc/index.php?topic=168537.0 */
  50. #define HT1632_CMD_SYSDIS 0x00 // CMD= 0000-0000-x Turn off oscil
  51. #define HT1632_CMD_SYSON 0x01 // CMD= 0000-0001-x Enable system oscil
  52. #define HT1632_CMD_LEDOFF 0x02 // CMD= 0000-0010-x LED duty cycle gen off
  53. #define HT1632_CMD_LEDON 0x03 // CMD= 0000-0011-x LEDs ON
  54. #define HT1632_CMD_BLOFF 0x08 // CMD= 0000-1000-x Blink OFF
  55. #define HT1632_CMD_BLON 0x09 // CMD= 0000-1001-x Blink On
  56. #define HT1632_CMD_SLVMD 0x10 // CMD= 0001-00xx-x Slave Mode
  57. #define HT1632_CMD_MSTMD 0x14 // CMD= 0001-01xx-x Master Mode
  58. #define HT1632_CMD_RCCLK 0x18 // CMD= 0001-10xx-x Use on-chip clock
  59. #define HT1632_CMD_EXTCLK 0x1C // CMD= 0001-11xx-x Use external clock
  60. #define HT1632_CMD_COMS00 0x20 // CMD= 0010-ABxx-x commons options
  61. #define HT1632_CMD_COMS01 0x24 // CMD= 0010-ABxx-x commons options
  62. #define HT1632_CMD_COMS10 0x28 // CMD= 0010-ABxx-x commons options
  63. #define HT1632_CMD_COMS11 0x2C // P-MOS OUTPUT AND 16COMMON OPTION
  64. #define HT1632_CMD_PWM 0xA0 // CMD= 101x-PPPP-x PWM duty cycle
  65. #define HT1632_ID_CMD 4 /* ID = 100 - Commands */
  66. #define HT1632_ID_RD 6 /* ID = 110 - Read RAM */
  67. #define HT1632_ID_WR 5 /* ID = 101 - Write RAM */
  68. #define HT1632_ID_LEN 3 // IDs are 3 bits
  69. #define HT1632_CMD_LEN 8 // CMDs are 8 bits
  70. #define HT1632_DATA_LEN 8 // Data are 4*2 bits
  71. #define HT1632_ADDR_LEN 7 // Address are 7 bits
  72. #if defined(ARDUINO)
  73. #if ARDUINO < 100
  74. #include <WProgram.h>
  75. #else
  76. #include <Arduino.h>
  77. #endif
  78. //#define WR_PIN 3
  79. //#define DATA_PIN 2
  80. //#define CS_PIN 4
  81. void ht1632_write_data_MSB(u8g_t *u8g, uint8_t cnt, uint8_t data, uint8_t extra)
  82. {
  83. int8_t i;
  84. uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
  85. uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
  86. for(i = cnt - 1; i >= 0; i--)
  87. {
  88. if ((data >> i) & 1)
  89. {
  90. digitalWrite(data_pin, HIGH);
  91. }
  92. else
  93. {
  94. digitalWrite(data_pin, LOW);
  95. }
  96. digitalWrite(wr_pin, LOW);
  97. u8g_MicroDelay();
  98. digitalWrite(wr_pin, HIGH);
  99. u8g_MicroDelay();
  100. }
  101. // Send an extra bit
  102. if (extra)
  103. {
  104. digitalWrite(data_pin, HIGH);
  105. digitalWrite(wr_pin, LOW);
  106. u8g_MicroDelay();
  107. digitalWrite(wr_pin, HIGH);
  108. u8g_MicroDelay();
  109. }
  110. }
  111. void ht1632_write_data(u8g_t *u8g, uint8_t cnt, uint8_t data)
  112. {
  113. uint8_t i;
  114. uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
  115. uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
  116. for (i = 0; i < cnt; i++)
  117. {
  118. if ((data >> i) & 1) {
  119. digitalWrite(data_pin, HIGH);
  120. }
  121. else {
  122. digitalWrite(data_pin, LOW);
  123. }
  124. digitalWrite(wr_pin, LOW);
  125. u8g_MicroDelay();
  126. digitalWrite(wr_pin, HIGH);
  127. u8g_MicroDelay();
  128. }
  129. }
  130. void ht1632_init(u8g_t *u8g)
  131. {
  132. //uint8_t i;
  133. uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
  134. uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
  135. uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
  136. pinMode(data_pin, OUTPUT);
  137. pinMode(wr_pin, OUTPUT);
  138. pinMode(cs_pin, OUTPUT);
  139. digitalWrite(data_pin, HIGH);
  140. digitalWrite(wr_pin, HIGH);
  141. digitalWrite(cs_pin, HIGH);
  142. digitalWrite(cs_pin, LOW);
  143. /* init display once after startup */
  144. ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false); // IDs are 3 bits
  145. ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSDIS, true); // 8 bits
  146. ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSON, true); // 8 bits
  147. ht1632_write_data_MSB(u8g, 8, HT1632_CMD_COMS11, true); // 8 bits
  148. ht1632_write_data_MSB(u8g, 8, HT1632_CMD_LEDON, true); // 8 bits
  149. ht1632_write_data_MSB(u8g, 8, HT1632_CMD_BLOFF, true); // 8 bits
  150. ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM+15, true); // 8 bits
  151. digitalWrite(cs_pin, HIGH);
  152. /* removed following (debug) code */
  153. /*
  154. digitalWrite(cs_pin, LOW);
  155. ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command
  156. ht1632_write_data_MSB(u8g, 7, 0, false);
  157. for(i = 0; i<48; ++i)
  158. {
  159. ht1632_write_data(u8g, 8, 0xFF);
  160. }
  161. digitalWrite(cs_pin, HIGH);
  162. */
  163. }
  164. /*
  165. page: 0=data contain lines 0..16, 1=data contain lines 16..32 (a 24x16 display will only have page 0)
  166. cnt: width of the display
  167. data: pointer to a buffer with 2*cnt bytes.
  168. */
  169. void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data)
  170. {
  171. uint8_t addr;
  172. uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
  173. /* send data to the ht1632 */
  174. digitalWrite(cs_pin, LOW);
  175. ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command
  176. ht1632_write_data_MSB(u8g, 7, page*2*cnt, false);
  177. // Operating in progressive addressing mode
  178. for (addr = 0; addr < cnt; addr++)
  179. {
  180. ht1632_write_data(u8g, 8, data[addr]);
  181. ht1632_write_data(u8g, 8, data[addr+cnt]);
  182. }
  183. digitalWrite(cs_pin, HIGH);
  184. }
  185. /* value is between 0...15 */
  186. void ht1632_set_contrast(u8g_t *u8g, uint8_t value)
  187. {
  188. uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
  189. digitalWrite(cs_pin, LOW);
  190. ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false);
  191. ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM + value, false);
  192. digitalWrite(cs_pin, HIGH);
  193. }
  194. #else
  195. void ht1632_init(u8g_t *u8g)
  196. {
  197. }
  198. void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data)
  199. {
  200. }
  201. void ht1632_set_contrast(u8g_t *u8g, uint8_t value)
  202. {
  203. }
  204. #endif /* ARDUINO */
  205. uint8_t u8g_dev_ht1632_24x16_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  206. {
  207. switch(msg)
  208. {
  209. case U8G_DEV_MSG_INIT:
  210. ht1632_init(u8g);
  211. break;
  212. case U8G_DEV_MSG_STOP:
  213. break;
  214. case U8G_DEV_MSG_PAGE_NEXT:
  215. {
  216. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  217. /* current page: pb->p.page */
  218. /* ptr to the buffer: pb->buf */
  219. ht1632_transfer_data(u8g, pb->p.page, WIDTH, pb->buf);
  220. }
  221. break;
  222. case U8G_DEV_MSG_CONTRAST:
  223. /* values passed to SetContrast() are between 0 and 255, scale down to 0...15 */
  224. ht1632_set_contrast(u8g, (*(uint8_t *)arg) >> 4);
  225. return 1;
  226. }
  227. return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
  228. }
  229. uint8_t u8g_dev_ht1632_24x16_buf[WIDTH*2] U8G_NOCOMMON ;
  230. u8g_pb_t u8g_dev_ht1632_24x16_pb = { {16, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ht1632_24x16_buf};
  231. u8g_dev_t u8g_dev_ht1632_24x16 = { u8g_dev_ht1632_24x16_fn, &u8g_dev_ht1632_24x16_pb, u8g_com_null_fn };