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

📄 lcdtest.c

📁 TMS320F2812的液晶显示源程序 TMS320F2812的液晶显示源程序
💻 C
字号:

#include "math.h"

static unsigned int PSystemSet[]={	//8 parameters of system set command.
//					0x30,0x87,0x07,0x27,0x2b,0x0f0,0x28,0x00,};
					0x30,0x87,0x07,0x28,0x2f,0x0f0,0x28,0x00,};
static unsigned int PScroll[]={		//10 parameters of scroll command.
//					0x00,0x00,0x0f0,0x80,0x25,0x0f0,0x00,0x4b,0x00,0x00};
					0x00,0x00,0x0f0,0x00,0x40,0x0f0,0x00,0x80,0x00,0x00};
static unsigned int PCsrBlock[] = {0x07,0x87};
static unsigned int PCsrLine[] = {0x07,0x07};

static unsigned int PDemo[] = {
					0x53,0x65,0x76,0x69,0x63,0x65,0x72,0x20,0x54,0x45,0x4c,0x3a,
					0x00,0x30,0x31,0x30,0x36,0x32,0x37,0x38,0x30,0x38,0x36,0x36};
static unsigned int PDemo1[] = {
					0x11,0x11,0x11,0x23,0x22,0x64,0x0A8,0x20,	//你
					0x00,0x00,0x00,0x0FC,0x04,0x08,0x40,0x40,					
					0x21,0x21,0x22,0x24,0x20,0x20,0x21,0x20,
					0x50,0x48,0x4C,0x44,0x40,0x40,0x40,0x80,
					
					0x10,0x11,0x10,0x10,0x0FC,0x24,0x24,0x27,	//好
					0x00,0x0FC,0x04,0x08,0x10,0x20,0x24,0x0FE,
					0x24,0x44,0x28,0x10,0x28,0x44,0x84,0x00,
					0x20,0x20,0x20,0x20,0x20,0x20,0x0A0,0x40,
					};
					

ioport unsigned int port8;		//LCD #reset
ioport unsigned int port6;
ioport unsigned int port7;

#define LCDRESET	port8
#define LCDPORTDATA	port6
#define LCDPORTCMD	port7	

//all command code define:
#define LCD_SYSTEMSET	0x40
#define LCD_SCROLL		0x44
#define LCD_SLEEPIN		0x53
#define LCD_DISPON		0x59
#define LCD_DISPOFF		0x58
#define LCD_OVLAY		0x5B
#define LCD_HDOTSCR		0x5A
#define LCD_CSRFORM		0x5D
#define LCD_CGRAMADDR	0x5C
#define LCD_CSRUP		0x4E
#define LCD_CSRDOWN		0x4F
#define LCD_CSRLEFT		0x4D
#define LCD_CSRRIGHT	0x4C
#define LCD_CSRW		0x46
#define LCD_CSRR		0x47
#define LCD_MWRITE		0x42
#define LCD_MREAD		0x43

#define LCD_MAXX		320
#define LCD_MAXY		240
#define LCD_SET			1
#define LCD_CLR			2
#define LCD_TOG			3
#define PI				3.1415926

#define LCDDELAY		Delay_ns(10);

void LCDInit(void);
void LCDDemo(void);
void LCDDemo1(void);
int dot(int, int, int);
int line(int, int, int, int, int );
int loadchinese(unsigned ,unsigned int *,int);
int writechar(int x,int y,int codeno,int mode);
void CLEAR(void);
void Black(void);
void Delay_ns(int);
void Delay1s(void);

unsigned int vramValue[LCD_MAXY][LCD_MAXX/8];	//图形显示控制缓冲

void main(void){
	LCDInit();
	Black();
	while(1) {
		CLEAR();
		LCDDemo1();
	}
}


void LCDInit(void){
	int i,j;

	LCDRESET = 2;

	for(i=0;i<LCD_MAXY;i++)
		for(j=0;j<LCD_MAXX/8;j++)
			vramValue[i][j] = 0;
	
	
	for(i=0;i<1000;i++)
		LCDDELAY;	
	
	LCDPORTCMD = LCD_SYSTEMSET;
	for(i=0;i<8;i++){
		LCDDELAY;
		LCDPORTDATA = PSystemSet[i];
	}
	
	LCDDELAY;	
	LCDPORTCMD = LCD_SCROLL;
	for(i=0;i<10;i++){
		LCDDELAY;	
		LCDPORTDATA = PScroll[i];
		
	} 
	
	LCDDELAY;	
	LCDPORTCMD = LCD_HDOTSCR;
	LCDDELAY;	
	LCDPORTDATA = 0;
	
	LCDDELAY;	
	LCDPORTCMD = LCD_OVLAY;	
	LCDDELAY;	
	LCDPORTDATA = 0x0C;	
	
	CLEAR();
	
	LCDDELAY;	
	LCDPORTCMD = LCD_DISPON;	
	LCDDELAY;	
	LCDPORTDATA = 0x4;	//0X54
	

}

