n3310.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "n3310.h"
  2. /*--------------------------------------------------------------------------------------------------
  3. Private function prototypes
  4. --------------------------------------------------------------------------------------------------*/
  5. // Function prototypes are mandatory otherwise the compiler generates unreliable code.
  6. const uint8_t FontLookup [] =
  7. {
  8. 0x00, 0x00, 0x00, 0x00, 0x00 , // sp
  9. 0x00, 0x00, 0x2f, 0x00, 0x00 , // !
  10. 0x00, 0x07, 0x00, 0x07, 0x00 , // "
  11. 0x14, 0x7f, 0x14, 0x7f, 0x14 , // #
  12. 0x24, 0x2a, 0x7f, 0x2a, 0x12 , // $
  13. 0xc4, 0xc8, 0x10, 0x26, 0x46 , // %
  14. 0x36, 0x49, 0x55, 0x22, 0x50 , // &
  15. 0x00, 0x05, 0x03, 0x00, 0x00 , // '
  16. 0x00, 0x1c, 0x22, 0x41, 0x00 , // (
  17. 0x00, 0x41, 0x22, 0x1c, 0x00 , // )
  18. 0x14, 0x08, 0x3E, 0x08, 0x14 , // *
  19. 0x08, 0x08, 0x3E, 0x08, 0x08 , // +
  20. 0x00, 0x00, 0x50, 0x30, 0x00 , // ,
  21. 0x10, 0x10, 0x10, 0x10, 0x10 , // -
  22. 0x00, 0x60, 0x60, 0x00, 0x00 , // .
  23. 0x20, 0x10, 0x08, 0x04, 0x02 , // /
  24. 0x3E, 0x51, 0x49, 0x45, 0x3E , // 0
  25. 0x00, 0x42, 0x7F, 0x40, 0x00 , // 1
  26. 0x42, 0x61, 0x51, 0x49, 0x46 , // 2
  27. 0x21, 0x41, 0x45, 0x4B, 0x31 , // 3
  28. 0x18, 0x14, 0x12, 0x7F, 0x10 , // 4
  29. 0x27, 0x45, 0x45, 0x45, 0x39 , // 5
  30. 0x3C, 0x4A, 0x49, 0x49, 0x30 , // 6
  31. 0x01, 0x71, 0x09, 0x05, 0x03 , // 7
  32. 0x36, 0x49, 0x49, 0x49, 0x36 , // 8
  33. 0x06, 0x49, 0x49, 0x29, 0x1E , // 9
  34. 0x00, 0x36, 0x36, 0x00, 0x00 , // :
  35. 0x00, 0x56, 0x36, 0x00, 0x00 , // ;
  36. 0x08, 0x14, 0x22, 0x41, 0x00 , // <
  37. 0x14, 0x14, 0x14, 0x14, 0x14 , // =
  38. 0x00, 0x41, 0x22, 0x14, 0x08 , // >
  39. 0x02, 0x01, 0x51, 0x09, 0x06 , // ?
  40. 0x32, 0x49, 0x59, 0x51, 0x3E , // @
  41. 0x7E, 0x11, 0x11, 0x11, 0x7E , // A
  42. 0x7F, 0x49, 0x49, 0x49, 0x36 , // B
  43. 0x3E, 0x41, 0x41, 0x41, 0x22 , // C
  44. 0x7F, 0x41, 0x41, 0x22, 0x1C , // D
  45. 0x7F, 0x49, 0x49, 0x49, 0x41 , // E
  46. 0x7F, 0x09, 0x09, 0x09, 0x01 , // F
  47. 0x3E, 0x41, 0x49, 0x49, 0x7A , // G
  48. 0x7F, 0x08, 0x08, 0x08, 0x7F , // H
  49. 0x00, 0x41, 0x7F, 0x41, 0x00 , // I
  50. 0x20, 0x40, 0x41, 0x3F, 0x01 , // J
  51. 0x7F, 0x08, 0x14, 0x22, 0x41 , // K
  52. 0x7F, 0x40, 0x40, 0x40, 0x40 , // L
  53. 0x7F, 0x02, 0x0C, 0x02, 0x7F , // M
  54. 0x7F, 0x04, 0x08, 0x10, 0x7F , // N
  55. 0x3E, 0x41, 0x41, 0x41, 0x3E , // O
  56. 0x7F, 0x09, 0x09, 0x09, 0x06 , // P
  57. 0x3E, 0x41, 0x51, 0x21, 0x5E , // Q
  58. 0x7F, 0x09, 0x19, 0x29, 0x46 , // R
  59. 0x46, 0x49, 0x49, 0x49, 0x31 , // S
  60. 0x01, 0x01, 0x7F, 0x01, 0x01 , // T
  61. 0x3F, 0x40, 0x40, 0x40, 0x3F , // U
  62. 0x1F, 0x20, 0x40, 0x20, 0x1F , // V
  63. 0x3F, 0x40, 0x38, 0x40, 0x3F , // W
  64. 0x63, 0x14, 0x08, 0x14, 0x63 , // X
  65. 0x07, 0x08, 0x70, 0x08, 0x07 , // Y
  66. 0x61, 0x51, 0x49, 0x45, 0x43 , // Z
  67. 0x00, 0x7F, 0x41, 0x41, 0x00 , // [
  68. 0x55, 0x2A, 0x55, 0x2A, 0x55 , // 55
  69. 0x00, 0x41, 0x41, 0x7F, 0x00 , // ]
  70. 0x04, 0x02, 0x01, 0x02, 0x04 , // ^
  71. 0x40, 0x40, 0x40, 0x40, 0x40 , // _
  72. 0x00, 0x01, 0x02, 0x04, 0x00 , // '
  73. 0x20, 0x54, 0x54, 0x54, 0x78 , // a
  74. 0x7F, 0x48, 0x44, 0x44, 0x38 , // b
  75. 0x38, 0x44, 0x44, 0x44, 0x20 , // c
  76. 0x38, 0x44, 0x44, 0x48, 0x7F , // d
  77. 0x38, 0x54, 0x54, 0x54, 0x18 , // e
  78. 0x08, 0x7E, 0x09, 0x01, 0x02 , // f
  79. 0x0C, 0x52, 0x52, 0x52, 0x3E , // g
  80. 0x7F, 0x08, 0x04, 0x04, 0x78 , // h
  81. 0x00, 0x44, 0x7D, 0x40, 0x00 , // i
  82. 0x20, 0x40, 0x44, 0x3D, 0x00 , // j
  83. 0x7F, 0x10, 0x28, 0x44, 0x00 , // k
  84. 0x00, 0x41, 0x7F, 0x40, 0x00 , // l
  85. 0x7C, 0x04, 0x18, 0x04, 0x78 , // m
  86. 0x7C, 0x08, 0x04, 0x04, 0x78 , // n
  87. 0x38, 0x44, 0x44, 0x44, 0x38 , // o
  88. 0x7C, 0x14, 0x14, 0x14, 0x08 , // p
  89. 0x08, 0x14, 0x14, 0x18, 0x7C , // q
  90. 0x7C, 0x08, 0x04, 0x04, 0x08 , // r
  91. 0x48, 0x54, 0x54, 0x54, 0x20 , // s
  92. 0x04, 0x3F, 0x44, 0x40, 0x20 , // t
  93. 0x3C, 0x40, 0x40, 0x20, 0x7C , // u
  94. 0x1C, 0x20, 0x40, 0x20, 0x1C , // v
  95. 0x3C, 0x40, 0x30, 0x40, 0x3C , // w
  96. 0x44, 0x28, 0x10, 0x28, 0x44 , // x
  97. 0x0C, 0x50, 0x50, 0x50, 0x3C , // y
  98. 0x44, 0x64, 0x54, 0x4C, 0x44 // z
  99. };
  100. /*--------------------------------------------------------------------------------------------------
  101. Name : LcdInit
  102. Description : Performs PINS & LCD controller initialization.
  103. Argument(s) : None.
  104. Return value : None.
  105. --------------------------------------------------------------------------------------------------*/
  106. void LcdInit ( uint8_t Contrast )
  107. {
  108. RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; /* Port B Clock */
  109. GPIOB->CRH &= ~(((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_DC_PIN-8)*4) |
  110. ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_IN_PIN-8)*4) |
  111. ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_CLK_PIN-8)*4)|
  112. ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_RST_PIN-8)*4) ); // Reset all pins
  113. GPIOB->CRH |= (((GPIO_CRL_MODE0_1)<<(LCD_DC_PIN-8)*4) |
  114. ((GPIO_CRL_MODE0_1)<<(LCD_IN_PIN-8)*4) |
  115. ((GPIO_CRL_MODE0_1)<<(LCD_CLK_PIN-8)*4)|
  116. ((GPIO_CRL_MODE0_1)<<(LCD_RST_PIN-8)*4) ); // Pins in push-pull 10MHz
  117. GPIOB->BRR = 1<<LCD_RST_PIN; /* RESET to 0 */
  118. if (Contrast == 0)
  119. Contrast = 60;
  120. if (Contrast > 90)
  121. Contrast = 90;
  122. /* Some delay */
  123. {
  124. __IO uint32_t Counter;
  125. for ( Counter = 0; Counter <10; Counter++)
  126. ; /* Blank */
  127. }
  128. GPIOB->BSRR = 1<<LCD_RST_PIN; /* RESET to 1 */
  129. #define FUNCTION_SET 0x20
  130. #define FUNCTION_PD 0x04
  131. #define FUNCTION_V 0x02
  132. #define FUNCTION_EXT 0x01
  133. LcdSend( FUNCTION_SET|FUNCTION_EXT, LCD_CMD ); // LCD Extended Commands.
  134. #define SET_VOP 0x80
  135. LcdSend( SET_VOP|Contrast, LCD_CMD ); // Set LCD Vop (Contrast). 3.06+0.06*72=4.38V
  136. #define TEMPERATURE_COEFFICIENT 0x4
  137. LcdSend( TEMPERATURE_COEFFICIENT | 2, LCD_CMD ); // Set Temp coefficent. 2 (0-3)
  138. #define SET_BIAS 0x10
  139. #define BIAS_1_10 0x07
  140. #define BIAS_1_18 0x06
  141. #define BIAS_1_24 0x05
  142. #define BIAS_1_40 0x04
  143. #define BIAS_1_48 0x03
  144. #define BIAS_1_65 0x02
  145. #define BIAS_1_80 0x01
  146. #define BIAS_1_100 0x00
  147. LcdSend( SET_BIAS|BIAS_1_48, LCD_CMD ); // LCD bias mode 1:48.
  148. LcdSend( FUNCTION_SET, LCD_CMD ); // LCD Standard Commands, Horizontal addressing mode.
  149. #define DISPLAY_CONTROL 0x08
  150. #define DISPLAY_BLANK 0x00
  151. #define DISPLAY_NORMAL 0x04
  152. #define DISPLAY_ALLON 0x01
  153. #define DISPLAY_INVERSE 0x05
  154. LcdSend( DISPLAY_CONTROL|DISPLAY_NORMAL, LCD_CMD ); // LCD in normal mode.
  155. #define SET_Y 0x40 /* from 0 to 5 */
  156. #define SET_X 0x80 /* from 0 to 83 */
  157. // LcdClear();
  158. }
  159. //Clear display
  160. void LcdClear()
  161. {
  162. uint16_t j = 7;
  163. while ( j-- )
  164. {
  165. uint16_t i = 84;
  166. LcdSend(SET_Y|j, LCD_CMD );
  167. LcdSend(SET_X|0, LCD_CMD);
  168. while ( i-- )
  169. {
  170. LcdSend(0/*xff*/, LCD_DATA);
  171. }
  172. }
  173. }
  174. /*--------------------------------------------------------------------------------------------------
  175. Name : LcdContrast
  176. Description : Set display contrast.
  177. Argument(s) : contrast -> Contrast value from 0x00 to 0x7F.
  178. Return value : None.
  179. Notes : No change visible at ambient temperature.
  180. --------------------------------------------------------------------------------------------------*/
  181. void LcdContrast ( uint8_t Contrast )
  182. {
  183. // LCD Extended Commands.
  184. LcdSend( FUNCTION_SET|FUNCTION_EXT, LCD_CMD ); // LCD Extended Commands.
  185. // Set LCD Vop (Contrast).
  186. if ( Contrast > 90 ) /* Limit by 8.5 V */
  187. Contrast = 90;
  188. LcdSend( SET_VOP | Contrast, LCD_CMD );
  189. // LCD Standard Commands, horizontal addressing mode.
  190. LcdSend( FUNCTION_SET, LCD_CMD );
  191. }
  192. void LcdGotoXY(uint8_t X, uint8_t Y)
  193. {
  194. LcdSend( SET_X|X, LCD_CMD );
  195. LcdSend( SET_Y|Y, LCD_CMD );
  196. }
  197. /*--------------------------------------------------------------------------------------------------
  198. Name : LcdChr
  199. Description : Displays a character at current cursor location and increment cursor location.
  200. Argument(s) : Ctrl = X*X_MUL+y*Y_MUL+ BIG1 + BIG2 + OutLen
  201. NoInverse == 0 - inverse output
  202. Str - out string
  203. Return value : None.
  204. --------------------------------------------------------------------------------------------------*/
  205. void LcdChr ( uint32_t Ctrl, const char* Str )
  206. {
  207. unsigned char ch;
  208. uint8_t Inverse;
  209. #if defined(BIG)
  210. #define INTERNAL_BIG_DOWN
  211. uint32_t Big = Ctrl;
  212. #endif
  213. Inverse = ((Ctrl & INVERSE) != 0);
  214. LcdGotoXY((Ctrl/X_POSITION & 0x0F)*6 + ((Ctrl/X_OFFSET)& 0x3F),
  215. (Ctrl/Y_POSITION) & 0x07);
  216. Ctrl = Ctrl & 0x7F; /* Only Len */
  217. while ( Ctrl != 0 )
  218. {
  219. const uint8_t* Start;
  220. if ( (ch = *Str) != 0)
  221. Str++;
  222. Ctrl--;
  223. if ( ch == 0 )
  224. ch = ' ';
  225. if ( (ch < 0x20) || (ch > 0x7b) )
  226. {
  227. // Convert to a printable character.
  228. ch = 92;
  229. }
  230. ch = ch - 32;
  231. Start = &FontLookup[ch*4+ch];
  232. #if defined(BIG)
  233. if ( (Big & (BIG_UP|BIG_DOWN)) == 0 )
  234. #endif
  235. {
  236. int i;
  237. for (i=0; i<=4; i++)
  238. {
  239. uint8_t E = Start[i];
  240. if ( Inverse )
  241. {
  242. E = ~E;
  243. }
  244. LcdSend( E, LCD_DATA );
  245. }
  246. if ( !Inverse )
  247. LcdSend( 0, LCD_DATA );
  248. else
  249. LcdSend( 0xFF, LCD_DATA );
  250. }
  251. #if defined(BIG)
  252. else
  253. {
  254. int i;
  255. for (i=0; i<=4; i++)
  256. {
  257. uint8_t Element = 0;
  258. uint8_t E = Start[i];
  259. if ( Big & BIG_UP )
  260. {
  261. if(E & 0x01) Element |= 0x03;
  262. if(E & 0x02) Element |= 0x0C;
  263. if(E & 0x04) Element |= 0x30;
  264. if(E & 0x08) Element |= 0xC0;
  265. }
  266. else
  267. {
  268. if(E & 0x10) Element |= 0x03;
  269. if(E & 0x20) Element |= 0x0C;
  270. if(E & 0x40) Element |= 0x30;
  271. if(E & 0x80) Element |= 0xC0;
  272. }
  273. if (Inverse)
  274. Element = ~Element;
  275. LcdSend( Element, LCD_DATA );
  276. LcdSend( Element, LCD_DATA );
  277. } /* For */
  278. if ( !Inverse )
  279. {
  280. LcdSend( 0, LCD_DATA );
  281. LcdSend( 0, LCD_DATA );
  282. }
  283. else
  284. {
  285. LcdSend( 0xFF, LCD_DATA );
  286. LcdSend( 0xFF, LCD_DATA );
  287. }
  288. }
  289. #endif /* BIG */
  290. }
  291. }
  292. /*--------------------------------------------------------------------------------------------------
  293. Name : LcdSend
  294. Description : Sends data to display controller.
  295. Argument(s) : data -> Data to be sent
  296. cd -> Command or data (see/use enum)
  297. Return value : None.
  298. --------------------------------------------------------------------------------------------------*/
  299. void LcdSend ( uint8_t Data, LcdCmdData cd )
  300. {
  301. int i;
  302. if ( cd != LCD_CMD ) /* If data */
  303. {
  304. GPIOB->BSRR = 1<<LCD_DC_PIN; /* DC pin to hight */
  305. }
  306. else
  307. {
  308. GPIOB->BRR = 1<<LCD_DC_PIN; /* DC pin to low */
  309. }
  310. LCD_DELAY;
  311. i=8;
  312. do
  313. {
  314. if ( Data & 0x80 )
  315. GPIOB->BSRR = 1<<LCD_IN_PIN; /* IN pin to hight */
  316. else
  317. GPIOB->BRR = 1<<LCD_IN_PIN; /* IN pin to low */
  318. LCD_DELAY;
  319. Data = Data<<1; /* Some additional delay */
  320. GPIOB->BSRR = 1<<LCD_CLK_PIN; /* CLK pin to hight */
  321. LCD_DELAY;
  322. i--;
  323. GPIOB->BRR = 1<<LCD_CLK_PIN; /* CLK pin to low */
  324. }while(i);
  325. }
  326. /*--------------------------------------------------------------------------------------------------
  327. Character generator
  328. This table defines the standard ASCII characters in a 5x7 dot format.
  329. --------------------------------------------------------------------------------------------------*/
  330. /*--------------------------------------------------------------------------------------------------
  331. End of file.
  332. --------------------------------------------------------------------------------------------------*/
  333. #if defined(LCDDEBUG)
  334. void SystemInit()
  335. {
  336. }
  337. int main(void)
  338. {
  339. RCC->CFGR |= (RCC_CFGR_HPRE_1|RCC_CFGR_HPRE_3); /* div 8 */
  340. LcdInit();
  341. LcdClear();
  342. LcdChr ( Y_POSITION*1+X_POSITION*1+13, "Hello world" );
  343. LcdChr ( Y_POSITION*2+X_POSITION*1+13+INVERSE+X_OFFSET*3, "Hello world" );
  344. LcdChr ( Y_POSITION*4+X_POSITION*0+2+BIG_UP, "15" );
  345. LcdChr ( Y_POSITION*5+X_POSITION*0+2+BIG_DOWN, "15" );
  346. return 0;
  347. }
  348. #endif