我的单片机驱动ILI9320源码
扫描二维码
随时随地手机看文章
ili9320.h
1 #ifndef __ILI9320_H__
2 #define __ILI9320_H__
3 #include "reg52.h"
4
5 #define u8 unsigned char
6 #define u16 unsigned int
7 #define uchar unsigned char
8 #define uint unsigned int
9
10 //#define LCD_COLORS_NUM 65536
11 #define LCD_SIZE_X 240
12 #define LCD_SIZE_Y 320
13
14
15
16 //-------------------------------------------------------
17 //-----------------------端口定义------------------------
18
19 #define LCD_DataH P0//高8位数据
20 #define LCD_DataL P1//低8位数据
21
22 sbit LCD_RST= P2^0 ;//复位
23 sbit LCD_CS =P2^1;//使能端
24
25 sbit LCD_RS= P3^5; //数据|指令
26 sbit LCD_WR= P3^6; //写
27 sbit LCD_RD =P3^7; //读
28
29 //---------------------------------------------------------
30 //-------------------函数声明------------------------------
31
32 //////RGB转换,888->565/////
33 u16 LCD_RGB(u8 R,u8 G,u8 B);
34
35 /////////延时//////////
36 void LCD_Delay(uint t);
37
38 ///////写索引寄存器////////
39 void LCD_WriteIndex(u8 index);
40
41 ///////写数据////////
42 void LCD_WriteData(u16 Data);
43
44 ///////写寄存器//////////
45 void LCD_WriteCOM(u8 index,u16 Data);
46
47 /////读芯片ID,0x9320/////
48 u16 LCD_ReadID(void);
49
50 //////读像素////////
51 u16 LCD_ReadPixel(u16 x,u16 y);
52
53 //////设置像素////////
54 void LCD_SetPixel(u16 color);
55
56 //////设置显示窗口/////
57 void LCD_SetWindow(uint x1,uint y1,uint x2,uint y2);
58
59 ///////设置光标//////
60 //void LCD_SetCursor(uint x,uint y);
61
62 ///////LCD初始化///////
63 void LCD_Init(void);
64
65 #endif
ili9320.c
1 #include "reg52.h"
2 #include "ili9320.h"
3
4
5 //-----------------------------------------------------
6 //读取芯片型号,型号:0X9320
7
8 u16 LCD_ReadID(void)
9 {
10 u16 t;
11 LCD_WriteIndex(0x00);
12 LCD_DataH=0xff;
13 LCD_DataL=0xff;
14 LCD_RS=1;
15 LCD_RD=1;
16 LCD_RD=0; //RD不需要变换两次
17 t=(LCD_DataH<<8)|LCD_DataL ;
18 return t;
19 }
20
21
22 //-----------------------------------------------------
23 //在65536色彩下,读取像素点
24
25 u16 LCD_ReadPixel(u16 x,u16 y)
26 {
27 u16 t;
28 LCD_WriteCOM(0x20,x);
29 LCD_WriteCOM(0x21,y);
30 LCD_WriteIndex(0x22);
31
32 LCD_DataH=0xff;
33 LCD_DataL=0xff;
34 LCD_RS=1;
35 LCD_RD=1;
36 LCD_RD=0;
37 LCD_RD=1;
38 LCD_RD=0; //RD需要变换两次
39 t=(LCD_DataH<<8)|LCD_DataL ;
40 return t;
41 }
42
43 //------------------------------------------------------
44 //设置显示窗口
45
46 void LCD_SetWindow(uint x1,uint y1,uint x2,uint y2)
47 {
48 LCD_WriteCOM(0x20,x1);
49 LCD_WriteCOM(0x21,y1);
50 LCD_WriteCOM(0x50,x1);
51 LCD_WriteCOM(0x52,y1);
52 LCD_WriteCOM(0x51,x2);
53 LCD_WriteCOM(0x53,y2);
54 LCD_WriteIndex(0x22);
55 }
56
57 //-------------------------------------------------------
58 //设置光标
59 void LCD_SetCursor(uint x,uint y)
60 {
61 LCD_WriteCOM(0x20,x);
62 LCD_WriteCOM(0x21,y);
63 LCD_WriteIndex(0x22);
64 }
65
66
67
68
69 //----------------------------------------------------------
70 //在65536色彩下,将18位颜色数据转化为16位
71 u16 LCD_RGB(u8 R,u8 G,u8 B)
72 {
73 u16 tRGB;
74 tRGB=0x00;
75 tRGB=tRGB|R;
76 tRGB=tRGB<<6;
77 tRGB=tRGB|G;
78 tRGB=tRGB<<5;
79 tRGB=tRGB|B;
80 return tRGB;
81 }
82
83 //---------------------------------------------------------
84 //延时函数
85 void LCD_Delay(uint t)
86 {
87 for(;t>0;t--);
88 }
89
90 //----------------------------------------------------------
91 //写入索引寄存器
92 void LCD_WriteIndex(u8 index)
93 {
94 LCD_RS=0;
95 LCD_RD=1;
96 LCD_WR=0;
97 LCD_DataH=0x00;
98 LCD_DataL=index;
99 LCD_WR=1;
100 }
101
102 //-----------------------------------------
103 //写入数据
104
105 void LCD_WriteData(u16 Data)
106 {
107 LCD_RS=1;
108 LCD_WR=0;
109 LCD_DataH=Data>>8;
110 LCD_DataL=Data;
111 LCD_WR=1;
112
113 }
114
115 //-----------------------------------------
116 //写寄存器
117
118 void LCD_WriteCOM(u8 index,u16 Data)
119 {
120 LCD_WriteIndex(index);
121 LCD_WriteData(Data);
122 }
123
124
125
126 //-----------------------------------------------
127 //LCD初始化函数
128
129 void LCD_Init(void)
130 {
131 //************* Reset LCD Driver ****************//
132 LCD_CS=0;
133 LCD_RST=1;
134 LCD_RD=1;
135 LCD_Delay(10);
136 LCD_RST=0;
137 LCD_Delay(100);
138 LCD_RST=1;
139 LCD_Delay(10);
140 //************* Start Initial Sequence **********//
141 LCD_WriteCOM(0x00, 0x0001); // 开始内部振荡
142 LCD_WriteCOM(0x01, 0x0100); // set SS and SM bit
143 LCD_WriteCOM(0x02, 0x0400); // set 1 line inversion
144 LCD_WriteCOM(0x03, 0x1030);
145 LCD_WriteCOM(0x04, 0x0000); // Resize register
146 LCD_WriteCOM(0x08, 0x0202); // set the back porch and front porch
147 LCD_WriteCOM(0x09, 0x0000); // set non-display area refresh cycle ISC[3:0]
148 LCD_WriteCOM(0x0A, 0x0000); // FMARK function
149 LCD_WriteCOM(0x0C, 0x0000); // RGB interface setting
150 LCD_WriteCOM(0x0D, 0x0000); // Frame marker Position
151 LCD_WriteCOM(0x0F, 0x0000); // RGB interface polarity
152
153 LCD_WriteCOM(0x07, 0x0101); // Display Control 1
154 LCD_Delay(500);
155
156 //*************Power On sequence ****************// LCD_WriteCOM(0x51, 0x00, 0xEF);
157
158 LCD_WriteCOM(0x10, 0x16b0);
159 LCD_WriteCOM(0x11, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]
160 LCD_WriteCOM(0x12, 0x0138);
161 LCD_WriteCOM(0x13, 0x0b00);
162 LCD_WriteCOM(0x2B, 0x2008);
163 // ---------- Gamma Control ---------- //
164 LCD_WriteCOM(0x30, 0x0000);
165 LCD_WriteCOM(0x31, 0x0306);
166 LCD_WriteCOM(0x32, 0x0200);
167 LCD_WriteCOM(0x35, 0x0107);
168 LCD_WriteCOM(0x36, 0x0404);
169 LCD_WriteCOM(0x37, 0x0606);
170 LCD_WriteCOM(0x38, 0x0105);
171 LCD_WriteCOM(0x39, 0x0707);
172 LCD_WriteCOM(0x3C, 0x0600);
173 LCD_WriteCOM(0x3D, 0x0807);
174 // ---------- Window Address Area ---------- //
175 LCD_WriteCOM(0x50, 0x0000); // Horizontal GRAM Start Address-----HSA[7:0]
176 LCD_WriteCOM(0x51, 0x00EF); // Horizontal GRAM End Address-----HEA[7:0]
177 LCD_WriteCOM(0x52, 0x0000); // Vertical GRAM Start Address-----VSA[8:0]
178 LCD_WriteCOM(0x53, 0x013F); // Vertical GRAM Start Address-----VEA[8:0]
179 // ---------- Gate Scan Control ---------- //
180 LCD_WriteCOM(0x60, 0x2700); // GS, NL[5:0], SCN[5:0]
181 LCD_WriteCOM(0x61, 0x0001); // NDL,VLE, REV
182 LCD_WriteCOM(0x6A, 0x0000); // VL[8:0]
183 // ---------- Partial Display Control ---------- //
184 LCD_WriteCOM(0x80, 0x0000); // Partial Image 1 Display Position-----PTDP0[8:0]
185 LCD_WriteCOM(0x81, 0x0000); // Partial Image 1 Start Address-----PTSA0[8:0]
186 LCD_WriteCOM(0x82, 0x0000); // Partial Image 1 End Address-----PTEA0[8:0]
187 LCD_WriteCOM(0x83, 0x0000); // Partial Image 2 Display Position-----PTDP1[8:0]
188 LCD_WriteCOM(0x84, 0x0000); // Partial Image 2 Start Address-----PTSA1[8:0]
189 LCD_WriteCOM(0x85, 0x0000); // Partial Image 2 Start Address-----PTEA1[8:0]
190 // ---------- Panel Interface Control ---------- //
191 LCD_WriteCOM(0x90, 0x0013); // Panel Interface Control 1-----DIVI[1:0], RTNI[4:0]
192 LCD_WriteCOM(0x92, 0x0000); // Panel Interface Control 2-----NOWI[2:0]
193
194 LCD_WriteCOM(0x93, 0x0001); //Panel Interface Control 3-----MCPI[2:0]
195
196 LCD_WriteCOM(0x95, 0x0110); // Panel Interface Control 4-----DIVE[1:0], RTNE[5:0]
197 LCD_WriteCOM(0x97, 0x0000); // Panel Interface Control 5-----NOWE[3:0]
198 LCD_WriteCOM(0x98, 0x0000); // Panel Interface Control 6-----MCPE[2:0]
199
200 LCD_WriteCOM(0x07, 0x0173); // Display Control 1----- display ON
201 LCD_Delay(500);
202
203 }