- #include<AI8051U.h>
- sbit SCLK=P1^0;
- sbit MOSI=P1^1;
- sbit MISO=P1^2;
- sbit CS=P1^3;
- sbit RES=P1^4;
- sbit DC=P1^5;
- void Delay(unsigned long int t) { while(--t){_nop_();} }
- void Gpio_init(void)
- {
- P0M0=0x00;P0M1=0x00;
- P1M0=0xff;P1M1=0x00;
- P2M0=0x00;P2M1=0x00;
- P3M0=0x00;P3M1=0x00;
- P4M0=0x00;P4M1=0x00;
- P5M0=0x00;P5M1=0x00;
- }
- void SpiSendBit(bit dat)
- {
- MOSI = dat;
- SCLK = 1;
- _nop_(); _nop_(); _nop_();
- SCLK = 0;
- }
- void SpiSendByte(unsigned char dat)
- {
- unsigned char j;
- for(j=0x80; j; j>>=1)
- {
- SpiSendBit(dat & j);
- }
- }
- void Lcd_Cmd(unsigned char cmd)
- {
- CS = 0;
- DC = 0;
- SpiSendByte(cmd);
- CS = 1;
- }
- void Lcd_Cmd_Ncs(unsigned char cmd)
- {
- DC = 0;
- SpiSendByte(cmd);
- }
- void Lcd_Data(unsigned char dat)
- {
- CS = 0;
- DC = 1;
- SpiSendByte(dat);
- CS = 1;
- }
- void Lcd_Data_Ncs(unsigned char dat)
- {
- DC = 1;
- SpiSendByte(dat);
- }
- void Lcd_Init(void)
- {
- RES = 0;
- Delay(50000);
- RES = 1;
- Delay(1000000);
- /* ---- 1. 软件复位 ---- */
- Lcd_Cmd(0x01); // Software Reset
- Delay(70000); // 复位后延时等待
-
- /* ---- 7. 退出睡眠,开启显示 ---- */
- Lcd_Cmd(0x11); // Sleep OUT
- Delay(1000000); // 退出睡眠后需延时
- /* ---- 2. 电源控制 ---- */
- Lcd_Cmd(0xC0); // Power Control 1
- Lcd_Data(0x21); // VRH[5:0] = 21h(参考命令表默认)
-
- Lcd_Cmd(0xC1); // Power Control 2
- Lcd_Data(0x10); // BT[2:0] = 0(命令表默认 10h)
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0xC5); // VCOM Control 1
- Lcd_Data_Ncs(0x31); // VMH[6:0] = 31h
- Lcd_Data_Ncs(0x3C); // VML[6:0] = 3Ch
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- Lcd_Cmd(0xC7); // VCOM Control 2
- Lcd_Data(0xC0); // nVM=1, VMF 参考命令表
-
- /* ---- 3. 时序 / 帧率控制 ---- */
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0xB1); // Frame Control (Normal Mode)
- Lcd_Data_Ncs(0x00); // DIVA[1:0] = 00
- Lcd_Data_Ncs(0x1B); // RTNA[4:0] = 1Bh
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0xB6); // Display Function Control
- Lcd_Data_Ncs(0x0A); // PTG[1:0]=00, PT[1:0]=10
- Lcd_Data_Ncs(0x82); // REV=1, GS=0, SS=0, SM=0, ISC=0010
- Lcd_Data_Ncs(0x27); // NL[5:0] = 27h(240行)
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- /* ---- 4. 像素格式设定 ---- */
- Lcd_Cmd(0x3A); // Pixel Format Set
- Lcd_Data(0x55); // DPI=110(16bit), DBI=110(16bit) -> RGB565
-
- /* ---- 5. 内存访问控制(显示方向 / RGB顺序)---- */
- Lcd_Cmd(0x36); // Memory Access Control
- Lcd_Data(0x08); // MY=0,MX=0,MV=0,ML=0,BGR=1,MH=0
-
- /* ---- 6. 正负伽马校正 ---- */
- // Positive Gamma Correction
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0xE0);
- Lcd_Data_Ncs(0x0F); Lcd_Data_Ncs(0x16); Lcd_Data_Ncs(0x14);
- Lcd_Data_Ncs(0x0A); Lcd_Data_Ncs(0x0D); Lcd_Data_Ncs(0x06);
- Lcd_Data_Ncs(0x43); Lcd_Data_Ncs(0x75); Lcd_Data_Ncs(0x33);
- Lcd_Data_Ncs(0x06); Lcd_Data_Ncs(0x0E); Lcd_Data_Ncs(0x00);
- Lcd_Data_Ncs(0x0C); Lcd_Data_Ncs(0x09); Lcd_Data_Ncs(0x08);
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- // Negative Gamma Correction
- Lcd_Cmd_Ncs(0xE1);
- Lcd_Data_Ncs(0x08); Lcd_Data_Ncs(0x2B); Lcd_Data_Ncs(0x2D);
- Lcd_Data_Ncs(0x04); Lcd_Data_Ncs(0x10); Lcd_Data_Ncs(0x04);
- Lcd_Data_Ncs(0x3E); Lcd_Data_Ncs(0x24); Lcd_Data_Ncs(0x4E);
- Lcd_Data_Ncs(0x04); Lcd_Data_Ncs(0x0F); Lcd_Data_Ncs(0x0E);
- Lcd_Data_Ncs(0x35); Lcd_Data_Ncs(0x38); Lcd_Data_Ncs(0x0F);
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
-
- Lcd_Cmd(0x29); // Display ON
- }
- /* 全屏显示 RGB565 图片(240x320),高字节在前,一维数组 */
- void Display(const unsigned char img[])
- {
- unsigned long i;
-
- /* 设置全屏写入窗口 */
- CS=0;
- Lcd_Cmd_Ncs(0x2A); /* Column Address Set */
- Lcd_Data_Ncs(0x00); Lcd_Data_Ncs(0x00); /* SC = 0 */
- Lcd_Data_Ncs(0x00); Lcd_Data_Ncs(0xEF); /* EC = 239 */
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0x2B); /* Page Address Set */
- Lcd_Data_Ncs(0x00); Lcd_Data_Ncs(0x00); /* SP = 0 */
- Lcd_Data_Ncs(0x01); Lcd_Data_Ncs(0x3F); /* EP = 319 */
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0x2C); /* Memory Write */
-
- for (i = 0; i < 240 * 320; i++)
- {
- Lcd_Data_Ncs(img[i * 2]); /* 高字节 */
- Lcd_Data_Ncs(img[i * 2 + 1]); /* 低字节 */
- }
- CS=1;
- }
- void Test(void)
- {
- unsigned long i;
-
- /* 设置全屏写入窗口 */
- CS=0;
- Lcd_Cmd_Ncs(0x2A); /* Column Address Set */
- Lcd_Data_Ncs(0x00); Lcd_Data_Ncs(0x00); /* SC = 0 */
- Lcd_Data_Ncs(0x00); Lcd_Data_Ncs(0xEF); /* EC = 239 */
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0x2B); /* Page Address Set */
- Lcd_Data_Ncs(0x00); Lcd_Data_Ncs(0x00); /* SP = 0 */
- Lcd_Data_Ncs(0x01); Lcd_Data_Ncs(0x3F); /* EP = 319 */
- CS=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- CS=0;
- Lcd_Cmd_Ncs(0x2C); /* Memory Write */
-
- for (i = 0; i < 240 * 320; i++)
- {
- Lcd_Data_Ncs(0xf8);
- Lcd_Data_Ncs(0x00);
- }
- CS=1;
- }
- void main()
- {
- WDT_CONTR = 0x00;
- Gpio_init();
- Lcd_Init();
- Test();
- while(1);
- }
复制代码
颜色经过修改后对了 |