void LCDDemo(void){
	int i,x,y;
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRFORM;	
	for(i=0;i<2;i++){	//设置块状光标
		LCDDELAY;	
		LCDPORTDATA = PCsrBlock[i];
	}	
	
	LCDDELAY;	
	LCDPORTCMD = LCD_OVLAY;	
	LCDDELAY;	
	LCDPORTDATA = 1;	//设置一、三区为文本属性
	
	LCDDELAY;	
	LCDPORTCMD = LCD_DISPON;	
	LCDDELAY;	
	LCDPORTDATA = 0x56;	//设置一~四区开显示
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = 0;	//
	LCDDELAY;	
	LCDPORTDATA = 0;	//
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRRIGHT;	
	
	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	//
	
	for(i=0;i<24;i++){
		LCDDELAY;	
		LCDPORTDATA = PDemo[i];
	}
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRR;	//
	LCDDELAY;
	x = LCDPORTDATA;
	LCDDELAY;	
	y = LCDPORTDATA;	
}

void LCDDemo1(void){
	int i,j,x,y,yb,value;
	double dx,dy;
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRFORM;	
	for(i=0;i<2;i++){	//设置块状光标
		LCDDELAY;	
		LCDPORTDATA = PCsrBlock[i];
	}	
	
	LCDDELAY;	
	LCDPORTCMD = LCD_OVLAY;	
	LCDDELAY;	
	LCDPORTDATA = 9;	//设置一、为文本属性,三区为图形属性
	
	LCDDELAY;	
	LCDPORTCMD = LCD_DISPON;	
	LCDDELAY;	
	LCDPORTDATA = 0x15;	//设置一~四区开显示,三关闭

	loadchinese(0x3000,PDemo1,sizeof(PDemo1));	
//	写文字
	writechar(4,6,0x80,0);
	writechar(6,6,0x84,0);
	writechar(8,7,' ',0);	
	writechar(8,7,',',0);		
	writechar(8,7,'S',0);
	writechar(9,7,'W',0);
	writechar(10,7,'Q',0);
				
//	Delay1s();
//	画纵座标
	line(8,10,8,220,LCD_SET);
//	画下划线
	line(0,9,191,9,LCD_SET);
//	画横座标
	line(8,119,310,119,LCD_SET);
	
//	画曲线
	for(i=0;i<LCD_MAXX-20;i++){
		vramValue[i][0] = i+8;
		y = ((LCD_MAXY/2-20.0)*(sin(((double)(i))*PI/180.0)+1)) +20 ;
//		vramValue[i][1] = i/2-80;		
			value=dot(i+8,y,LCD_SET);
	}


	LCDDELAY;	
	LCDPORTCMD = LCD_CSRR;	//
	LCDDELAY;
	x = LCDPORTDATA;
	LCDDELAY;	
	y = LCDPORTDATA;	
}
/*
	画点子程序。在显示二区以图形方式画点。
	参数:
		x,y		为该点座标,以左上角为原点。座标正方向为向左、向下。
		mode	为填充模式,共有3种:
				LCD_SET		画黑点
				LCD_CLR		画白点
				LCD_TOG 	反转点
	返回:
		该点所在显存字节的值。
*/
int dot(int x, int y, int mode){
	int value;				

	if( x < 0 || x >= LCD_MAXX || y < 0 || y >= LCD_MAXY )
		return -1; 
				
/*	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
		
	LCDDELAY;	
	LCDPORTDATA = ((PScroll[4]<<8)+(y)*PSystemSet[6]+(x-x%8)/8) & 0x0ff ;	//
	LCDDELAY;	
	LCDPORTDATA = ((PScroll[4]<<8)+(y)*PSystemSet[6]+(x-x%8)/8) >> 8 ;	//

	LCDDELAY;	
	LCDPORTCMD = LCD_MREAD;	//

	LCDDELAY;	
	value = LCDPORTDATA ;
*/
	value = vramValue[y][(x-x%8)/8];
	switch (mode){
		case LCD_SET:
			value = value | (1<<(7-x%8));
			break;
		case LCD_CLR:
			value = value & (0x0ff-(1<<(7-x%8)));
			break;
		case LCD_TOG:
			value = value ^ (1<<(7-x%8));
			break;	
	}
	vramValue[y][(x-x%8)/8] = value;	
			
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = ((PScroll[4]<<8)+(y)*PSystemSet[6]+(x-x%8)/8) & 0x0ff ;	//
	LCDDELAY;	
	LCDPORTDATA = ((PScroll[4]<<8)+(y)*PSystemSet[6]+(x-x%8)/8) >> 8 ;	//

	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	//

	LCDDELAY;	
	LCDPORTDATA = value;


	LCDDELAY;	
	LCDPORTCMD = LCD_CSRR;	//
	LCDDELAY;
	value = LCDPORTDATA;
	LCDDELAY;	
	value = LCDPORTDATA;	

	return value;
}

