⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tft018.c

📁 LPC21XX系列的LCD中文显示源程序DEMO C语言Keil环境
💻 C
字号:
/*

                         \\\|///
                       \\  - -  //
                        (  @ @  )
+---------------------oOOo-(_)-oOOo-------------------------+
|                                                           |
|                        TFT018.c                           |
|                     by Xiaoran Liu                        |
|                        2008.3.16                          |
|                                                           |
|                    ZERO research group                    |
|                        www.the0.net                       |
|                                                           |
|                            Oooo                           |
+----------------------oooO--(   )--------------------------+
                      (   )   ) /
                       \ (   (_/
                        \_)     

*/
// 引入相关芯片的头文件 
#include <LPC214X.h>
//#include "LPC2103Z.h"
#include "TFT018.h"
#include "MyType.h"

volatile U8 Font=0;

/*----------------------------------------------------------*\
 | Backlight Initialize                                     |
\*----------------------------------------------------------*/
void BacklightOn(void) {
	PINSEL1 &= 0xFFFFF3FF;
	PINSEL1 |= 0x00000400;		// 设置PWM5连接到P0.21管脚
	PWMPR = 0x00;			    // 不分频,计数频率为Fpclk
	PWMMR0 = 50;				// 设置PWM周期,PWMMR0=50
	PWMMR5 = 6;				// 设置PWM占空比,PWMMR5=8
	PWMLER = 0x20;				// PWMMR0、PWMMR5锁存,更新PWM占空比
	PWMPCR = 0x2000;			// 允许PWM5输出,单边PWM
	PWMTCR = 0x09;			    // 启动定时器,PWM使能
	}

/*----------------------------------------------------------*\
 | Delay                                                    |
\*----------------------------------------------------------*/
void Delay1Ms(unsigned int tmr) {
	while( tmr ) {
		unsigned int i;
		for(i=0;i<10;i++);
		tmr--;
		}
	}
/*----------------------------------------------------------*\
 | BUS simulate                                             |
\*----------------------------------------------------------*/

/* ------ LOW LEVEL OPERATE ------ */
void LCD_Bus_Initial( void ) {
	//PINSEL2 = 0x00000004;
	//SCS=0x03;		//设置快速FGPIO
	//FIO0MASK = ~(LCD_BUS_RST | LCD_BUS_CS | LCD_BUS_RD);	// 允许快速I/O
	IO0DIR = LCD_BUS_RST | LCD_BUS_CS | LCD_BUS_RD;
	//FIO1MASK = ~(LCD_BUS_RS | LCD_BUS_RW | LCD_BUS_DAT);	// 允许快速I/O
	IO1DIR = LCD_BUS_RS | LCD_BUS_RW;

	IO0CLR = LCD_BUS_RST;
	Delay1Ms( 20 );
	IO0SET = LCD_BUS_RST | LCD_BUS_CS | LCD_BUS_RD;
	IO1SET = LCD_BUS_RS | LCD_BUS_RW;
	}

void LCD_Bus_Write_Byte( unsigned char Data ) {
	unsigned int d = (((unsigned int)Data)<<16) & LCD_BUS_DAT;
	IO1CLR = LCD_BUS_DAT;	// data on bus
	IO1SET = d;
	//IO1PIN = ((unsigned int)Data)<<16;
	IO1CLR =  LCD_BUS_RW;	// RW to LOW
	//Delay1Ms( 1 );
	IO1SET =  LCD_BUS_RW;	// RW to HIGH
	}

void LCD_Bus_Write_Data( unsigned short Data ) {
	LCD_Bus_Write_Byte( Data>>8 );
	LCD_Bus_Write_Byte( Data );
	}

unsigned int LCD_Bus_Read_Byte( void ) {
	unsigned int BusData;

	IO0CLR =  LCD_BUS_RD;	// RD to LOW
	BusData = IO1PIN;
	Delay1Ms( 1 );
	IO0SET =  LCD_BUS_RD;	// RD to HIGH

	return (BusData>>16)&0xFF;
	}

void LCD_Inst(unsigned int Cmd) {
	IO1DIR |= LCD_BUS_DAT;

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;

	IO1CLR =  LCD_BUS_RS;
	LCD_Bus_Write_Byte( Cmd );

	IO0SET =  LCD_BUS_CS;

	IO1DIR &= ~LCD_BUS_DAT;
	}

