ili9325.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include "stm32f10x.h"
  2. #include "ili9325.h"
  3. /* LCD Registers */
  4. #define R0 0x00
  5. #define R1 0x01
  6. #define R2 0x02
  7. #define R3 0x03
  8. #define R4 0x04
  9. #define R5 0x05
  10. #define R6 0x06
  11. #define R7 0x07
  12. #define R8 0x08
  13. #define R9 0x09
  14. #define R10 0x0A
  15. #define R12 0x0C
  16. #define R13 0x0D
  17. #define R14 0x0E
  18. #define R15 0x0F
  19. #define R16 0x10
  20. #define R17 0x11
  21. #define R18 0x12
  22. #define R19 0x13
  23. #define R20 0x14
  24. #define R21 0x15
  25. #define R22 0x16
  26. #define R23 0x17
  27. #define R24 0x18
  28. #define R25 0x19
  29. #define R26 0x1A
  30. #define R27 0x1B
  31. #define R28 0x1C
  32. #define R29 0x1D
  33. #define R30 0x1E
  34. #define R31 0x1F
  35. #define R32 0x20
  36. #define R33 0x21
  37. #define R34 0x22
  38. #define R36 0x24
  39. #define R37 0x25
  40. #define R40 0x28
  41. #define R41 0x29
  42. #define R43 0x2B
  43. #define R45 0x2D
  44. #define R48 0x30
  45. #define R49 0x31
  46. #define R50 0x32
  47. #define R51 0x33
  48. #define R52 0x34
  49. #define R53 0x35
  50. #define R54 0x36
  51. #define R55 0x37
  52. #define R56 0x38
  53. #define R57 0x39
  54. #define R59 0x3B
  55. #define R60 0x3C
  56. #define R61 0x3D
  57. #define R62 0x3E
  58. #define R63 0x3F
  59. #define R64 0x40
  60. #define R65 0x41
  61. #define R66 0x42
  62. #define R67 0x43
  63. #define R68 0x44
  64. #define R69 0x45
  65. #define R70 0x46
  66. #define R71 0x47
  67. #define R72 0x48
  68. #define R73 0x49
  69. #define R74 0x4A
  70. #define R75 0x4B
  71. #define R76 0x4C
  72. #define R77 0x4D
  73. #define R78 0x4E
  74. #define R79 0x4F
  75. #define R80 0x50
  76. #define R81 0x51
  77. #define R82 0x52
  78. #define R83 0x53
  79. #define R96 0x60
  80. #define R97 0x61
  81. #define R106 0x6A
  82. #define R118 0x76
  83. #define R128 0x80
  84. #define R129 0x81
  85. #define R130 0x82
  86. #define R131 0x83
  87. #define R132 0x84
  88. #define R133 0x85
  89. #define R134 0x86
  90. #define R135 0x87
  91. #define R136 0x88
  92. #define R137 0x89
  93. #define R139 0x8B
  94. #define R140 0x8C
  95. #define R141 0x8D
  96. #define R143 0x8F
  97. #define R144 0x90
  98. #define R145 0x91
  99. #define R146 0x92
  100. #define R147 0x93
  101. #define R148 0x94
  102. #define R149 0x95
  103. #define R150 0x96
  104. #define R151 0x97
  105. #define R152 0x98
  106. #define R153 0x99
  107. #define R154 0x9A
  108. #define R157 0x9D
  109. #define R192 0xC0
  110. #define R193 0xC1
  111. #define R229 0xE5
  112. /* LCD color */
  113. #define White 0xFFFF
  114. #define Black 0x0000
  115. #define Grey 0xF7DE
  116. #define Blue 0x001F
  117. #define Blue2 0x051F
  118. #define Red 0xF800
  119. #define Magenta 0xF81F
  120. #define Green 0x07E0
  121. #define Cyan 0x7FFF
  122. #define Yellow 0xFFE0
  123. static u16 DeviceCode;
  124. static void LCD_CtrlLinesConfig(void)
  125. {
  126. GPIO_InitTypeDef GPIO_InitStructure;
  127. /* Enable FSMC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
  128. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  129. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
  130. RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |
  131. RCC_APB2Periph_AFIO, ENABLE);
  132. /* Set PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
  133. PD.10(D15), PD.14(D0), PD.15(D1) as alternate
  134. function push pull */
  135. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
  136. GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
  137. GPIO_Pin_15;
  138. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  139. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  140. GPIO_Init(GPIOD, &GPIO_InitStructure);
  141. /* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
  142. PE.14(D11), PE.15(D12) as alternate function push pull */
  143. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
  144. GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
  145. GPIO_Pin_15;
  146. GPIO_Init(GPIOE, &GPIO_InitStructure);
  147. GPIO_WriteBit(GPIOE, GPIO_Pin_6, Bit_SET);
  148. /* Set PF.00(A0 (RS)) as alternate function push pull */
  149. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2;
  150. GPIO_Init(GPIOF, &GPIO_InitStructure);
  151. /* Set NE2(LCD/CS) as alternate function push pull */
  152. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  153. GPIO_Init(GPIOG, &GPIO_InitStructure);
  154. }
  155. /*******************************************************************************
  156. * Function Name : LCD_FSMCConfig
  157. * Description : Configures the Parallel interface (FSMC) for LCD(Parallel mode)
  158. * Input : None
  159. * Output : None
  160. * Return : None
  161. *******************************************************************************/
  162. static void LCD_FSMCConfig(void)
  163. {
  164. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  165. FSMC_NORSRAMTimingInitTypeDef p;
  166. /*-- FSMC Configuration ------------------------------------------------------*/
  167. /*----------------------- SRAM Bank 4 ----------------------------------------*/
  168. /* FSMC_Bank1_NORSRAM4 configuration */
  169. p.FSMC_AddressSetupTime = 0;
  170. p.FSMC_AddressHoldTime = 0;
  171. p.FSMC_DataSetupTime = 2;
  172. p.FSMC_BusTurnAroundDuration = 0;
  173. p.FSMC_CLKDivision = 0;
  174. p.FSMC_DataLatency = 0;
  175. p.FSMC_AccessMode = FSMC_AccessMode_A;
  176. /* Color LCD configuration ------------------------------------
  177. LCD configured as follow:
  178. - Data/Address MUX = Disable
  179. - Memory Type = SRAM
  180. - Data Width = 16bit
  181. - Write Operation = Enable
  182. - Extended Mode = Enable
  183. - Asynchronous Wait = Disable */
  184. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
  185. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  186. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  187. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  188. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  189. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  190. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  191. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  192. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  193. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  194. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  195. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  196. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  197. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  198. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  199. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);
  200. }
  201. static void LCD_X_Init(void)
  202. {
  203. /* Configure the LCD Control pins --------------------------------------------*/
  204. LCD_CtrlLinesConfig();
  205. /* Configure the FSMC Parallel interface -------------------------------------*/
  206. LCD_FSMCConfig();
  207. }
  208. /*******************************************************************************
  209. * Function Name : ili9325_WriteReg
  210. * Description : Writes to the selected LCD register.
  211. * Input : - LCD_Reg: address of the selected register.
  212. * - LCD_RegValue: value to write to the selected register.
  213. * Output : None
  214. * Return : None
  215. *******************************************************************************/
  216. void ili9325_WriteReg(u8 LCD_Reg,u16 LCD_RegValue)
  217. {
  218. /* Write 16-bit Index, then Write Reg */
  219. ili9325_REG = LCD_Reg;
  220. /* Write 16-bit Reg */
  221. ili9325_RAM = LCD_RegValue;
  222. }
  223. /*******************************************************************************
  224. * Function Name : LCD_ReadReg
  225. * Description : Reads the selected LCD Register.
  226. * Input : None
  227. * Output : None
  228. * Return : LCD Register Value.
  229. *******************************************************************************/
  230. u16 LCD_ReadReg(u8 LCD_Reg)
  231. {
  232. /* Write 16-bit Index (then Read Reg) */
  233. ili9325_REG = LCD_Reg;
  234. /* Read 16-bit Reg */
  235. return (ili9325_RAM);
  236. }
  237. /*******************************************************************************
  238. * Function Name : ili9325_WriteRAM_Prepare
  239. * Description : Prepare to write to the LCD RAM.
  240. * Input : None
  241. * Output : None
  242. * Return : None
  243. *******************************************************************************/
  244. void ili9325_WriteRAM_Prepare(void)
  245. {
  246. ili9325_REG = R34;
  247. }
  248. /*******************************************************************************
  249. * Function Name : ili9325_WriteRAM
  250. * Description : Writes to the LCD RAM.
  251. * Input : - RGB_Code: the pixel color in RGB mode (5-6-5).
  252. * Output : None
  253. * Return : None
  254. *******************************************************************************/
  255. void ili9325_WriteRAM(u16 RGB_Code)
  256. {
  257. /* Write 16-bit GRAM Reg */
  258. ili9325_RAM = RGB_Code;
  259. }
  260. /* 读取指定地址的GRAM */
  261. unsigned short ili9325_ReadGRAM(unsigned short x,unsigned short y)
  262. {
  263. unsigned short temp;
  264. ili9325_SetCursor(x,y);
  265. ili9325_REG = R34;
  266. /* dummy read */
  267. temp = ili9325_RAM;
  268. temp = ili9325_RAM;
  269. return temp;
  270. }
  271. void ili9325_Initializtion(void)
  272. {
  273. volatile unsigned int i;
  274. LCD_X_Init();
  275. {
  276. GPIO_InitTypeDef GPIO_InitStructure;
  277. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
  278. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  279. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  280. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  281. GPIO_Init(GPIOF,&GPIO_InitStructure);
  282. }
  283. GPIO_SetBits(GPIOF, GPIO_Pin_10);
  284. DeviceCode = LCD_ReadReg(0x0000);
  285. if (DeviceCode==0x9325||DeviceCode==0x9328)
  286. {
  287. ili9325_WriteReg(0x00e7,0x0010);
  288. ili9325_WriteReg(0x0000,0x0001); //start internal osc
  289. ili9325_WriteReg(0x0001,0x0100);
  290. ili9325_WriteReg(0x0002,0x0700); //power on sequence
  291. /* [5:4]-ID1~ID0 [3]-AM-1垂直-0水平 */
  292. ili9325_WriteReg(0x0003,(1<<12)|(1<<5)|(0<<4) | (1<<3) );
  293. ili9325_WriteReg(0x0004,0x0000);
  294. ili9325_WriteReg(0x0008,0x0207);
  295. ili9325_WriteReg(0x0009,0x0000);
  296. ili9325_WriteReg(0x000a,0x0000); //display setting
  297. ili9325_WriteReg(0x000c,0x0001); //display setting
  298. ili9325_WriteReg(0x000d,0x0000); //0f3c
  299. ili9325_WriteReg(0x000f,0x0000);
  300. //Power On sequence //
  301. ili9325_WriteReg(0x0010,0x0000);
  302. ili9325_WriteReg(0x0011,0x0007);
  303. ili9325_WriteReg(0x0012,0x0000);
  304. ili9325_WriteReg(0x0013,0x0000);
  305. for (i=50000; i>0; i--);
  306. for (i=50000; i>0; i--);
  307. ili9325_WriteReg(0x0010,0x1590);
  308. ili9325_WriteReg(0x0011,0x0227);
  309. for (i=50000; i>0; i--);
  310. for (i=50000; i>0; i--);
  311. ili9325_WriteReg(0x0012,0x009c);
  312. for (i=50000; i>0; i--);
  313. for (i=50000; i>0; i--);
  314. ili9325_WriteReg(0x0013,0x1900);
  315. ili9325_WriteReg(0x0029,0x0023);
  316. ili9325_WriteReg(0x002b,0x000e);
  317. for (i=50000; i>0; i--);
  318. for (i=50000; i>0; i--);
  319. ili9325_WriteReg(0x0020,0x0000);
  320. ili9325_WriteReg(0x0021,0x0000);
  321. for (i=50000; i>0; i--);
  322. for (i=50000; i>0; i--);
  323. ili9325_WriteReg(0x0030,0x0007);
  324. ili9325_WriteReg(0x0031,0x0707);
  325. ili9325_WriteReg(0x0032,0x0006);
  326. ili9325_WriteReg(0x0035,0x0704);
  327. ili9325_WriteReg(0x0036,0x1f04);
  328. ili9325_WriteReg(0x0037,0x0004);
  329. ili9325_WriteReg(0x0038,0x0000);
  330. ili9325_WriteReg(0x0039,0x0706);
  331. ili9325_WriteReg(0x003c,0x0701);
  332. ili9325_WriteReg(0x003d,0x000f);
  333. for (i=50000; i>0; i--);
  334. for (i=50000; i>0; i--);
  335. ili9325_WriteReg(0x0050,0x0000);
  336. ili9325_WriteReg(0x0051,0x00ef);
  337. ili9325_WriteReg(0x0052,0x0000);
  338. ili9325_WriteReg(0x0053,0x013f);
  339. ili9325_WriteReg(0x0060,0xa700);
  340. ili9325_WriteReg(0x0061,0x0001);
  341. ili9325_WriteReg(0x006a,0x0000);
  342. ili9325_WriteReg(0x0080,0x0000);
  343. ili9325_WriteReg(0x0081,0x0000);
  344. ili9325_WriteReg(0x0082,0x0000);
  345. ili9325_WriteReg(0x0083,0x0000);
  346. ili9325_WriteReg(0x0084,0x0000);
  347. ili9325_WriteReg(0x0085,0x0000);
  348. ili9325_WriteReg(0x0090,0x0010);
  349. ili9325_WriteReg(0x0092,0x0000);
  350. ili9325_WriteReg(0x0093,0x0003);
  351. ili9325_WriteReg(0x0095,0x0110);
  352. ili9325_WriteReg(0x0097,0x0000);
  353. ili9325_WriteReg(0x0098,0x0000);
  354. //display on sequence
  355. ili9325_WriteReg(0x0007,0x0133);
  356. ili9325_WriteReg(0x0020,0x0000);
  357. ili9325_WriteReg(0x0021,0x0000);
  358. }
  359. ili9325_Clear( Blue );
  360. }
  361. /****************************************************************************
  362. * 名 称:void ili9325_SetCursor(u16 x,u16 y)
  363. * 功 能:设置屏幕座标
  364. * 入口参数:x 行座标
  365. * y 列座标
  366. * 出口参数:无
  367. * 说 明:
  368. * 调用方法:ili9325_SetCursor(10,10);
  369. ****************************************************************************/
  370. void ili9325_SetCursor(u16 x,u16 y)
  371. {
  372. ili9325_WriteReg(R32,x); /* 0-239 */
  373. ili9325_WriteReg(R33,y); /* 0-319 */
  374. }
  375. /****************************************************************************
  376. * 名 称:void ili9325_SetWindows(u16 StartX,u16 StartY,u16 EndX,u16 EndY)
  377. * 功 能:设置窗口区域
  378. * 入口参数:StartX 行起始座标
  379. * StartY 列起始座标
  380. * EndX 行结束座标
  381. * EndY 列结束座标
  382. * 出口参数:无
  383. * 说 明:
  384. * 调用方法:ili9325_SetWindows(0,0,100,100);
  385. ****************************************************************************/
  386. void ili9325_SetWindows(u16 StartX,u16 StartY,u16 EndX,u16 EndY)
  387. {
  388. ili9325_SetCursor(StartX,StartY);
  389. ili9325_WriteReg(0x0050, StartX);
  390. ili9325_WriteReg(0x0052, StartY);
  391. ili9325_WriteReg(0x0051, EndX);
  392. ili9325_WriteReg(0x0053, EndY);
  393. }
  394. /****************************************************************************
  395. * 名 称:void ili9325_Clear(u16 dat)
  396. * 功 能:将屏幕填充成指定的颜色,如清屏,则填充 0xffff
  397. * 入口参数:dat 填充值
  398. * 出口参数:无
  399. * 说 明:
  400. * 调用方法:ili9325_Clear(0xffff);
  401. ****************************************************************************/
  402. void ili9325_Clear(u16 Color)
  403. {
  404. u32 index=0;
  405. ili9325_SetCursor(0,0);
  406. ili9325_WriteRAM_Prepare(); /* Prepare to write GRAM */
  407. for (index=0; index<76800; index++)
  408. {
  409. ili9325_RAM=Color;
  410. }
  411. }
  412. /****************************************************************************
  413. * 名 称:void ili9325_DrawPicture(u16 StartX,u16 StartY,u16 EndX,u16 EndY,u16 *pic)
  414. * 功 能:在指定座标范围显示一副图片
  415. * 入口参数:StartX 行起始座标
  416. * StartY 列起始座标
  417. * EndX 行结束座标
  418. * EndY 列结束座标
  419. pic 图片头指针
  420. * 出口参数:无
  421. * 说 明:图片取模格式为水平扫描,16位颜色模式
  422. * 调用方法:ili9325_DrawPicture(0,0,100,100,(u16*)demo);
  423. ****************************************************************************/
  424. void ili9325_DrawPicture(u16 StartX,u16 StartY,u16 EndX,u16 EndY,u16 *pic)
  425. {
  426. u16 i;
  427. ili9325_SetWindows(StartX,StartY,EndX,EndY);
  428. ili9325_SetCursor(StartX,StartY);
  429. ili9325_WriteRAM_Prepare();
  430. for (i=0; i<(EndX*EndY); i++)
  431. {
  432. ili9325_WriteRAM(*pic++);
  433. }
  434. }
  435. /****************************************************************************
  436. * 名 称:u16 ili9325_BGR2RGB(u16 c)
  437. * 功 能:RRRRRGGGGGGBBBBB 改为 BBBBBGGGGGGRRRRR 格式
  438. * 入口参数:c BRG 颜色值
  439. * 出口参数:RGB 颜色值
  440. * 说 明:内部函数调用
  441. * 调用方法:
  442. ****************************************************************************/
  443. unsigned short ili9325_BGR2RGB(unsigned short c)
  444. {
  445. u16 r, g, b, rgb;
  446. b = (c>>0) & 0x1f;
  447. g = (c>>5) & 0x3f;
  448. r = (c>>11) & 0x1f;
  449. rgb = (b<<11) + (g<<5) + (r<<0);
  450. return( rgb );
  451. }