/*
	画线子程序。在显示二区以图形方式画直线,必须是水平线或垂直线。
	参数:
		x1,y1	为直线起始点座标,以左上角为原点。座标正方向为向左、向下。
		x2,y3	为直线中止点座标,以左上角为原点。座标正方向为向左、向下。
		mode	为填充模式,共有3种:
				LCD_SET		画黑
				LCD_CLR		画白
				LCD_TOG 	反转
	返回:
		正常:  0
		异常:	-1
*/
int line(int x1, int y1, int x2, int y2, int mode){
	int i,value;
			
	if( x1 < 0 || x1 >= LCD_MAXX || y1 < 0 || y1 >= LCD_MAXY )
		return -1; 
	if( x2 < 0 || x2 >= LCD_MAXX || y2 < 0 || y2 >= LCD_MAXY )
		return -1; 
					
	if( x1 != x2 && y1 != y2 ) 	//非水平或垂直直线。
		return -1;
							
	if( x1 == x2 ){				//垂直直线
		if( y1 > y2 ){
			value = y1;
			y1 = y2;
			y2 = value;
		}
		switch (mode){
			case LCD_SET:
				for(i=y1;i<=y2;i++)
					vramValue[i][(x1-x1%8)/8] = vramValue[i][(x1-x1%8)/8] | (1<<(7-x1%8));
				break;
			case LCD_CLR:
				for(i=y1;i<=y2;i++)
					vramValue[i][(x1-x1%8)/8] = vramValue[i][(x1-x1%8)/8] & (0x0ff-(1<<(7-x1%8)));
				break;
			case LCD_TOG:
				for(i=y1;i<=y2;i++)
					vramValue[i][(x1-x1%8)/8] = vramValue[i][(x1-x1%8)/8] ^ (1<<(7-x1%8));
				break;	
		}
	}else if( y1 == y2 ){		//水平直线
		if( x1 > x2 ){
			value = x1;
			x1 = x2;
			x2 = value;
		}

		switch (mode){
			case LCD_SET:
				for(i=x1;i<=x2;i++)
					vramValue[y1][(i-i%8)/8] = vramValue[y1][(i-i%8)/8] | (1<<(7-i%8));
				break;
			case LCD_CLR:
				for(i=x1;i<=x2;i++)
					vramValue[y1][(i-i%8)/8] = vramValue[y1][(i-i%8)/8] & (0x0ff-(1<<(7-i%8)));
				break;
			case LCD_TOG:
				for(i=x1;i<=x2;i++)
					vramValue[y1][(i-i%8)/8] = vramValue[y1][(i-i%8)/8] ^ (1<<(7-i%8));
				break;	
		}
	}	
			
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = ((PScroll[4]<<8)+(y1)*PSystemSet[6]+(x1-x1%8)/8) & 0x0ff ;	//
	LCDDELAY;	
	LCDPORTDATA = ((PScroll[4]<<8)+(y1)*PSystemSet[6]+(x1-x1%8)/8) >> 8 ;	//

	if( x1 == x2 ){
		LCDDELAY;	
		LCDPORTCMD = LCD_CSRDOWN;	
	}else if( y1 == y2 ){
		LCDDELAY;	
		LCDPORTCMD = LCD_CSRRIGHT;		
	}
	
	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	//

	if( x1 == x2 ){
		for(i=y1;i<=y2;i++){
			LCDDELAY;	
			LCDPORTDATA = vramValue[i][(x1-x1%8)/8];		
		}		
	}else if( y1 == y2 ){
		for(i=(x1-x1%8)/8;i<=(x2+7-(x2+7)%8)/8;i++){
			LCDDELAY;	
			LCDPORTDATA = vramValue[y1][i];		
		}		
	}		


	LCDDELAY;	
	LCDPORTCMD = LCD_CSRR;	
	LCDDELAY;
	value = LCDPORTDATA;
	LCDDELAY;	
	value = LCDPORTDATA;	

	return 0;
}

