nokia_5110.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "nokia_5110.h"
  2. #include "english_6x8_pixel.h"
  3. #include "write_chinese_string_pixel.h"
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6. #include "delay.h"
  7. #include "SPI-SD/Lcd/font.h"
  8. #include "SPI-SD/Fatfs/ff.h"
  9. char n5110_string[6][14]; //6行,每行13byte
  10. /*-----------------------------------------------------------------------
  11. LCD_init : 3310LCD初始化
  12. 编写日期 :2004-8-10
  13. 最后修改日期 :2004-8-10
  14. -----------------------------------------------------------------------*/
  15. void LCD_init ( void )
  16. {
  17. #if LCD_def
  18. // 产生一个让LCD复位的低电平脉冲
  19. LCD_RST = 0;
  20. delay_us ( 1 );
  21. LCD_RST = 1;
  22. // 关闭LCD
  23. LCD_CE = 0;
  24. delay_us ( 1 );
  25. // 使能LCD
  26. LCD_CE = 1;
  27. delay_us ( 1 );
  28. LCD_write_byte ( 0x21, 0 ); // 使用扩展命令设置LCD模式
  29. LCD_write_byte ( 0xc8, 0 ); // 设置偏置电压
  30. LCD_write_byte ( 0x06, 0 ); // 温度校正
  31. LCD_write_byte ( 0x13, 0 ); // 1:48
  32. LCD_write_byte ( 0x20, 0 ); // 使用基本命令
  33. LCD_clear(); // 清屏
  34. LCD_write_byte ( 0x0c, 0 ); // 设定显示模式,正常显示
  35. // 关闭LCD
  36. LCD_CE = 0;
  37. //开启背光灯
  38. BACKLIGHT = 1;
  39. #endif
  40. }
  41. /*-----------------------------------------------------------------------
  42. LCD_clear : LCD清屏函数
  43. 编写日期 :2004-8-10
  44. 最后修改日期 :2004-8-10
  45. -----------------------------------------------------------------------*/
  46. void LCD_clear ( void )
  47. {
  48. unsigned int i;
  49. LCD_write_byte ( 0x0c, 0 );
  50. LCD_write_byte ( 0x80, 0 );
  51. for ( i = 0; i < 504; i++ )
  52. LCD_write_byte ( 0, 1 );
  53. }
  54. /*-----------------------------------------------------------------------
  55. LCD_set_XY : 设置LCD坐标函数
  56. 输入参数:X :0-83
  57. Y :0-5
  58. 编写日期 :2004-8-10
  59. 最后修改日期 :2004-8-10
  60. -----------------------------------------------------------------------*/
  61. void LCD_set_XY ( unsigned char X, unsigned char Y )
  62. {
  63. LCD_write_byte ( 0x40 | Y, 0 ); // column
  64. LCD_write_byte ( 0x80 | X, 0 ); // row
  65. }
  66. /*-----------------------------------------------------------------------
  67. LCD_write_char : 显示英文字符
  68. 输入参数:c :显示的字符;
  69. 编写日期 :2004-8-10
  70. 最后修改日期 :2004-8-10
  71. -----------------------------------------------------------------------*/
  72. void LCD_write_char ( unsigned char c )
  73. {
  74. unsigned char line;
  75. c -= 32;
  76. for ( line = 0; line < 6; line++ )
  77. LCD_write_byte ( font6x8[c][line], 1 );
  78. }
  79. /*-----------------------------------------------------------------------
  80. LCD_write_english_String : 英文字符串显示函数
  81. 输入参数:*s :英文字符串指针;
  82. X、Y : 显示字符串的位置,x 0-83 ,y 0-5
  83. 编写日期 :2004-8-10
  84. 最后修改日期 :2004-8-10
  85. -----------------------------------------------------------------------*/
  86. void LCD_write_english_string ( unsigned char X, unsigned char Y, char *s )
  87. {
  88. #if LCD_def
  89. LCD_set_XY ( X, Y );
  90. while ( *s )
  91. {
  92. LCD_write_char ( *s );
  93. s++;
  94. }
  95. #endif
  96. }
  97. /*-----------------------------------------------------------------------
  98. LCD_write_chinese_string: 在LCD上显示汉字
  99. 输入参数:X、Y :显示汉字的起始X、Y坐标;
  100. ch_with :汉字点阵的宽度
  101. num :显示汉字的个数;
  102. line :汉字点阵数组中的起始行数
  103. row :汉字显示的行间距
  104. 编写日期 :2004-8-11
  105. 最后修改日期 :2004-8-12
  106. 测试:
  107. LCD_write_chi(0,0,12,7,0,0);
  108. LCD_write_chi(0,2,12,7,0,0);
  109. LCD_write_chi(0,4,12,7,0,0);
  110. -----------------------------------------------------------------------*/
  111. void LCD_write_chinese_string ( unsigned char X, unsigned char Y,
  112. unsigned char ch_with, unsigned char num,
  113. unsigned char line, unsigned char row )
  114. {
  115. unsigned char i, n;
  116. LCD_set_XY ( X, Y ); //设置初始位置
  117. for ( i = 0; i < num; )
  118. {
  119. for ( n = 0; n < ch_with * 2; n++ ) //写一个汉字
  120. {
  121. if ( n == ch_with ) //写汉字的下半部分
  122. {
  123. if ( i == 0 ) LCD_set_XY ( X, Y + 1 );
  124. else
  125. LCD_set_XY ( ( X + ( ch_with + row ) *i ), Y + 1 );
  126. }
  127. LCD_write_byte ( write_chinese[line + i][n], 1 );
  128. }
  129. i++;
  130. LCD_set_XY ( ( X + ( ch_with + row ) *i ), Y );
  131. }
  132. }
  133. /*-----------------------------------------------------------------------
  134. LCD_draw_map : 位图绘制函数
  135. 输入参数:X、Y :位图绘制的起始X、Y坐标;
  136. *map :位图点阵数据;
  137. Pix_x :位图像素(长)
  138. Pix_y :位图像素(宽)
  139. 编写日期 :2004-8-13
  140. 最后修改日期 :2004-8-13
  141. -----------------------------------------------------------------------*/
  142. void LCD_draw_bmp_pixel ( unsigned char X, unsigned char Y, unsigned char *map,
  143. unsigned char Pix_x, unsigned char Pix_y )
  144. {
  145. unsigned int i, n;
  146. unsigned char row;
  147. if ( Pix_y % 8 == 0 ) row = Pix_y / 8; //计算位图所占行数
  148. else
  149. row = Pix_y / 8 + 1;
  150. for ( n = 0; n < row; n++ )
  151. {
  152. LCD_set_XY ( X, Y );
  153. for ( i = 0; i < Pix_x; i++ )
  154. {
  155. LCD_write_byte ( map[i + n * Pix_x], 1 );
  156. }
  157. Y++; //换行
  158. }
  159. }
  160. /*-----------------------------------------------------------------------
  161. LCD_write_byte : 使用SPI接口写数据到LCD
  162. 输入参数:data :写入的数据;
  163. command :写数据/命令选择;
  164. 编写日期 :2004-8-10
  165. 最后修改日期 :2004-8-13
  166. -----------------------------------------------------------------------*/
  167. void LCD_write_byte ( unsigned char dat, unsigned char command )
  168. {
  169. unsigned char i;
  170. //PORTB &= ~LCD_CE ; // 使能LCD
  171. LCD_CE = 0;
  172. if ( command == 0 )
  173. // PORTB &= ~LCD_DC ; // 传送命令
  174. LCD_DC = 0;
  175. else
  176. // PORTB |= LCD_DC ; // 传送数据
  177. LCD_DC = 1;
  178. for ( i = 0; i < 8; i++ )
  179. {
  180. if ( dat & 0x80 )
  181. SDIN = 1;
  182. else
  183. SDIN = 0;
  184. SCLK = 0;
  185. dat = dat << 1;
  186. SCLK = 1;
  187. }
  188. // SPDR = data; // 传送数据到SPI寄存器
  189. //while ((SPSR & 0x80) == 0); // 等待数据传送完毕
  190. //PORTB |= LCD_CE ; // 关闭LCD
  191. LCD_CE = 1;
  192. }
  193. void RefreshLCD ( void )
  194. {
  195. #if LCD_def
  196. u8 i;
  197. for ( i = 0; i < 6; i++ )
  198. {
  199. LCD_write_english_string ( 0, i, n5110_string[i] );
  200. }
  201. #endif
  202. }
  203. #define LCD_Clear(c) LCD_clear()
  204. /*********************************************************************************
  205. 名称:void LCD_ShowCharString(uint16_t x, uint16_t y, const uint8_t *p, uint16_t PenColor, uint16_t BackColor)
  206. 参数:x,y 起始坐标
  207. p 指向字符串起始地址
  208. PenColor 字符颜色
  209. BackColor 背景颜色
  210. 功能:
  211. 备注:用16字体,可以调节 此函数不能单独调用
  212. *********************************************************************************/
  213. void LCD_ShowCharString(uint16_t x, uint16_t y, const uint8_t *p, uint16_t PenColor, uint16_t BackColor)
  214. {
  215. uint8_t size = 16; /*---字符大小默认16*8---*/
  216. if(x>MAX_CHAR_POSX){x=0;y+=size;} /*超出X轴字体最小单位,换行*/
  217. if(y>MAX_CHAR_POSY){y=x=0;LCD_Clear(WHITE);} /*超出Y轴字体最小单位,回到原点,并且清屏*/
  218. //LCD_ShowChar(x, y, *p, size, PenColor, BackColor); /*0表示非叠加方式*/
  219. LCD_set_XY ( x, y );
  220. LCD_write_char(*p);
  221. }
  222. /*********************************************************************************
  223. *名称: u16 findHzIndex(u8 *hz)
  224. *参数:hz
  225. *功能:索引汉字存储的内存地址
  226. *备注:
  227. *********************************************************************************/
  228. u16 findHzIndex(u8 *hz) /* 在自定义汉字库在查找所要显示 */
  229. /* 的汉字的位置 */
  230. {
  231. u16 i=0;
  232. FNT_GB16 *ptGb16 = (FNT_GB16 *)GBHZ_16; /*ptGb16指向GBHZ_16*/
  233. while(ptGb16[i].Index[0] > 0x80)
  234. {
  235. if ((*hz == ptGb16[i].Index[0]) && (*(hz+1) == ptGb16[i].Index[1])) /*汉字用两位来表示地址码*/
  236. {
  237. return i;
  238. }
  239. i++;
  240. if(i > (sizeof((FNT_GB16 *)GBHZ_16) / sizeof(FNT_GB16) - 1)) /* 搜索下标约束 */
  241. {
  242. break;
  243. }
  244. }
  245. return 0;
  246. }
  247. #if 0
  248. /*********************************************************************************
  249. *名称:void WriteOneHz(uint16_t x0, uint16_t y0, uint8_t *pucMsk, uint16_t PenColor, uint16_t BackColor)
  250. *参数:x0,y0 起始坐标
  251. *pucMsk 指向
  252. PenColor 字符颜色
  253. BackColor 背景颜色
  254. *功能:
  255. *备注:此函数不能单独作为汉字字符显示
  256. *********************************************************************************/
  257. void WriteOneHz(u16 x0, u16 y0, u8 *pucMsk, u16 PenColor, u16 BackColor)
  258. {
  259. u16 i,j;
  260. u16 mod[16]; /* 当前字模 16*16 */
  261. u16 *pusMsk; /* 当前字库地址 */
  262. u16 y;
  263. u16 size = 16; /*汉字默认大小16*16*/
  264. pusMsk = (u16 *)pucMsk;
  265. for(i=0; i<16; i++) /* 保存当前汉字点阵式字模 */
  266. {
  267. mod[i] = *pusMsk; /* 取得当前字模,半字对齐访问 */
  268. mod[i] = ((mod[i] & 0xff00) >> 8) | ((mod[i] & 0x00ff) << 8);/* 字模交换高低字节*/
  269. pusMsk = pusMsk+1;
  270. }
  271. y = y0;
  272. LCD_WindowMax(x0,y0,x0+size,y0+size); /*设置窗口*/
  273. LCD_SetCursor(x0,y0); /*设置光标位置 */
  274. LCD_WriteRAM_Prepare(); /*开始写入GRAM*/
  275. for(i=0; i<16; i++) /* 16行 */
  276. {
  277. for(j=0; j<16; j++) /* 16列 */
  278. {
  279. if((mod[i] << j) & 0x8000) /* 显示第i行 共16个点 */
  280. {
  281. LCD_WriteRAM(PenColor);
  282. }
  283. else
  284. {
  285. LCD_WriteRAM(BackColor); /* 用读方式跳过写空白点的像素*/
  286. }
  287. }
  288. y++;
  289. }
  290. LCD_WindowMax(0x0000,0x0000,240,320); /*恢复窗体大小*/
  291. }
  292. #endif
  293. /*********************************************************************************
  294. *名称:void LCD_ShowHzString(u16 x0, u16 y0, u8 *pcStr, u16 PenColor, u16 BackColor)
  295. *参数:x0,y0 起始坐标
  296. pcStr 指向
  297. PenColor 字体颜色
  298. BackColor 字体背景
  299. *功能:显示汉字字符串
  300. *备注:这个函数不能单独调用
  301. *********************************************************************************/
  302. void LCD_ShowHzString(u16 x0, u16 y0, u8 *pcStr, u16 PenColor, u16 BackColor)
  303. {
  304. u16 usIndex;
  305. u8 size = 16;
  306. FNT_GB16 *ptGb16 = 0;
  307. ptGb16 = (FNT_GB16 *)GBHZ_16;
  308. if(x0>MAX_HZ_POSX){x0=0;y0+=size;} /*超出X轴字体最小单位,换行*/
  309. if(y0>MAX_HZ_POSY){y0=x0=0;LCD_Clear(WHITE);} /*超出Y轴字体最小单位,回到原点,并且清屏*/
  310. usIndex = findHzIndex(pcStr);
  311. //WriteOneHz(x0, y0, (u8 *)&(ptGb16[usIndex].Msk[0]), PenColor, BackColor); /* 显示字符 */
  312. LCD_draw_bmp_pixel(x0, y0, (u8 *)&(ptGb16[usIndex].Msk[0]),16, 16 );
  313. }
  314. /*********************************************************************************
  315. *名称:void LCD_ShowString(u16 x0, u16 y0, u8 *pcstr, u16 PenColor, u16 BackColor)
  316. *参数:x0 y0 起始坐标
  317. pcstr 字符串指针
  318. PenColor 字体颜色
  319. BackColor 字体背景色
  320. *功能:调用字符和汉字显示函数,实现字符串显示
  321. *备注:
  322. *********************************************************************************/
  323. void LCD_ShowString(u16 x0, u16 y0, u8 *pcStr, u16 PenColor, u16 BackColor)
  324. {
  325. while(*pcStr!='\0')
  326. {
  327. if (x0>MAX_HZ_POSX) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  328. {
  329. y0+=16;
  330. x0=0;
  331. }
  332. if (y0>MAX_HZ_POSY) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  333. {
  334. y0=0;
  335. }
  336. if(*pcStr>0x80) /*显示汉字*/
  337. {
  338. LCD_ShowHzString(x0, y0, pcStr, PenColor, BackColor);
  339. pcStr += 2;
  340. x0 += 16;
  341. }
  342. else /*显示字符*/
  343. {
  344. LCD_ShowCharString(x0, y0, pcStr, PenColor, BackColor);
  345. pcStr +=1;
  346. x0+= 8;
  347. }
  348. }
  349. }
  350. /*--------------下面三个函数 实现字库的调用及显示-------------------*/
  351. FATFS fs;
  352. FIL fsrc; /* file objects */
  353. UINT br; // File R/W count
  354. static uint8_t Find_Chinese(uint8_t* str, uint8_t* p)
  355. {
  356. uint8_t High8bit,Low8bit;
  357. FRESULT res;
  358. uint8_t error[]="Please put the sys folder which include the font file HZK16.bin into the SD card root directory. ";
  359. High8bit=*str; /* 高8位数据 */
  360. Low8bit=*(str+1); /* 低8位数据 */
  361. f_mount(0, &fs);
  362. /* Infinite loop */
  363. res = f_open(&fsrc, "/sys/HZK16.bin", FA_OPEN_EXISTING | FA_READ); /* 在SD卡里搜索HZK16.bin*/
  364. if(res != 0)
  365. {
  366. LCD_ShowString(32,100,error,RED ,BLACK );
  367. return 1;
  368. }
  369. f_lseek(&fsrc,32*((High8bit-0xa0-1)*94+(Low8bit-0xa0-1)));
  370. f_read(&fsrc, p, 32, &br);
  371. f_close(&fsrc);
  372. f_mount(0, NULL);
  373. return 0;
  374. }
  375. /*********************************************************************************
  376. * 名 称:void PutChinese(u16 x, u16 y, u8 *c, u32 PenColor, u32 BackColor)
  377. * 入口参数:u16 x,u16 y 字符坐标位置
  378. u8 *c 汉字的指针
  379. u32 PenColor画笔颜色
  380. u32 BackColor背景颜色
  381. * 出口参数:无
  382. * 功 能:从SPI_FLASH里查找汉字基地址,
  383. 并将32字节的点阵数据送到LCD显示
  384. * 说 明:只能被GUI_Put调用
  385. *********************************************************************************/
  386. u8 buffer_r[32];
  387. u8 *c_r;
  388. void PutChinese(u16 x, u16 y, u8 *c, u32 PenColor, u32 BackColor)
  389. {
  390. u16 i=0;
  391. u16 j=0;
  392. u8 buffer[32];
  393. u16 tmp_char=0;
  394. if(!Find_Chinese(c,buffer))
  395. {
  396. c_r = c;
  397. memcpy(buffer_r,buffer,32);
  398. LCD_draw_bmp_pixel(x,y,(u8 *)buffer,16,16);
  399. #if 0
  400. LCD_WindowMax(x,y,x+16,y+16); /*设置窗口*/
  401. LCD_SetCursor(x,y); /*设置光标位置 */
  402. LCD_WriteRAM_Prepare();
  403. /* 16*16汉字点阵,共有16行 每行16个点,存储的时候是以字节为单位
  404. 需合并成字*/
  405. for (i=0;i<16;i++)
  406. {
  407. tmp_char=buffer[i*2];
  408. tmp_char=(tmp_char<<8);
  409. tmp_char|=buffer[2*i+1]; /*现在tmp_char存储着一行的点阵数据*/
  410. for (j=0;j<16;j++)
  411. {
  412. if((tmp_char<<j) & 0x8000)
  413. LCD_WriteRAM(PenColor);
  414. else
  415. LCD_WriteRAM(BackColor);
  416. }
  417. }
  418. LCD_WindowMax(0x00,0x00,240,320); /*恢复窗体大小*/
  419. #endif
  420. }
  421. }
  422. /*********************************************************************************
  423. * 名 称:void GUI_Put(u16 x, u16 y, u8 *str,u32 PenColor, u32 BackColor)
  424. * 入口参数:u16 x,u16 y 字符坐标位置
  425. u8 *c 汉字的指针
  426. u32 PenColor画笔颜色
  427. u32 BackColor背景颜色
  428. * 出口参数:无
  429. * 功 能:计算汉字显示坐标
  430. * 说 明:
  431. *********************************************************************************/
  432. void GUI_Put(u16 x, u16 y, u8 *str,u32 PenColor, u32 BackColor)
  433. {
  434. do
  435. {
  436. if (x>MAX_HZ_POSX) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  437. {
  438. y+=16;
  439. x=0;
  440. }
  441. if (y>MAX_HZ_POSY) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  442. {
  443. y=0;
  444. }
  445. if ( *str < 0x80) /* 显示字符,字符点阵直接从font.h里调用,大小为16*8 */
  446. {
  447. LCD_ShowCharString(x, y, str, PenColor, BackColor);
  448. x+= 8;
  449. str++;
  450. }
  451. else /*大于等于0x80判断为汉字字符*/
  452. {
  453. PutChinese(x, y, str++, PenColor, BackColor);
  454. str++;
  455. x+=16;
  456. }
  457. }while(*str!=0);
  458. }