107_32_Driver_IO16.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /****************************Copyright(c)*****************************
  2. ** http://www.openmcu.com
  3. **--------------File Info---------------------------------------------------------------------------------
  4. ** File name: 107_32_Driver_IO16.c
  5. ** Last modified Date: 2014-03-03
  6. ** Last Version: V1.10
  7. ** Descriptions:
  8. **
  9. **----------------------------------------------------------------------------------------------------
  10. ** Created by: openmcu
  11. ** Created date: 2014-03-03
  12. ** Version: V1.00
  13. ** Descriptions: 编写示例代码
  14. **
  15. **----------------------------------------------------------------------------------------------------
  16. ** Modified by: hzq
  17. ** Modified date:
  18. ** Version:
  19. ** Descriptions: 主要对3.2寸屏(SSD1298)的初始化配置,以及各种API函数的编写
  20. **
  21. *****************************************************************/
  22. #include "font.h"
  23. #include "stdio.h"
  24. #include "delay.h"
  25. #include "stdlib.h"
  26. #include "107_32_Driver_IO16.h"
  27. #include "w25q16.h"
  28. #include "ff.h"
  29. /* Private variable ---------------------------------------------------------*/
  30. uint16_t DeviceCode; //LCD的ID号变量
  31. /*********************************************************************************
  32. 函数名:void LCD_GPIO_Configuration(void)
  33. 功能:配置并初始化LCD数据和控制端口
  34. 输入参数:无
  35. 输出参数:无
  36. *********************************************************************************/
  37. void LCD_Configuration(void)
  38. {
  39. GPIO_InitTypeDef GPIO_InitStructure;
  40. /*开启相应时钟 */
  41. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
  42. |RCC_APB2Periph_GPIOE, ENABLE);
  43. /*所有LCD数据引脚配置为推挽输出*/
  44. /*GPIOE*/
  45. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  46. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  47. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  48. GPIO_Init(GPIOE, &GPIO_InitStructure);
  49. /* LCD_CS */
  50. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  51. GPIO_Init(GPIOC, &GPIO_InitStructure);
  52. /* LCD_RS LCD_WR LCD_RD*/
  53. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  54. GPIO_Init(GPIOD, &GPIO_InitStructure);
  55. }
  56. /*********************************************************************************
  57. 函数名:void LCD_WR_REG(uint16_t LCD_Reg)
  58. 功能:写入LCD的控制命令
  59. 输入参数:要写入的命令uint16_t LCD_Reg
  60. 输出参数:无
  61. **********************************************************************************/
  62. void LCD_WR_REG(uint16_t LCD_Reg)
  63. {
  64. GPIO_SetBits(GPIOD,LCD_RD_PIN); //读失能
  65. GPIO_ResetBits(GPIOD,LCD_RS_PIN); //写入的是命令
  66. GPIO_ResetBits(GPIOD,LCD_WR_PIN); //拉低写入的引脚,准备写入
  67. GPIO_ResetBits(GPIOC,LCD_CS_PIN); //拉低片选段CS
  68. GPIO_Write(LCD_DATA_GPIO, LCD_Reg);//写入命令
  69. GPIO_SetBits(GPIOC,LCD_CS_PIN); //拉高片选段CS
  70. GPIO_SetBits(GPIOD,LCD_WR_PIN); //拉高写入的引脚 ,写入命令
  71. }
  72. /*********************************************************************************
  73. 函数名:void LCD_WR_DATA(uint16_t LCD_Reg)
  74. 功能:写入LCD的数据
  75. 输入参数:要写入的数据uint16_t LCD_Data
  76. 输出参数:无
  77. *********************************************************************************/
  78. void LCD_WR_DATA(uint16_t LCD_Data)
  79. {
  80. GPIO_SetBits(GPIOD,LCD_RD_PIN); //读失能
  81. GPIO_SetBits(GPIOD,LCD_RS_PIN); //写入的是数据
  82. GPIO_ResetBits(GPIOD,LCD_WR_PIN); //拉低写入的引脚,准备写入
  83. GPIO_ResetBits(GPIOC,LCD_CS_PIN); //拉低片选段CS
  84. GPIO_Write(LCD_DATA_GPIO, LCD_Data);//写入数据
  85. GPIO_SetBits(GPIOC,LCD_CS_PIN); //拉高片选段CS
  86. GPIO_SetBits(GPIOD,LCD_WR_PIN); //拉高写入的引脚,写入数据
  87. }
  88. /*********************************************************************************
  89. 函数名:uint16_t LCD_ReadReg(uint16_t LCD_Reg)
  90. 功能:读取LCD的数据
  91. 输入参数:uint16_t LCD_Reg 要读取的寄存器
  92. 输出参数:temp 读取的数据
  93. *********************************************************************************/
  94. uint16_t LCD_ReadReg(uint16_t LCD_Reg)
  95. {
  96. uint16_t temp;
  97. GPIO_InitTypeDef GPIO_InitStructure;
  98. LCD_WR_REG(LCD_Reg);
  99. /* 设置为输入模式 */
  100. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  101. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  102. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  103. GPIO_Init(GPIOE, &GPIO_InitStructure);
  104. GPIO_ResetBits(GPIOC,LCD_CS_PIN); //拉低片选段CS
  105. GPIO_SetBits(GPIOD,LCD_RS_PIN); //读取的是数据
  106. GPIO_ResetBits(GPIOD,LCD_RD_PIN); //拉低读取的引脚,准备读取数据
  107. GPIO_SetBits(GPIOD,LCD_RD_PIN); //拉高读取的引脚,读取数据
  108. temp=GPIO_ReadInputData(GPIOE);
  109. GPIO_SetBits(GPIOC,LCD_CS_PIN); //拉高片选段CS
  110. /* 设置为输出模式 */
  111. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  112. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  113. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  114. GPIO_Init(GPIOE, &GPIO_InitStructure);
  115. return temp;
  116. }
  117. /*********************************************************************************
  118. 函数名:void LCD_WriteReg(uint16_t LCD_Reg ,uint16_t LCD_RegValue)
  119. 功能:向指定寄存器写入指定数据
  120. 输入参数:uint16_t LCD_Reg 寄存器地址;
  121. uint16_t LCD_RegValue 要写入的数据
  122. 输出参数:无
  123. *********************************************************************************/
  124. void LCD_WriteReg(uint16_t LCD_Reg ,uint16_t LCD_RegValue)
  125. {
  126. LCD_WR_REG(LCD_Reg);
  127. LCD_WR_DATA(LCD_RegValue);
  128. }
  129. /*********************************************************************************
  130. *名称:void LCD_WriteRAM_Prepare(void)
  131. *参数:无
  132. *返回:无
  133. *功能:
  134. *备注:开始写GRAM
  135. *********************************************************************************/
  136. void LCD_WriteRAM_Prepare(void)
  137. {
  138. LCD_WR_REG(0x22);
  139. }
  140. /*********************************************************************************
  141. *名称:void LCD_WriteRAM(u16 RGB_Code)
  142. *参数:u16 RGB_Code,写入颜色的数值
  143. *返回:无
  144. *功能:LCD写GRAM
  145. *备注:
  146. *********************************************************************************/
  147. void LCD_WriteRAM(u16 RGB_Code)
  148. {
  149. LCD_WR_DATA(RGB_Code); /*写十六位GRAM*/
  150. }
  151. /*********************************************************************************
  152. *名称:void LCD_SetCursor(u16 Xpos, u16 Ypos)
  153. *参数:Xpos 横坐标
  154. Ypos 纵坐标
  155. *返回:无
  156. *功能:设置光标位置
  157. *********************************************************************************/
  158. void LCD_SetCursor(u16 Xpos, u16 Ypos)
  159. {
  160. LCD_WriteReg(0x004E, Xpos);
  161. LCD_WriteReg(0X004F, Ypos);
  162. }
  163. /*********************************************************************************
  164. *名称:void_LCD_Clear(uint16_t Colour)
  165. *参数:Colour 16位颜色数据
  166. *返回:无
  167. *功能:按指定颜色刷屏
  168. *********************************************************************************/
  169. void LCD_Clear(uint16_t Color)
  170. {
  171. uint32_t index=0;
  172. LCD_SetCursor(0x00,0x0000); /*设置光标位置 */
  173. LCD_WriteRAM_Prepare(); /*开始写入GRAM */
  174. for(index=0;index<76800;index++)
  175. {
  176. LCD_WR_DATA(Color); /*写入数据 */
  177. }
  178. }
  179. /*********************************************************************************
  180. *名称:void LCD_DrawPoint(void)
  181. *参数:xsta X起始坐标 0~239
  182. ysta Y起始坐标 0~319
  183. *返回:无
  184. *功能:POINT_COLOR指定点的颜色
  185. *********************************************************************************/
  186. void LCD_DrawPoint(uint16_t xsta, uint16_t ysta)
  187. {
  188. LCD_SetCursor(xsta,ysta); /*设置光标位置 */
  189. LCD_WR_REG(0x22); /*开始写入GRAM */
  190. LCD_WR_DATA(POINT_COLOR);
  191. }
  192. /*********************************************************************************
  193. *名称:void LCD_WindowMax()
  194. *参数:
  195. *返回:无
  196. *功能:设置窗口
  197. *备注:
  198. *********************************************************************************/
  199. void LCD_WindowMax (unsigned int x,unsigned int y,unsigned int x_end,unsigned int y_end)
  200. {
  201. LCD_WriteReg(0x44,x|((x_end-1)<<8));
  202. LCD_WriteReg(0x45,y);
  203. LCD_WriteReg(0x46,y_end-1);
  204. }
  205. /*********************************************************************************
  206. *名称:void LCD_Fill(uint8_t xsta, uint16_t ysta, uint8_t xend, uint16_t yend, uint16_t colour)
  207. *参数:xsta 起始X坐标
  208. ysta 起始Y坐标
  209. xend 结束X坐标
  210. yend 结束Y坐标
  211. color 待填充颜色
  212. *返回:无
  213. *功能:在指定矩形区域填充指定颜色,区域大小(xend-xsta)*(yend-ysta)
  214. *备注:部分区域少一行像素点
  215. *********************************************************************************/
  216. void LCD_Fill(uint8_t xsta, uint16_t ysta, uint8_t xend, uint16_t yend, uint16_t colour)
  217. {
  218. u32 n;
  219. /*设置窗口 */
  220. LCD_WindowMax (xsta, ysta, xend, yend);
  221. LCD_SetCursor(xsta,ysta); /*设置光标位置 */
  222. LCD_WriteRAM_Prepare(); /*开始写入GRAM*/
  223. n=(u32)(yend-ysta+1)*(xend-xsta+1);
  224. while(n--){LCD_WR_DATA(colour);} /*显示所填充的颜色*/
  225. /*恢复窗口*/
  226. LCD_WindowMax (0, 0, 240, 320);
  227. }
  228. /*********************************************************************************
  229. *名称:void LCD_DrawLine(uint16_t xsta, uint16_t ysta, uint16_t xend, uint16_t yend)
  230. *参数:xsta X起始坐标
  231. ysta Y起始坐标
  232. xend X终点坐标
  233. yend Y终点坐标
  234. *返回:无
  235. *功能:指定坐表(两点),画线
  236. *备注:需要添加颜色参数
  237. *********************************************************************************/
  238. void LCD_DrawLine(uint16_t xsta, uint16_t ysta, uint16_t xend, uint16_t yend)
  239. {
  240. u16 x, y, t;
  241. if((xsta==xend)&&(ysta==yend))LCD_DrawPoint(xsta, ysta);
  242. else if(abs(yend-ysta)>abs(xend-xsta))/*斜率大于1 */
  243. {
  244. if(ysta>yend)
  245. {
  246. t=ysta;
  247. ysta=yend;
  248. yend=t;
  249. t=xsta;
  250. xsta=xend;
  251. xend=t;
  252. }
  253. for(y=ysta;y<yend;y++) /*以y轴为基准*/
  254. {
  255. x=(u32)(y-ysta)*(xend-xsta)/(yend-ysta)+xsta;
  256. LCD_DrawPoint(x, y);
  257. }
  258. }
  259. else /*斜率小于等于1 */
  260. {
  261. if(xsta>xend)
  262. {
  263. t=ysta;
  264. ysta=yend;
  265. yend=t;
  266. t=xsta;
  267. xsta=xend;
  268. xend=t;
  269. }
  270. for(x=xsta;x<=xend;x++) /*以x轴为基准*/
  271. {
  272. y =(u32)(x-xsta)*(yend-ysta)/(xend-xsta)+ysta;
  273. LCD_DrawPoint(x,y);
  274. }
  275. }
  276. }
  277. /*********************************************************************************
  278. *名称:void Draw_Circle(uint16_t x0, uint16_t y0, uint8_t r)
  279. *参数:x0 中心点横坐标
  280. y0 中心点纵坐标
  281. r 半径
  282. *返回:无
  283. *功能:在指定位置画一个指定大小的圆
  284. *备注:加入颜色参数,是否填充等
  285. *********************************************************************************/
  286. void Draw_Circle(uint16_t x0, uint16_t y0, uint8_t r)
  287. {
  288. int a,b;
  289. int di;
  290. a=0;b=r;
  291. di=3-(r<<1); /*判断下个点位置的标志*/
  292. while(a<=b)
  293. {
  294. LCD_DrawPoint(x0-b,y0-a); //3
  295. LCD_DrawPoint(x0+b,y0-a); //0
  296. LCD_DrawPoint(x0-a,y0+b); //1
  297. LCD_DrawPoint(x0-b,y0-a); //7
  298. LCD_DrawPoint(x0-a,y0-b); //2
  299. LCD_DrawPoint(x0+b,y0+a); //4
  300. LCD_DrawPoint(x0+a,y0-b); //5
  301. LCD_DrawPoint(x0+a,y0+b); //6
  302. LCD_DrawPoint(x0-b,y0+a);
  303. a++;
  304. /*使用Bresenham算法画圆*/
  305. if(di<0)di +=4*a+6;
  306. else
  307. {
  308. di+=10+4*(a-b);
  309. b--;
  310. }
  311. LCD_DrawPoint(x0+a,y0+b);
  312. }
  313. }
  314. /*********************************************************************************
  315. *名称:void LCD_DrawRectangle(uint16_t xsta, uint16_t ysta, uint16_t xend, uint16_t yend)
  316. *参数:xsta X起始坐标
  317. ysta Y起始坐标
  318. xend X结束坐标
  319. yend Y结束坐标
  320. *返回:无
  321. *功能:在指定区域画矩形
  322. *备注:
  323. *********************************************************************************/
  324. void LCD_DrawRectangle(uint16_t xsta, uint16_t ysta, uint16_t xend, uint16_t yend)
  325. {
  326. LCD_DrawLine(xsta,ysta,xend,ysta);
  327. LCD_DrawLine(xsta,ysta,xsta,yend);
  328. LCD_DrawLine(xsta,yend,xend,yend);
  329. LCD_DrawLine(xend,ysta,xend,yend);
  330. }
  331. /*********************************************************************************
  332. *名称:void LCD_ShowChar(u8 x, u16 y, u8 num, u8 size, u16 PenColor, u16 BackColor)
  333. *参数:x,y 起始坐标(x:0~234 y:0~308)
  334. num 字符ASCII码值
  335. size 字符大小,使用默认8*16
  336. PenColor 字体颜色
  337. BackColor 字体背景颜色
  338. *功能:
  339. *备注:注意屏幕大小
  340. *********************************************************************************/
  341. void LCD_ShowChar(u8 x, u16 y, u8 num, u8 size, u16 PenColor, u16 BackColor)
  342. {
  343. #define MAX_CHAR_POSX 232
  344. #define MAX_CHAR_POSY 304
  345. u8 temp;
  346. u8 pos,t;
  347. if(x>MAX_CHAR_POSX||y>MAX_CHAR_POSY)return;
  348. LCD_WindowMax(x,y,x+size/2,y+size); /*设置窗口 */
  349. LCD_SetCursor(x, y); /*设置光标位置 */
  350. LCD_WriteRAM_Prepare(); /*开始写入GRAM */
  351. num=num-' '; /*得到偏移后的值 */
  352. for(pos=0;pos<size;pos++)
  353. {
  354. if(size==12)
  355. temp=asc2_1206[num][pos];/*调用1206字体*/
  356. else
  357. temp=asc2_1608[num][pos]; /*调用1608字体 */
  358. for(t=0;t<size/2;t++)
  359. {
  360. if(temp&0x01) /*从低位开始*/
  361. {
  362. LCD_WR_DATA(PenColor); /*画字体颜色 一个点*/
  363. }
  364. else
  365. LCD_WR_DATA(BackColor); /*画背景颜色 一个点*/
  366. temp>>=1;
  367. }
  368. }
  369. LCD_WindowMax(0x0000,0x0000,240,320); /*恢复窗体大小*/
  370. }
  371. /*********************************************************************************
  372. 名称:void LCD_ShowCharString(uint16_t x, uint16_t y, const uint8_t *p, uint16_t PenColor, uint16_t BackColor)
  373. 参数:x,y 起始坐标
  374. p 指向字符串起始地址
  375. PenColor 字符颜色
  376. BackColor 背景颜色
  377. 功能:
  378. 备注:用16字体,可以调节 此函数不能单独调用
  379. *********************************************************************************/
  380. void LCD_ShowCharString(uint16_t x, uint16_t y, const uint8_t *p, uint16_t PenColor, uint16_t BackColor)
  381. {
  382. uint8_t size = 16; /*---字符大小默认16*8---*/
  383. if(x>MAX_CHAR_POSX){x=0;y+=size;} /*超出X轴字体最小单位,换行*/
  384. if(y>MAX_CHAR_POSY){y=x=0;LCD_Clear(WHITE);} /*超出Y轴字体最小单位,回到原点,并且清屏*/
  385. LCD_ShowChar(x, y, *p, size, PenColor, BackColor); /*0表示非叠加方式*/
  386. }
  387. /*********************************************************************************
  388. *名称: u16 findHzIndex(u8 *hz)
  389. *参数:hz
  390. *功能:索引汉字存储的内存地址
  391. *备注:
  392. *********************************************************************************/
  393. u16 findHzIndex(u8 *hz) /* 在自定义汉字库在查找所要显示 */
  394. /* 的汉字的位置 */
  395. {
  396. u16 i=0;
  397. FNT_GB16 *ptGb16 = (FNT_GB16 *)GBHZ_16; /*ptGb16指向GBHZ_16*/
  398. while(ptGb16[i].Index[0] > 0x80)
  399. {
  400. if ((*hz == ptGb16[i].Index[0]) && (*(hz+1) == ptGb16[i].Index[1])) /*汉字用两位来表示地址码*/
  401. {
  402. return i;
  403. }
  404. i++;
  405. if(i > (sizeof((FNT_GB16 *)GBHZ_16) / sizeof(FNT_GB16) - 1)) /* 搜索下标约束 */
  406. {
  407. break;
  408. }
  409. }
  410. return 0;
  411. }
  412. /*********************************************************************************
  413. *名称:void WriteOneHz(uint16_t x0, uint16_t y0, uint8_t *pucMsk, uint16_t PenColor, uint16_t BackColor)
  414. *参数:x0,y0 起始坐标
  415. *pucMsk 指向
  416. PenColor 字符颜色
  417. BackColor 背景颜色
  418. *功能:
  419. *备注:此函数不能单独作为汉字字符显示
  420. *********************************************************************************/
  421. void WriteOneHz(u16 x0, u16 y0, u8 *pucMsk, u16 PenColor, u16 BackColor)
  422. {
  423. u16 i,j;
  424. u16 mod[16]; /* 当前字模 16*16 */
  425. u16 *pusMsk; /* 当前字库地址 */
  426. u16 y;
  427. u16 size = 16; /*汉字默认大小16*16*/
  428. pusMsk = (u16 *)pucMsk;
  429. for(i=0; i<16; i++) /* 保存当前汉字点阵式字模 */
  430. {
  431. mod[i] = *pusMsk; /* 取得当前字模,半字对齐访问 */
  432. mod[i] = ((mod[i] & 0xff00) >> 8) | ((mod[i] & 0x00ff) << 8);/* 字模交换高低字节*/
  433. pusMsk = pusMsk+1;
  434. }
  435. y = y0;
  436. LCD_WindowMax(x0,y0,x0+size,y0+size); /*设置窗口*/
  437. LCD_SetCursor(x0,y0); /*设置光标位置 */
  438. LCD_WriteRAM_Prepare(); /*开始写入GRAM*/
  439. for(i=0; i<16; i++) /* 16行 */
  440. {
  441. for(j=0; j<16; j++) /* 16列 */
  442. {
  443. if((mod[i] << j) & 0x8000) /* 显示第i行 共16个点 */
  444. {
  445. LCD_WriteRAM(PenColor);
  446. }
  447. else
  448. {
  449. LCD_WriteRAM(BackColor); /* 用读方式跳过写空白点的像素*/
  450. }
  451. }
  452. y++;
  453. }
  454. LCD_WindowMax(0x0000,0x0000,240,320); /*恢复窗体大小*/
  455. }
  456. /*********************************************************************************
  457. *名称:void LCD_ShowHzString(u16 x0, u16 y0, u8 *pcStr, u16 PenColor, u16 BackColor)
  458. *参数:x0,y0 起始坐标
  459. pcStr 指向
  460. PenColor 字体颜色
  461. BackColor 字体背景
  462. *功能:显示汉字字符串
  463. *备注:这个函数不能单独调用
  464. *********************************************************************************/
  465. void LCD_ShowHzString(u16 x0, u16 y0, u8 *pcStr, u16 PenColor, u16 BackColor)
  466. {
  467. u16 usIndex;
  468. u8 size = 16;
  469. FNT_GB16 *ptGb16 = 0;
  470. ptGb16 = (FNT_GB16 *)GBHZ_16;
  471. if(x0>MAX_HZ_POSX){x0=0;y0+=size;} /*超出X轴字体最小单位,换行*/
  472. if(y0>MAX_HZ_POSY){y0=x0=0;LCD_Clear(WHITE);} /*超出Y轴字体最小单位,回到原点,并且清屏*/
  473. usIndex = findHzIndex(pcStr);
  474. WriteOneHz(x0, y0, (u8 *)&(ptGb16[usIndex].Msk[0]), PenColor, BackColor); /* 显示字符 */
  475. }
  476. /*********************************************************************************
  477. *名称:void LCD_ShowString(u16 x0, u16 y0, u8 *pcstr, u16 PenColor, u16 BackColor)
  478. *参数:x0 y0 起始坐标
  479. pcstr 字符串指针
  480. PenColor 字体颜色
  481. BackColor 字体背景色
  482. *功能:调用字符和汉字显示函数,实现字符串显示
  483. *备注:
  484. *********************************************************************************/
  485. void LCD_ShowString(u16 x0, u16 y0, u8 *pcStr, u16 PenColor, u16 BackColor)
  486. {
  487. while(*pcStr!='\0')
  488. {
  489. if (x0>MAX_HZ_POSX) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  490. {
  491. y0+=16;
  492. x0=0;
  493. }
  494. if (y0>MAX_HZ_POSY) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  495. {
  496. y0=0;
  497. }
  498. if(*pcStr>0x80) /*显示汉字*/
  499. {
  500. LCD_ShowHzString(x0, y0, pcStr, PenColor, BackColor);
  501. pcStr += 2;
  502. x0 += 16;
  503. }
  504. else /*显示字符*/
  505. {
  506. LCD_ShowCharString(x0, y0, pcStr, PenColor, BackColor);
  507. pcStr +=1;
  508. x0+= 8;
  509. }
  510. }
  511. }
  512. /****************************************************************************
  513. * 名 称:u16 ili9320_BGRtoRGB(u16 Color)
  514. * 功 能:RRRRRGGGGGGBBBBB 改为 BBBBBGGGGGGRRRRR 格式
  515. * 入口参数:Color BRG 颜色值
  516. * 出口参数:RGB 颜色值
  517. * 说 明:内部函数调用
  518. * 调用方法:
  519. ****************************************************************************/
  520. u16 LCD_RGBtoBGR(u16 Color)
  521. {
  522. u16 r, g, b, bgr;
  523. b = (Color>>0) & 0x1f; /* 提取B */
  524. g = (Color>>5) & 0x3f; /* 中间六位 */
  525. r = (Color>>11) & 0x1f; /* 提取R */
  526. bgr = (b<<11) + (g<<5) + (r<<0);
  527. return( bgr );
  528. }
  529. /*********************************************************************************
  530. * 名 称:void LCD_DrawPicture(u16 StartX,u16 StartY,u16 EndX,u16 EndY,u16 *pic)
  531. * 功 能:在指定座标范围显示一副图片
  532. * 入口参数:StartX 行起始座标
  533. * StartY 列起始座标
  534. * EndX 行结束座标
  535. * EndY 列结束座标
  536. pic 图片头指针
  537. * 出口参数:无
  538. * 说 明:图片取模格式为水平扫描,16位颜色模式
  539. * 调用方法:LCD_DrawPicture(0,0,100,100,(u16*)demo);
  540. *********************************************************************************/
  541. void LCD_DrawPicture(u16 StartX,u16 StartY,u16 Xend,u16 Yend,u8 *pic)
  542. {
  543. static u16 i=0,j=0;
  544. u16 *bitmap = (u16 *)pic;
  545. /*设置图片显示窗口大小*/
  546. LCD_WindowMax(StartX, StartY, Xend, Yend);
  547. LCD_SetCursor(StartX,StartY);
  548. LCD_WriteRAM_Prepare();
  549. for(j=0; j<Yend-StartY; j++)
  550. {
  551. for(i=0; i<Xend-StartX; i++) LCD_WriteRAM(*bitmap++);
  552. }
  553. /*恢复窗口*/
  554. LCD_WindowMax(0, 0, 240, 320);
  555. }
  556. /*--------------下面三个函数 实现字库的调用及显示-------------------*/
  557. FATFS fs;
  558. FIL fsrc; /* file objects */
  559. UINT br; // File R/W count
  560. static uint8_t Find_Chinese(uint8_t* str, uint8_t* p)
  561. {
  562. uint8_t High8bit,Low8bit;
  563. FRESULT res;
  564. uint8_t error[]="Please put the sys folder which include the font file HZK16.bin into the SD card root directory. ";
  565. High8bit=*str; /* 高8位数据 */
  566. Low8bit=*(str+1); /* 低8位数据 */
  567. f_mount(0, &fs);
  568. /* Infinite loop */
  569. res = f_open(&fsrc, "/sys/HZK16.bin", FA_OPEN_EXISTING | FA_READ); /* 在SD卡里搜索HZK16.bin*/
  570. if(res != 0)
  571. {
  572. LCD_ShowString(32,100,error,RED ,BLACK );
  573. return 1;
  574. }
  575. f_lseek(&fsrc,32*((High8bit-0xa0-1)*94+(Low8bit-0xa0-1)));
  576. f_read(&fsrc, p, 32, &br);
  577. f_close(&fsrc);
  578. f_mount(0, NULL);
  579. return 0;
  580. }
  581. /*********************************************************************************
  582. * 名 称:void PutChinese(u16 x, u16 y, u8 *c, u32 PenColor, u32 BackColor)
  583. * 入口参数:u16 x,u16 y 字符坐标位置
  584. u8 *c 汉字的指针
  585. u32 PenColor画笔颜色
  586. u32 BackColor背景颜色
  587. * 出口参数:无
  588. * 功 能:从SPI_FLASH里查找汉字基地址,
  589. 并将32字节的点阵数据送到LCD显示
  590. * 说 明:只能被GUI_Put调用
  591. *********************************************************************************/
  592. void PutChinese(u16 x, u16 y, u8 *c, u32 PenColor, u32 BackColor)
  593. {
  594. u16 i=0;
  595. u16 j=0;
  596. u8 buffer[32];
  597. u16 tmp_char=0;
  598. if(!Find_Chinese(c,buffer))
  599. {
  600. LCD_WindowMax(x,y,x+16,y+16); /*设置窗口*/
  601. LCD_SetCursor(x,y); /*设置光标位置 */
  602. LCD_WriteRAM_Prepare();
  603. /* 16*16汉字点阵,共有16行 每行16个点,存储的时候是以字节为单位
  604. 需合并成字*/
  605. for (i=0;i<16;i++)
  606. {
  607. tmp_char=buffer[i*2];
  608. tmp_char=(tmp_char<<8);
  609. tmp_char|=buffer[2*i+1]; /*现在tmp_char存储着一行的点阵数据*/
  610. for (j=0;j<16;j++)
  611. {
  612. if((tmp_char<<j) & 0x8000)
  613. LCD_WriteRAM(PenColor);
  614. else
  615. LCD_WriteRAM(BackColor);
  616. }
  617. }
  618. LCD_WindowMax(0x00,0x00,240,320); /*恢复窗体大小*/
  619. }
  620. }
  621. /*********************************************************************************
  622. * 名 称:void GUI_Put(u16 x, u16 y, u8 *str,u32 PenColor, u32 BackColor)
  623. * 入口参数:u16 x,u16 y 字符坐标位置
  624. u8 *c 汉字的指针
  625. u32 PenColor画笔颜色
  626. u32 BackColor背景颜色
  627. * 出口参数:无
  628. * 功 能:计算汉字显示坐标
  629. * 说 明:
  630. *********************************************************************************/
  631. void GUI_Put(u16 x, u16 y, u8 *str,u32 PenColor, u32 BackColor)
  632. {
  633. do
  634. {
  635. if (x>MAX_HZ_POSX) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  636. {
  637. y+=16;
  638. x=0;
  639. }
  640. if (y>MAX_HZ_POSY) /*保证剩余的空间能放下一个16*16的字,否则换行 */
  641. {
  642. y=0;
  643. }
  644. if ( *str < 0x80) /* 显示字符,字符点阵直接从font.h里调用,大小为16*8 */
  645. {
  646. LCD_ShowCharString(x, y, str, PenColor, BackColor);
  647. x+= 8;
  648. str++;
  649. }
  650. else /*大于等于0x80判断为汉字字符*/
  651. {
  652. PutChinese(x, y, str++, PenColor, BackColor);
  653. str++;
  654. x+=16;
  655. }
  656. }while(*str!=0);
  657. }
  658. /*********************************************************************************
  659. *名称:void LCD_Init(void)
  660. *输入:无
  661. *返回:无
  662. *功能:LCD初始化
  663. *说明:
  664. *********************************************************************************/
  665. void LCD_Init(void)
  666. {
  667. LCD_Configuration();
  668. Delay_10ms(10); /* delay 50 ms */
  669. Delay_10ms(10); /* delay 50 ms */
  670. DeviceCode = LCD_ReadReg(0x0000); /*读取屏的ID号*/
  671. Delay_10ms(10); /* delay 50 ms */
  672. if(DeviceCode==0x8999) /*对应的驱动IC为SSD1298*/
  673. {
  674. /*----- Start Initial Sequence ------*/
  675. LCD_WriteReg(0x00, 0x0001); /*开启内部振荡器*/
  676. LCD_WriteReg(0x01, 0x3B3F); /*驱动输出控制 */
  677. LCD_WriteReg(0x02, 0x0600); /* set 1 line inversion */
  678. /*-------- Power control setup --------*/
  679. LCD_WriteReg(0x0C, 0x0007); /* Adjust VCIX2 output voltage */
  680. LCD_WriteReg(0x0D, 0x0006); /* Set amplitude magnification of VLCD63 */
  681. LCD_WriteReg(0x0E, 0x3200); /* Set alternating amplitude of VCOM */
  682. LCD_WriteReg(0x1E, 0x00BB); /* Set VcomH voltage */
  683. LCD_WriteReg(0x03, 0x6A64); /* Step-up factor/cycle setting */
  684. /*-------- RAM position control --------*/
  685. LCD_WriteReg(0x0F, 0x0000); /* Gate scan position start at G0 */
  686. LCD_WriteReg(0x44, 0xEF00); /* Horizontal RAM address position */
  687. LCD_WriteReg(0x45, 0x0000); /* Vertical RAM address start position*/
  688. LCD_WriteReg(0x46, 0x013F); /* Vertical RAM address end position */
  689. /* ------ Adjust the Gamma Curve -------*/
  690. LCD_WriteReg(0x30, 0x0000);
  691. LCD_WriteReg(0x31, 0x0706);
  692. LCD_WriteReg(0x32, 0x0206);
  693. LCD_WriteReg(0x33, 0x0300);
  694. LCD_WriteReg(0x34, 0x0002);
  695. LCD_WriteReg(0x35, 0x0000);
  696. LCD_WriteReg(0x36, 0x0707);
  697. LCD_WriteReg(0x37, 0x0200);
  698. LCD_WriteReg(0x3A, 0x0908);
  699. LCD_WriteReg(0x3B, 0x0F0D);
  700. /*--------- Special command -----------*/
  701. LCD_WriteReg(0x28, 0x0006); /* Enable test command */
  702. LCD_WriteReg(0x2F, 0x12EB); /* RAM speed tuning */
  703. LCD_WriteReg(0x26, 0x7000); /* Internal Bandgap strength */
  704. LCD_WriteReg(0x20, 0xB0E3); /* Internal Vcom strength */
  705. LCD_WriteReg(0x27, 0x0044); /* Internal Vcomh/VcomL timing */
  706. LCD_WriteReg(0x2E, 0x7E45); /* VCOM charge sharing time */
  707. /*--------- Turn On display ------------*/
  708. LCD_WriteReg(0x10, 0x0000); /* Sleep mode off */
  709. Delay_10ms(3); /* Wait 30mS */
  710. LCD_WriteReg(0x11, 0x6870); /* Entry mode setup. 262K type B, take care on the data bus with 16it only */
  711. LCD_WriteReg(0x07, 0x0033); /* Display ON */
  712. }
  713. Delay_10ms(5); /*延时50ms*/
  714. LCD_Clear(BLACK);
  715. }