/*
	写汉字子程序。在显示一区以文本方式写汉字。
	参数:
		x,y		为汉字所写位置座标,以左上角为原点。座标正方向为向左、向下。
		codeno	汉字代码序号,从0x80~0xbf,每个汉字占用四个序号,如0x80所表汉字
				占用0x80,0x81,0x82,0x83四个序号.0x80~0x9f序号共有8个汉字,字模
				占用1335中用户自定义字库一区;0xa0~0xbf序号共有8个汉字,字模
				占用1335中用户自定义字库二区.
		mode	为填充模式,共有3种:
				LCD_SET		画黑
				LCD_CLR		画白
				LCD_TOG 	反转
	返回:
		正常:  0
		异常:	-1
*/
int writechar(int x,int y,int codeno,int mode){
	int i;
	
	if( x < 0 || x >= PSystemSet[6] || y < 0 || y >= LCD_MAXY )
		return -1; 
	if( codeno > 0x0ff || codeno < 0 )
		return -1;
	
	if(codeno < 0x80){			//写ASCII字符
		LCDDELAY;	
		LCDPORTCMD = LCD_CSRW;	
		LCDDELAY;	
		LCDPORTDATA = (y*PSystemSet[6] + x) & 0x0ff;	//
		LCDDELAY;	
		LCDPORTDATA = (y*PSystemSet[6] + x) >> 8;	//
		
		LCDDELAY;	
		LCDPORTCMD = LCD_CSRRIGHT;	
		
		LCDDELAY;	
		LCDPORTCMD = LCD_MWRITE;	//
		LCDDELAY;			
		LCDPORTDATA = codeno;
					
		return 0;
	
	}
	//写汉字
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = (y*PSystemSet[6] + x) & 0x0ff;	//
	LCDDELAY;	
	LCDPORTDATA = (y*PSystemSet[6] + x) >> 8;	//
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRRIGHT;	
	
	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	//
	
	for(i=0;i<2;i++){
		LCDDELAY;	
		LCDPORTDATA = i+codeno;
	}
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = ((y+1)*PSystemSet[6] + x) & 0x0ff;	//
	LCDDELAY;	
	LCDPORTDATA = ((y+1)*PSystemSet[6] + x) >> 8;	//
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRRIGHT;	
	
	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	//
	
	for(i=2;i<4;i++){
		LCDDELAY;	
		LCDPORTDATA = i+codeno;
	}
	return 0;
}

/*
	载入汉字字模库子程序。在1335的用户自定义字库一区。
	字库为16*16的点阵字库,每个汉字占用32bytes占用字库
	序号的4个序号,依次序左上,右上,左下,右下.
	参数:
		ramaddr	为汉字字模库在内存中的加载地址.
		length	为汉字字模库长度.
	返回:
		正常:  0
		异常:	-1
*/
int loadchinese(unsigned ramaddr,unsigned int *chinese,int length){
	int i;
	
	if((long)ramaddr+(long)length+0x400 > 0x0ffff){ // 0x400 = 0x80*8
		return -1;
	}
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CGRAMADDR;	
	LCDDELAY;	
	LCDPORTDATA = ramaddr & 0x0ff;	
	LCDDELAY;	
	LCDPORTDATA = ramaddr >> 8;	//SAG
		
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = (0x400 + ramaddr) & 0x0ff;	
	LCDDELAY;	
	LCDPORTDATA = (0x400 + ramaddr) >> 8;	//SAG
	
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRRIGHT;	
	
	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	
	
	for( i=0;i<length;i++){
		LCDDELAY;	
		LCDPORTDATA = chinese[i];			
	}
	
	return 0;
}

void CLEAR(void){
	long i; 
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRRIGHT;	

	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = 0;	//
	LCDDELAY;	
	LCDPORTDATA = 0;	//

	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	//
	
	for(i=0;i<65536;i++){
		LCDDELAY;	
		LCDPORTDATA = 0X00;
	}

}


void Black(void){
	long i; 
	LCDDELAY;	
	LCDPORTCMD = LCD_CSRRIGHT;	

	LCDDELAY;	
	LCDPORTCMD = LCD_CSRW;	
	LCDDELAY;	
	LCDPORTDATA = 0;	//
	LCDDELAY;	
	LCDPORTDATA = 0;	//

	LCDDELAY;	
	LCDPORTCMD = LCD_MWRITE;	//
	
	for(i=0;i<65536;i++){
		LCDDELAY;	
		LCDPORTDATA = 0xFf;
	}

}

void WriteCommand(int command, int *parameter, int length){
	int i;
	
	LCDDELAY;				// delay 50 ns
	
	LCDPORTCMD = command;	// write command to LCD
	for(i=0;i<length;i++){		// write parameters of this command
		LCDDELAY;
		LCDPORTDATA = parameter[i];
	}	
}

void Delay_ns(int time)
{	int i;

	for(i=0;i<time;i++)
		asm("	nop"); 
		
	return;	
}
void Delay1s(void)
{	int i,j;

	for(i=0;i<100;i++)
		for(j=0;j<1000;j++)		
			asm("	nop"); 
		
	return;	
}

⌨️ 快捷键说明

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