void LCD_Data(unsigned int Data) {
	IO1DIR |= LCD_BUS_DAT;

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;

	IO1SET =  LCD_BUS_RS;
	LCD_Bus_Write_Byte( Data );

	IO0SET =  LCD_BUS_CS;

	IO1DIR &= ~LCD_BUS_DAT;
	}

void LCD_Reg_Set(unsigned int Cmd,unsigned int Data) {
	IO1DIR |= LCD_BUS_DAT;

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;

	IO1CLR =  LCD_BUS_RS;
	LCD_Bus_Write_Byte( Cmd );

	IO1SET =  LCD_BUS_RS;
	LCD_Bus_Write_Byte( Data );

	IO0SET =  LCD_BUS_CS;

	IO1DIR &= ~LCD_BUS_DAT;
	}

/*----------------------------------------------------------*\
 | LCD Initialize                                           |
\*----------------------------------------------------------*/
void LCD_Init( void ) {
	LCD_Bus_Initial();

	LCD_Inst(0x11);	// sleep out/power on(SLPOUT)
	Delay1Ms(150);
	LCD_Inst(0x20);	// display inversion off
	LCD_Inst(0x38);	// ID mode off (IDMOFF)
	LCD_Inst(0x13);	// Normal display mode on (Partial mode off)
	// color mode Interface pixel format  (COLMOD)
	LCD_Reg_Set(0x3A,0x05);	// 16-bit/pixel , 1-times data transfer
	LCD_Reg_Set(0xC0,0x03);	// power control 1      (PWCTR1) , GVDD voltage set 4.65
	LCD_Reg_Set(0xC1,0x05);	// VCOMH voltage set 4.10V
	Delay1Ms(20);
	LCD_Reg_Set(0xC5,0xBA);	// VCOMAC voltage set 4.35V
	LCD_Reg_Set(0xC6,0x10);	// VCOMAC voltage set 4.35V
	Delay1Ms(20);
	// Gamma voltage adjustalbe level
	//Gamma + Polarity correction characteristics set
	LCD_Inst(0xE0);
	LCD_Data(0x01);
	LCD_Data(0x0A);
	LCD_Data(0x11);
	LCD_Data(0x23);
	LCD_Data(0x20);
	LCD_Data(0x01);
	LCD_Data(0x1E);
	LCD_Data(0x02);
	LCD_Data(0x06);
	LCD_Data(0x05);
	LCD_Data(0x0E);
	LCD_Data(0x0D);
	LCD_Data(0x01);
	LCD_Data(0x05);
	LCD_Data(0x03);
	LCD_Data(0x06);
	//Gamma + Polarity correction characteristics set
	LCD_Inst(0xE1);
	LCD_Data(0x01);
	LCD_Data(0x20);
	LCD_Data(0x23);
	LCD_Data(0x11);
	LCD_Data(0x0A);
	LCD_Data(0x01);
	LCD_Data(0x02);
	LCD_Data(0x1E);
	LCD_Data(0x06);
	LCD_Data(0x03);
	LCD_Data(0x05);
	LCD_Data(0x01);
	LCD_Data(0x0D);
	LCD_Data(0x0E);
	LCD_Data(0x05);
	LCD_Data(0x06);
	// memory data access control  (MADCTR)
	LCD_Reg_Set(0x36,0xA0);	// MY=1; MX=0; MV=0; ML=0; RGB=0
	LCD_Inst(0x37);
	LCD_Data(0x00);
	LCD_Data(0x00);
	// display on
	LCD_Inst(0x29);
	}

void LCD_SetArea( U8 x0, U8 y0, U8 x1, U8 y1 ) {
	LCD_Inst(0x2A);
	LCD_Data(0x00);
	LCD_Data(x0+1);
	LCD_Data(0x00);
	LCD_Data(x1+1);

	LCD_Inst(0x2B);
	LCD_Data(0x00);
	LCD_Data(y0+1);
	LCD_Data(0x00);
	LCD_Data(y1+1);
	}


void LCD_Rectangle( unsigned short x0, unsigned short y0, unsigned short x1, unsigned short y1, unsigned int Data ) {
	int x,y;

	LCD_SetArea(x0,y0,x1,y1);

	LCD_Inst(0x2C);

	IO1DIR |= LCD_BUS_DAT;

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;

	IO1SET =  LCD_BUS_RS;
	for(x=0;x<=x1-x0;x++)
		for(y=0;y<=y1-y0;y++) {
			LCD_Bus_Write_Byte( Data>>8 );
			LCD_Bus_Write_Byte( Data );
			}

	IO0SET =  LCD_BUS_CS;

	IO1DIR &= ~LCD_BUS_DAT;
	}

