1 | #define _SUPPRESS_PLIB_WARNING 1
|
2 |
|
3 | #include <p32xxxx.h>
|
4 | #include <stdio.h>
|
5 | #include <stdlib.h>
|
6 | #include <plib.h>
|
7 |
|
8 |
|
9 | #define color_brown 0x40c0
|
10 | #define color_black 0x0000
|
11 | #define color_white 0xffff
|
12 | #define color_red 0xf800
|
13 | #define color_green 0x07e0
|
14 | #define color_blue 0x001f
|
15 | #define color_yellow color_red|color_green
|
16 | #define color_cyan color_green|color_blue
|
17 | #define color_purple color_red|color_blue
|
18 |
|
19 |
|
20 | // Configuration Bit settings
|
21 |
|
22 | // SYSCLK = 80 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
|
23 | // PBCLK = 80 MHz (SYSCLK / FPBDIV)
|
24 | // Primary Osc w/PLL (XT+,HS+,EC+PLL)
|
25 | // WDT OFF
|
26 | // Other options are don't care
|
27 | #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
|
28 | #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1 // Primary Oszillator -> HS: High Speed crystal
|
29 | #define SYS_FREQ (80000000L)
|
30 |
|
31 | // SPI
|
32 | #define LCD_CS_OUT mPORTBSetPinsDigitalOut(BIT_12)
|
33 | #define LCD_CS_0 mPORTBClearBits(BIT_12)
|
34 | #define LCD_CS_1 mPORTBSetBits(BIT_12)
|
35 |
|
36 |
|
37 |
|
38 | void Delay1ms(unsigned int i)
|
39 | {
|
40 | unsigned char j;
|
41 | while(i--)
|
42 | for(j=0;j<125;j++);
|
43 | }
|
44 |
|
45 |
|
46 | void Delay10ms(unsigned int i)
|
47 | { while(i--)
|
48 | Delay1ms(10);
|
49 | }
|
50 |
|
51 | void Delay100ms(unsigned int i)
|
52 | { while(i--)
|
53 | Delay1ms(100);
|
54 | }
|
55 |
|
56 | //*********4W_SPI_Write()
|
57 | unsigned char SPI_Write(unsigned char dat)
|
58 | {
|
59 | SPI1BUF = dat; // write to shift register to begin transmission
|
60 | while( !SPI1STATbits.SPIRBF); // wait for transfer to complete
|
61 | return SPI1BUF;
|
62 | }
|
63 |
|
64 | //////////////SPI Write command
|
65 | void LCD_CmdWrite(unsigned char cmd)
|
66 | {
|
67 |
|
68 | LCD_CS_0;
|
69 | //SPI_Delay();
|
70 | SPI_Write(0x80);
|
71 | SPI_Write(cmd);
|
72 | LCD_CS_1;
|
73 | }
|
74 |
|
75 | //////////////SPI Write data or parameter
|
76 | void LCD_DataWrite(unsigned char Data)
|
77 | {
|
78 | LCD_CS_0;
|
79 | SPI_Write(0x00);
|
80 | SPI_Write(Data);
|
81 | LCD_CS_1;
|
82 | }
|
83 |
|
84 |
|
85 | ///////////////Background color settings
|
86 | void Text_Background_Color1(unsigned int b_color)
|
87 | {
|
88 |
|
89 | LCD_CmdWrite(0x60);//BGCR0
|
90 | LCD_DataWrite((unsigned char)(b_color>>11));
|
91 |
|
92 | LCD_CmdWrite(0x61);//BGCR0
|
93 | LCD_DataWrite((unsigned char)(b_color>>5));
|
94 |
|
95 | LCD_CmdWrite(0x62);//BGCR0
|
96 | LCD_DataWrite((unsigned char)(b_color));
|
97 | }
|
98 |
|
99 |
|
100 | ////////////////Write command and parameter
|
101 | void Write_Dir(unsigned char Cmd,unsigned char Data)
|
102 | {
|
103 | LCD_CmdWrite(Cmd);
|
104 | LCD_DataWrite(Data);
|
105 | }
|
106 |
|
107 | /////////////PLL setting
|
108 | void PLL_ini(void)
|
109 | {
|
110 | LCD_CmdWrite(0x88);
|
111 | LCD_DataWrite(0x0a);
|
112 |
|
113 | Delay1ms(1);
|
114 | LCD_CmdWrite(0x89);
|
115 | LCD_DataWrite(0x02);
|
116 | Delay1ms(1);
|
117 | }
|
118 |
|
119 | /////////////LCM initial
|
120 | void LCD_Initial(void)
|
121 | {
|
122 |
|
123 | PLL_ini();
|
124 | LCD_CmdWrite(0x10); //SYSR bit[4:3]=00 256 color bit[2:1]= 00 8bit MPU interface
|
125 | LCD_DataWrite(0x0C); // 1x 64k color 1x 16bit
|
126 |
|
127 | LCD_CmdWrite(0x04); //PCLK
|
128 | LCD_DataWrite(0x82); //
|
129 | // DelayMs(1);
|
130 | Delay1ms(1);
|
131 |
|
132 | //Horizontal set
|
133 | LCD_CmdWrite(0x14); //HDWR//Horizontal Display Width Setting Bit[6:0]
|
134 | LCD_DataWrite(0x3b); //Horizontal display width(pixels) = (HDWR + 1)*8 0x27
|
135 | LCD_CmdWrite(0x15); //HNDFCR//Horizontal Non-Display Period fine tune Bit[3:0]
|
136 | LCD_DataWrite(0x02); //(HNDR + 1)*8 +HNDFCR
|
137 | LCD_CmdWrite(0x16); //HNDR//Horizontal Non-Display Period Bit[4:0]
|
138 | LCD_DataWrite(0x03); //Horizontal Non-Display Period (pixels) = (HNDR + 1)*8
|
139 | LCD_CmdWrite(0x17); //HSTR//HSYNC Start Position[4:0]
|
140 | LCD_DataWrite(0x01); //HSYNC Start Position(PCLK) = (HSTR + 1)*8
|
141 | LCD_CmdWrite(0x18); //HPWR//HSYNC Polarity ,The period width of HSYNC.
|
142 | LCD_DataWrite(0x03); //HSYNC Width [4:0] HSYNC Pulse width(PCLK) = (HPWR + 1)*8
|
143 |
|
144 | //Vertical set
|
145 | LCD_CmdWrite(0x19); //VDHR0 //Vertical Display Height Bit [7:0]
|
146 | LCD_DataWrite(0x0f); //Vertical pixels = VDHR + 1 0xef
|
147 | LCD_CmdWrite(0x1a); //VDHR1 //Vertical Display Height Bit [8]
|
148 | LCD_DataWrite(0x01); //Vertical pixels = VDHR + 1 0x00
|
149 | LCD_CmdWrite(0x1b); //VNDR0 //Vertical Non-Display Period Bit [7:0]
|
150 | LCD_DataWrite(0x0F); //Vertical Non-Display area = (VNDR + 1)
|
151 | LCD_CmdWrite(0x1c); //VNDR1 //Vertical Non-Display Period Bit [8]
|
152 | LCD_DataWrite(0x00); //Vertical Non-Display area = (VNDR + 1)
|
153 | LCD_CmdWrite(0x1d); //VSTR0 //VSYNC Start Position[7:0]
|
154 | LCD_DataWrite(0x0e); //VSYNC Start Position(PCLK) = (VSTR + 1)
|
155 | LCD_CmdWrite(0x1e); //VSTR1 //VSYNC Start Position[8]
|
156 | LCD_DataWrite(0x06); //VSYNC Start Position(PCLK) = (VSTR + 1)
|
157 | LCD_CmdWrite(0x1f); //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]
|
158 | LCD_DataWrite(0x01); //VSYNC Pulse Width(PCLK) = (VPWR + 1)
|
159 |
|
160 | LCD_CmdWrite(0x8a);//PWM setting
|
161 | LCD_DataWrite(0x80);
|
162 | LCD_CmdWrite(0x8a);//PWM setting
|
163 | LCD_DataWrite(0x81);//open PWM
|
164 | LCD_CmdWrite(0x8b);//Backlight brightness setting
|
165 | LCD_DataWrite(0xff);//Brightness parameter 0xff-0x00
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 | void NextStep(void)
|
171 | {
|
172 | Delay1ms(100);
|
173 | Delay1ms(100);
|
174 | Delay1ms(100);
|
175 | }
|
176 |
|
177 |
|
178 | //full display test
|
179 | void Test(void)
|
180 | { ///display red
|
181 | Text_Background_Color1(color_red);//Background color setting
|
182 | Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
183 | NextStep();
|
184 | ///display green
|
185 | Text_Background_Color1(color_green);//Background color setting
|
186 | Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
187 | NextStep();
|
188 | ///display blue
|
189 | Text_Background_Color1(color_blue);//Background color setting
|
190 | Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
191 | NextStep();
|
192 | ///display white
|
193 | Text_Background_Color1(color_white);//Background color setting
|
194 | Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
195 | NextStep();
|
196 | ///display cyan
|
197 | Text_Background_Color1(color_cyan);//Background color setting
|
198 | Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
199 | NextStep();
|
200 | ///display yellow
|
201 | Text_Background_Color1(color_yellow);//Background color setting
|
202 | Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
203 | NextStep();
|
204 | ///display purple
|
205 | Text_Background_Color1(color_purple);//Background color setting
|
206 | Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
207 | NextStep();
|
208 | ///display black
|
209 | Text_Background_Color1(color_blue);//Background color setting
|
210 | // Write_Dir(0x8E,0x80);//Began to clear the screen (display window)
|
211 | NextStep();
|
212 | }
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | /*
|
218 | Initialize SPI module in pic32
|
219 | */
|
220 | void InitSPI(void)
|
221 | {
|
222 | // 8 Bit, Master-Mode, eanable SPI-1
|
223 | // nano_CS_LAT_BIT=1; //nano deselected
|
224 | // OpenSPI1(SPI_MODE16_ON | MASTER_ENABLE_ON , SPI_ENABLE);
|
225 |
|
226 | /* Idle state for clock is high, active is low
|
227 | /* Transmit happens from active clock state to idle clock state*/
|
228 |
|
229 |
|
230 | // OpenSPI1(SPI_MODE8_ON | MASTER_ENABLE_ON | CLK_POL_ACTIVE_LOW | SPI_CKE_ON, SPI_ENABLE);
|
231 | OpenSPI1(SPI_MODE8_ON | MASTER_ENABLE_ON | CLK_POL_ACTIVE_LOW | SPI_CKE_ON | SEC_PRESCAL_1_1 | PRI_PRESCAL_4_1 | SPI_SMP_ON , SPI_ENABLE);
|
232 | // OpenSPI1(SPI_MODE8_ON | MASTER_ENABLE_ON | CLK_POL_ACTIVE_HIGH | SPI_CKE_ON | SEC_PRESCAL_8_1 | PRI_PRESCAL_16_1, SPI_ENABLE);
|
233 | ConfigIntSPI1(SPI_TX_INT_EN | SPI_RX_INT_EN | SPI_FAULT_INT_EN);
|
234 | // EnableIntSPI1;
|
235 | }
|
236 | //
|
237 | ////////////////Foreground color settings
|
238 | void Text_Foreground_Color1(unsigned int b_color)
|
239 | {
|
240 |
|
241 | LCD_CmdWrite(0x63);//BGCR0
|
242 | LCD_DataWrite((unsigned char)(b_color>>11));
|
243 |
|
244 | LCD_CmdWrite(0x64);//BGCR0
|
245 | LCD_DataWrite((unsigned char)(b_color>>5));
|
246 |
|
247 | LCD_CmdWrite(0x65);//BGCR0
|
248 | LCD_DataWrite((unsigned char)(b_color));
|
249 | }
|
250 |
|
251 |
|
252 | /*
|
253 | *
|
254 | */
|
255 | int main() {
|
256 | LCD_CS_OUT;
|
257 | InitSPI();
|
258 | LCD_CS_1;
|
259 |
|
260 | Delay100ms(5);
|
261 |
|
262 | LCD_Initial();
|
263 |
|
264 | Write_Dir(0x01,0x80);//display on
|
265 |
|
266 | while(1)
|
267 | {
|
268 | //full display test
|
269 | Test();
|
270 | Text_Foreground_Color1(color_white);//Set the foreground color
|
271 | Text_Background_Color1(color_black);//Set the background color
|
272 |
|
273 | }
|
274 |
|
275 |
|
276 | }
|