ST7579.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /***************************************************************************//**
  2. * @file ST7579.c
  3. * @brief Implementation of ST7579 Driver.
  4. * @author DBogdan (dragos.bogdan@analog.com)
  5. ********************************************************************************
  6. * Copyright 2012(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. * SVN Revision: 381
  41. *******************************************************************************/
  42. /******************************************************************************/
  43. /* Include Files */
  44. /******************************************************************************/
  45. #include "Communication.h" // Communication definitions.
  46. #include "ST7579.h" // ST7579 definitions.
  47. /******************************************************************************/
  48. /* Private variables */
  49. /******************************************************************************/
  50. const unsigned char st7579Font[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. * @brief Transmits 8 bits to ST7579 controller.
  150. *
  151. * @param data - data to transmit.
  152. *
  153. * @return None.
  154. *******************************************************************************/
  155. void ST7579_WriteByte(unsigned char data)
  156. {
  157. unsigned char spiWord[2] = {0};
  158. spiWord[0] = 0x02;
  159. spiWord[1] = data;
  160. SPI_Write(spiWord,1);
  161. }
  162. /***************************************************************************//**
  163. * @brief Initializes the ST7579 controller.
  164. *
  165. * @param None.
  166. *
  167. * @return None.
  168. *******************************************************************************/
  169. unsigned char ST7579_Init(void)
  170. {
  171. unsigned char status = 0x0;
  172. status = SPI_Init(0, 1000000, 1, 0);
  173. ST7579_RS_PIN_OUT;
  174. ST7579_RS_LOW;
  175. /* Select LCD bias ratio of the voltage required for driving the LCD. */
  176. ST7579_WriteByte(0x21);
  177. ST7579_WriteByte(0x16);
  178. /* Select Booster efficiency and Booster stage. */
  179. ST7579_WriteByte(0x23);
  180. ST7579_WriteByte(0x99);
  181. /* Set V0 Range. */
  182. ST7579_WriteByte(0x20);
  183. ST7579_WriteByte(0x04);
  184. /* 50 ms delay is recommended. */
  185. /* Set the frame frequency */
  186. ST7579_WriteByte(0x23);
  187. ST7579_WriteByte(0x0C);
  188. /* Set V0. */
  189. ST7579_WriteByte(0x21);
  190. ST7579_WriteByte(0xE7);
  191. /* Select the display mode */
  192. ST7579_WriteByte(0x20);
  193. ST7579_WriteByte(0x0C);
  194. ST7579_RS_HIGH;
  195. /* Clear RAM. */
  196. ST7579_Clear();
  197. return(status);
  198. }
  199. /***************************************************************************//**
  200. * @brief Clears ST7579 RAM.
  201. *
  202. * @param None.
  203. *
  204. * @return None.
  205. *******************************************************************************/
  206. void ST7579_Clear(void)
  207. {
  208. unsigned short clearPosition;
  209. unsigned char yPosition = 0x0;
  210. for(clearPosition = 0;clearPosition < (64 * 96);clearPosition ++)
  211. {
  212. if(!(clearPosition % 96))
  213. {
  214. ST7579_RS_LOW;
  215. /* Set X address of RAM. */
  216. ST7579_WriteByte(0x28);
  217. ST7579_WriteByte(0x80);
  218. /* Set Y address of RAM. */
  219. ST7579_WriteByte(0x28);
  220. ST7579_WriteByte(0x40 + yPosition);
  221. yPosition ++;
  222. ST7579_RS_HIGH;
  223. }
  224. ST7579_WriteByte(0x00);
  225. }
  226. }
  227. /***************************************************************************//**
  228. * @brief Sends a character to ST7579 controller.
  229. *
  230. * @param yPosition - Y address of RAM.
  231. * @param xPosition - X address of RAM.
  232. * @param character - The character.
  233. *
  234. * @return None.
  235. *******************************************************************************/
  236. void ST7579_Char(unsigned char yPosition,
  237. unsigned char xPosition,
  238. unsigned char character)
  239. {
  240. volatile unsigned char column;
  241. ST7579_RS_LOW;
  242. /* Set X address of RAM. */
  243. ST7579_WriteByte(0x28);
  244. ST7579_WriteByte(0x80 + xPosition);
  245. /* Set Y address of RAM. */
  246. ST7579_WriteByte(0x28);
  247. ST7579_WriteByte(0x40 + yPosition);
  248. ST7579_RS_HIGH;
  249. /* Send character data. */
  250. for(column = 0;column < 5;column ++)
  251. {
  252. ST7579_WriteByte(st7579Font[character - 32][column] << 1);
  253. }
  254. ST7579_WriteByte(0x00);
  255. }
  256. /***************************************************************************//**
  257. * @brief Sends a string to ST7579 controller.
  258. *
  259. * @param yPosition - Y address of RAM.
  260. * @param xPosition - X address of RAM.
  261. * @param string - The string.
  262. *
  263. * @return None.
  264. *******************************************************************************/
  265. void ST7579_String(unsigned char yPosition,
  266. unsigned char xPosition,
  267. unsigned char* string)
  268. {
  269. while(*string)
  270. {
  271. /* Send each character of the string. */
  272. ST7579_Char(yPosition, xPosition, *string++);
  273. xPosition += 6;
  274. }
  275. }
  276. /***************************************************************************//**
  277. * @brief Sends an integer number to ST7579 controller.
  278. *
  279. * @param yPosition - Y address of RAM.
  280. * @param xPosition - X address of RAM.
  281. * @param number - The number.
  282. *
  283. * @return None.
  284. *******************************************************************************/
  285. void ST7579_Number(unsigned char yPosition,
  286. unsigned char xPosition,
  287. unsigned int number)
  288. {
  289. char position = 6;
  290. while(position--)
  291. {
  292. ST7579_Char(yPosition, xPosition, (number % 10) + 0x30);
  293. xPosition -= 6;
  294. number = number / 10;
  295. }
  296. }
  297. /***************************************************************************//**
  298. * @brief Sends an integer number in Hexa format to ST7579 controller.
  299. *
  300. * @param yPosition - Y address of RAM.
  301. * @param xPosition - X address of RAM.
  302. * @param number - The number.
  303. * @param bytesNumber - Number of bytes.
  304. *
  305. * @return None.
  306. *******************************************************************************/
  307. void ST7579_HexNumber(unsigned char yPosition,
  308. unsigned char xPosition,
  309. unsigned int number,
  310. unsigned char bytesNumber)
  311. {
  312. char position = bytesNumber * 2;
  313. char character = 0;
  314. while(position--)
  315. {
  316. character = (number % 16);
  317. if (character <= 9)
  318. {
  319. character += 0x30;
  320. }
  321. else
  322. {
  323. character +=0x37;
  324. }
  325. ST7579_Char(yPosition, xPosition, character);
  326. xPosition -= 6;
  327. number = number / 16;
  328. }
  329. }
  330. /***************************************************************************//**
  331. * @brief Sends an image array to ST7579 controller.
  332. *
  333. * @param yPosition - Y address of RAM.
  334. * @param xPosition - X address of RAM.
  335. * @param image - The image array.
  336. * @param width - The width of the image (pixels).
  337. * @param height - The height of the image (pixels).
  338. *
  339. * @return None.
  340. *******************************************************************************/
  341. void ST7579_Image(unsigned char yPosition,
  342. unsigned char xPosition,
  343. const unsigned char image[],
  344. unsigned char width,
  345. unsigned char height)
  346. {
  347. unsigned short index;
  348. for(index = 0;index < (width * height / 8);index ++)
  349. {
  350. if(!(index % width))
  351. {
  352. ST7579_RS_LOW;
  353. /* Set X address of RAM. */
  354. ST7579_WriteByte(0x28);
  355. ST7579_WriteByte(0x80 + xPosition);
  356. /* Set Y address of RAM. */
  357. ST7579_WriteByte(0x28);
  358. ST7579_WriteByte(0x40 + yPosition);
  359. yPosition ++;
  360. ST7579_RS_HIGH;
  361. }
  362. ST7579_WriteByte(image[index]);
  363. }
  364. }