void LCD_Clear_Screen( unsigned int Data ) {
	LCD_Rectangle(0,0,159,128, Data );
	}

void LCD_SetPixel(unsigned short x, unsigned short y, unsigned short color) {
	LCD_SetArea(x,y,x,y);

	IO1DIR |= LCD_BUS_DAT;

	LCD_Inst(0x2C);

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;

	LCD_Bus_Write_Byte( color>>8 );
	LCD_Bus_Write_Byte( color );

	IO0SET =  LCD_BUS_CS;

	IO1DIR &= ~LCD_BUS_DAT;
	}


#include "8X16.h"

void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int f, unsigned int b) {
	register unsigned int i,j;

	LCD_SetArea(x,y,x+8-1,y+16-1);
	LCD_Inst(0x2C);

	IO1DIR |= LCD_BUS_DAT;

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;
	IO1SET =  LCD_BUS_RS;

	for(i=0; i<16;i++) {
		unsigned char m=Font8x16[c*16+i];
		for(j=0;j<8;j++) {
			if((m&0x80)==0x80) {
				LCD_Bus_Write_Byte(f>>8);
				LCD_Bus_Write_Byte(f);
				}
			else {
				LCD_Bus_Write_Byte(b>>8);
				LCD_Bus_Write_Byte(b);
				}
			m<<=1;
			}
		}

	IO0SET =  LCD_BUS_CS;
	IO1DIR &= ~LCD_BUS_DAT;
	}

#include "8X8h.h"

void LCD_PutChar8x8(unsigned short x, unsigned short y, char c, unsigned int f, unsigned int b) {
	register unsigned int i,j;

	LCD_SetArea(x,y,x+8-1,y+8-1);
	LCD_Inst(0x2C);

	IO1DIR |= LCD_BUS_DAT;

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;
	IO1SET =  LCD_BUS_RS;

	for(i=0; i<8;i++) {
		unsigned char m=Font8x8[c][i];
		for(j=0;j<8;j++) {
			if((m&0x80)==0x80) {
				LCD_Bus_Write_Byte(f>>8);
				LCD_Bus_Write_Byte(f);
				}
			else {
				LCD_Bus_Write_Byte(b>>8);
				LCD_Bus_Write_Byte(b);
				}
			m<<=1;
			}
		}

	IO0SET =  LCD_BUS_CS;
	IO1DIR &= ~LCD_BUS_DAT;
	}

void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int f, unsigned int b) {
	if( Font )
		LCD_PutChar8x8( x, y, c, f, b );
	else
		LCD_PutChar8x16( x, y, c, f, b );
	}

#include "GB1616.h"	//16*16汉字字模

void PutGB1616(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int f,unsigned int b){
	unsigned int i,j,k;

	LCD_SetArea(x, y, x+16-1, y+16-1);
	LCD_Inst(0x2C);

	IO1DIR |= LCD_BUS_DAT;

	IO0CLR =  LCD_BUS_CS;
	IO0SET =  LCD_BUS_RD;
	IO1SET =  LCD_BUS_RS;

	for (k=0;k<49;k++) {
	  if ((codeGB_16[k].Index[0]==c[0])&&(codeGB_16[k].Index[1]==c[1])){ 
    	for(i=0;i<32;i++) {
		  unsigned short m=codeGB_16[k].Msk[i];
		  for(j=0;j<8;j++) {
			if((m&0x80)==0x80) {
				LCD_Bus_Write_Byte(f>>8);
				LCD_Bus_Write_Byte(f);
				}
			else {
				LCD_Bus_Write_Byte(b>>8);
				LCD_Bus_Write_Byte(b);
				}
			m<<=1;
			} 
		  }
		}  
	  }	

	IO0SET =  LCD_BUS_CS;
	IO1DIR &= ~LCD_BUS_DAT;
	}

void LCD_PutString(unsigned short x, unsigned short y, char *s, unsigned int f, unsigned int b) {
	register unsigned char l=0;
	while(*s) {
		if( *s < 0x80 ) {
			LCD_PutChar(x+l*8,y,*s,f,b);
			s++;l++;
			}
		else {
			PutGB1616(x+l*8,y,(unsigned char *)s,f,b);
			s+=2;l+=2;
			}
		}
	}

/*----------------------------------------------------------*\
 | END OF FILE                                              |
\*----------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -