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

📄 newlcd.c

📁 该程序是本人学习单片机期间针对2片SED1520编写的122X32点阵液晶驱动.内附详细注释.
💻 C
字号:
/*sed1520的122X32点阵液晶驱动 (2片)  

硬件环境:Lab2000实验箱
LCD CS-->CS0 0x8000~0x8007
0x8000:写E1指令
0x8001:写E1数据
0x8002:读E1状态
0x8003:读E1数据
0x8004:写E2指令
0x8005:写E2数据
0x8006:读E2状态
0x8007:读E2数据

                              方式命令字
读状态字
R/W A0		D7   D6    D5     D4  D3 D2 D1 D0
1    0		BUSY ADC OFF/0N RESET 0  0  0  0
		忙:1右:1关:1复位:1
		闲:0左:0开:0正常:0

复位
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0		1   1  1  0  0  0  1  0		0xe2	LCD_RESET

占空比
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0		1   0  1  0  1  0  0 0/1
				     32行:1	0xa9	LCD_DUTY32
				     16行:0	0xa8	LCD_DUTY16

显示起始行设置
R/W A0  	D7 D6 D5 D4 D3 D2 D1 D0
0   0  		1   1  0   起始行:0~31
				  起始:0	0xc0	LCD_STARTLINE0

休闲工作状态设置
R/W A0 		D7 D6 D5 D4 D3 D2 D1 D0
0    0    	1   0  1  0  0  1  0 0/1
				     休闲:1	0xa5	LCD_DRIVERON
				     正常:0	0xa4	LCD_DRIVEROFF

ADC选择指令
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0		 1  0  1  0  0  0  0 0/1
				     ADC=0	0xa0	LCD_ADC

显示开、关指令
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0		1   0  1  0  1  1  1 0/1
				     开:1	0xaf	LCD_DISPLAYON
				     关:0	0xae	LCD_DISPLAYOFF

设置页地址
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0 		1   0  1  1  1  0 页地址:0~3  0xb?
				  起始:0	0xb8	LCD_PAGE0

设置列地址
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0		 0     列地址:0~79
				  起始:0	0x00	LCD_COLUMMADDR0

改写方式设置指令
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0		1   1  1  0  0  0  0  0		0xe0	LCD_RWMODE
		写数据后列地址自动+1;读数据则不变

改写方式结束指令
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0		1   1  1  0  1  1  1  0		0xee	LCD_END
		无论读写数据之后,列地址+1

写数据
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0			显示数据(正常情况下,读写后,列地址自动+1)

读数据
R/W A0		D7 D6 D5 D4 D3 D2 D1 D0
0    0			显示数据
                                                        刘忠峰    2005.7.20
*/
#include <reg51.h>
#include <absacc.h>

#define WR_COMM1 		XBYTE[0x8000]
#define WR_DATA1 		XBYTE[0x8001]
#define RD_STAT1 		XBYTE[0x8002]
#define RD_DATA1 		XBYTE[0x8003]
#define WR_COMM2		XBYTE[0x8004]
#define WR_DATA2 		XBYTE[0x8005]
#define RD_STAT2 		XBYTE[0x8006]
#define RD_DATA2 		XBYTE[0x8007]

#define LCD_DISPLAYON		0xaf
#define LCD_DISPLAYOFF		0xae
#define LCD_DRIVERON		0xa5
#define LCD_DRIVEROFF		0xa4
#define LCD_DUTY16		0xa8
#define LCD_DUTY32		0xa9
#define LCD_ADC			0xa0

#define LCD_END			0xee
#define LCD_RESET		0xe2
#define LCD_RWMODE		0xe0
#define LCD_STARTLINE0		0xc0
#define LCD_PAGE0		0xb8
#define LCD_COLUMNADDR0		0x00

#define LCD_ON()		LCD_Command(LCD_DISPLAYON)
#define LCD_OFF()		LCD_Command(LCD_DISPLAYOFF)
#define LCD_StaticON()		LCD_Command(LCD_DRIVERON)
#define LCD_StaticOFF()		LCD_Command(LCD_DRIVEROFF)
#define LCD_Select16()		LCD_Command(LCD_DUTY16)
#define LCD_Select32()		LCD_Command(LCD_DUTY32)
#define LCD_StartRW()		LCD_Command(LCD_RWMODE)
#define LCD_EndRW()		LCD_Command(LCD_END)
#define LCD_Reset()		LCD_Command(LCD_RESET)
#define LCD_SetStartLine(i) 	LCD_Command(LCD_STARTLINE0+i)
#define LCD_SetPage(i) 		LCD_Command(LCD_PAGE0+i)
#define LCD_SetColumn(i) 	LCD_Command(LCD_COLUMNADDR0+i)
#define LCD_SelectDAC()         LCD_Command(LCD_ADC)

#define TIME                    10

/*ASCII字体,大小6X8,上到下D0~D7,左到右*/
unsigned char code ASCII[][6]={
{0x00,0x00,0x00,0x00,0x00,0x00},/*SPACE*/
{0x00,0x7e,0x21,0x21,0x21,0x7e},/*A*/
{0x00,0x7f,0x49,0x49,0x49,0x36},/*B*/
{0x00,0x3e,0x41,0x41,0x41,0x22},/*C*/
{0x00,0x7f,0x41,0x41,0x22,0x1c},/*D*/
{0x00,0x7f,0x49,0x49,0x49,0x49},/*E*/
{0x00,0x7f,0x09,0x09,0x09,0x01},/*F*/
{0x00,0x3e,0x41,0x49,0x49,0x7a},/*G*/
{0x00,0x7f,0x08,0x08,0x08,0x7f},/*H*/
{0x00,0x41,0x7f,0x41,0x00,0x00},/*I*/
{0x00,0x20,0x40,0x41,0x3f,0x01},/*J*/
{0x00,0x7f,0x08,0x14,0x22,0x41},/*K*/
{0x00,0x7f,0x40,0x40,0x40,0x40},/*L*/
{0x00,0x7f,0x02,0x0c,0x02,0x7f},/*M*/
{0x00,0x7f,0x04,0x08,0x10,0x7f},/*N*/
{0x00,0x3e,0x41,0x41,0x41,0x3e},/*O*/
{0x00,0x7f,0x09,0x09,0x09,0x06},/*P*/
{0x00,0x3e,0x41,0x41,0x41,0x3e},/*Q*/
{0x00,0x7f,0x09,0x19,0x29,0x46},/*R*/
{0x00,0x46,0x49,0x49,0x49,0x31},/*S*/
{0x00,0x01,0x01,0x7f,0x01,0x01},/*T*/
{0x00,0x3f,0x40,0x40,0x40,0x3f},/*U*/
{0x00,0x1f,0x20,0x40,0x20,0x1f},/*V*/
{0x00,0x3f,0x40,0x38,0x40,0x3f},/*W*/
{0x00,0x63,0x14,0x08,0x14,0x63},/*X*/
{0x00,0x07,0x08,0x70,0x08,0x07},/*Y*/
{0x00,0x61,0x51,0x49,0x45,0x43},/*Z*/
{0x00,0x20,0x54,0x54,0x54,0x7c},/*a*/
{0x00,0x7f,0x48,0x44,0x44,0x38},/*b*/
{0x00,0x38,0x44,0x44,0x44,0x20},/*c*/
{0x00,0x38,0x44,0x44,0x48,0x7f},/*d*/
{0x00,0x38,0x54,0x54,0x54,0x18},/*e*/
{0x00,0x08,0x7e,0x09,0x01,0x02},/*f*/
{0x00,0x0c,0x52,0x52,0x52,0x3e},/*g*/
{0x00,0x7f,0x08,0x04,0x04,0x78},/*h*/
{0x00,0x00,0x44,0x7d,0x40,0x00},/*i*/
{0x00,0x40,0x40,0x44,0x3d,0x00},/*j*/
{0x00,0x7f,0x10,0x28,0x44,0x00},/*k*/
{0x00,0x00,0x41,0x7e,0x40,0x00},/*l*/
{0x00,0x7c,0x04,0x18,0x04,0x78},/*m*/
{0x00,0x7c,0x08,0x04,0x04,0x78},/*n*/
{0x00,0x38,0x44,0x44,0x44,0x38},/*o*/
{0x00,0x7c,0x14,0x14,0x14,0x08},/*p*/
{0x00,0x08,0x14,0x14,0x14,0x7c},/*q*/
{0x00,0x7c,0x08,0x04,0x04,0x08},/*r*/
{0x00,0x48,0x54,0x54,0x54,0x20},/*s*/
{0x00,0x04,0x3f,0x44,0x44,0x24},/*t*/
{0x00,0x3c,0x40,0x40,0x20,0x7e},/*u*/
{0x00,0x1d,0x20,0x40,0x20,0x1c},/*v*/
{0x00,0x3c,0x40,0x30,0x40,0x3c},/*w*/
{0x00,0x44,0x28,0x10,0x28,0x44},/*x*/
{0x00,0x06,0x48,0x48,0x48,0x3e},/*y*/
{0x00,0x44,0x64,0x54,0x4c,0x44},/*z*/
{0x00,0x00,0x01,0x02,0x04,0x00},/*`*/
{0x00,0x00,0x42,0x7f,0x40,0x40},/*1*/
{0x00,0x62,0x51,0x51,0x49,0x46},/*2*/
{0x00,0x21,0x41,0x45,0x4b,0x31},/*3*/
{0x00,0x18,0x14,0x12,0x7f,0x10},/*4*/
{0x00,0x27,0x45,0x45,0x45,0x39},/*5*/
{0x00,0x3c,0x4a,0x49,0x49,0x30},/*6*/
{0x00,0x01,0x71,0x09,0x05,0x03},/*7*/
{0x00,0x36,0x49,0x49,0x49,0x36},/*8*/
{0x00,0x06,0x49,0x49,0x29,0x1e},/*9*/
{0x00,0x3e,0x51,0x49,0x45,0x3e},/*0*/
{0x00,0x08,0x08,0x08,0x08,0x08},/*-*/
{0x00,0x14,0x14,0x14,0x14,0x14},/*=*/
{0x00,0x01,0x02,0x08,0x10,0x20},/*\\*/
{0x00,0x04,0x08,0x04,0x08,0x04},/*~*/
{0x00,0x00,0x00,0x4f,0x00,0x00},/*!*/
{0x00,0x3e,0x59,0x55,0x79,0x3e},/*@*/
{0x00,0x14,0x7f,0x14,0x7f,0x14},/*#*/
{0x00,0x24,0x2a,0x7f,0x2a,0x12},/*$*/
{0x00,0x23,0x13,0x08,0x62,0x61},/*%*/
{0x00,0x04,0x02,0x01,0x02,0x04},/*^*/
{0x00,0x36,0x49,0x55,0x22,0x50},/*&*/
{0x00,0x14,0x08,0x3e,0x08,0x14},/***/
{0x00,0x00,0x1c,0x22,0x41,0x00},/*(*/
{0x00,0x00,0x41,0x22,0x1c,0x00},/*)*/
{0x00,0x40,0x40,0x40,0x40,0x40},/*_*/
{0x00,0x08,0x08,0x3e,0x08,0x08},/*+*/
{0x00,0x00,0x00,0x7e,0x00,0x00},/*|*/
{0x00,0x00,0x7f,0x41,0x41,0x00},/*[*/
{0x00,0x00,0x41,0x41,0x7f,0x00},/*]*/
{0x00,0x00,0x56,0x36,0x00,0x00},/*;*/
{0x00,0x00,0x00,0x05,0x03,0x00},/*\'*/
{0x00,0x00,0x00,0x28,0x18,0x00},/*,*/
{0x00,0x00,0x00,0x60,0x60,0x00},/*.*/
{0x00,0x20,0x10,0x08,0x04,0x02},/*/*/
{0x00,0x00,0x08,0x36,0x41,0x00},/*{*/
{0x00,0x00,0x41,0x36,0x08,0x00},/*}*/
{0x00,0x00,0x36,0x36,0x00,0x00},/*:*/
{0x00,0x00,0x00,0x07,0x00,0x07},/*\"*/
{0x00,0x08,0x14,0x22,0x41,0x00},/*<*/
{0x00,0x00,0x41,0x22,0x14,0x08},/*>*/
{0x00,0x02,0x01,0x51,0x09,0x06}/*?*/
};

unsigned char GetIndex(unsigned char szText)      /*寻字以寻址*/
{
	unsigned char code TAB[]={' ',
		'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',
		'R','S','T','U','V','W','X','Y','Z',
		'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
		'r','s','t','u','v','w','x','y','z',
		'`','1','2','3','4','5','6','7','8','9','0','-','=','\\',
		'~','!','@','#','$','%','^','&','*','(',')','_','+','|',
		'[',']',';','\'',',','.','/',
		'{','}',':','\"','<','>','?'
                };
	unsigned char i;
	for(i=0;;i++)
	{
        	if(szText==TAB[i])
                	return i;
        }
        return 0x00;
}

void LCD_Wait(unsigned char sta)       /*存储器忙否?*/
/*1:E1 空闲;     2: E2 空闲;     3:E1,E2 空闲*/
{
	unsigned char LCD_State1,LCD_State2;
	while(1)
	{
	/*	LCD_State1=RD_STAT1;
		LCD_State2=RD_STAT2;
		if(((LCD_State1&0x80)==0)&&((LCD_State2&0x80)==0))
			break;     */
                if(sta==1)
                {

		        LCD_State1=RD_STAT1;
			if((LCD_State1&0x80)==0)
				break;
                }
                else if(sta==2)
                {
                        LCD_State2=RD_STAT2;
			if((LCD_State2&0x80)==0)
				break;
                }
                else if(sta==3)
                {

		        LCD_State1=RD_STAT1;
		        LCD_State2=RD_STAT2;
		        if(((LCD_State1&0x80)==0)&&((LCD_State2&0x80)==0))
				break;
                }
                else    break;
	}
}

void LCD_Command(unsigned char nCommand)       /*写指令*/
{
        LCD_Wait(3);
	WR_COMM1=nCommand;
	WR_COMM2=nCommand;
}

void LCD_SetRam(bit bChip2,unsigned char ndata)      /*向存储器映像写值*/
{
	if(!bChip2)
	{
                LCD_Wait(1);
		WR_DATA1=ndata;
	}
	else
	{
                LCD_Wait(2);
		WR_DATA2=ndata;
	}
}

void LCD_Init()    /*初始化LCD*/
{
        LCD_Reset();
        LCD_ON();
        LCD_SelectDAC();
        LCD_Select32();
        LCD_StaticOFF();
        LCD_SetStartLine(0);
        LCD_SetPage(0);
        LCD_SetColumn(0);
        LCD_EndRW();
}

unsigned char *GetTextFont(unsigned char index)      /*字符编码*/
{
	static unsigned char Font[6];
	unsigned char i;
	for(i=0;i<6;i++)
		Font[i]=ASCII[index][i];
	return Font;
}

/*
x:0~19
y:0~3
*/

void LCD_TextOut(unsigned char x,unsigned char y,unsigned char *szText)         /*定位输出字符*/
{
	unsigned char i;
	bit bChip2;
        if(x>=20||y>=4) return;
        if(x<10)    bChip2=0;
        else
        {
                bChip2=1;
                x-=10;
        }
	LCD_SetPage((y+2)%4);
	LCD_SetColumn(x*6);
	for(i=0;i<6;i++)
		LCD_SetRam(bChip2,szText[i]);
}

void Printf(unsigned char x,unsigned char y,unsigned char *szText)       /*定位输出字符串*/
{
	unsigned char *Text;
        if(x>=20||y>=4) return;
	while(*szText)
	{
		Text=GetTextFont(GetIndex(*szText));
		LCD_TextOut(x++,y,Text);
                if(x>=20)
                {
                        x=0;
                        y++;
                }
                if(y>=4) return;
		szText++;
	}
}

void LCD_Write(unsigned char npage,unsigned char ncolumn,unsigned char ndata)      /*定页单列写数*/
{
        bit bChip2;
        if(npage>=4||ncolumn>=122) return;
        if(ncolumn<61) bChip2=0;
        else
        {
                bChip2=1;
                ncolumn-=61;
        }        
	LCD_SetPage(npage);
	LCD_SetColumn(ncolumn);
	LCD_SetRam(npage>3,ndata);       
}

void LCD_DrawMap(unsigned char idata *ndata)      /*全屏图像122×32*/
{
	unsigned char i,j,k,l;
        for(j=0,k=0;j<4;j++)
        for(l=0;l<2;l++)
        {                       
	        LCD_SetPage((j+2)%4);                  
	        LCD_SetColumn(0);
	        for(i=0;i<61;i++)
                {
		LCD_SetRam(l,ndata[k++]);
                }
         }
}

void clr(void)     /*清屏*/
{
        unsigned char i,j,k;
        bit bChip2;
        for(j=0;j<4;j++)
        {
	       LCD_SetPage((j+2)%4);
               for(i=0;i<122;i++)
               {
                        if(i<61)
                        {
                                bChip2=0;
                                k=i;
                        }
                        else
                        {
                                bChip2=1;
                                k=i-61;
                        }
	                LCD_SetColumn(k);
		        LCD_SetRam(bChip2,0x00);
               }
        }
}

unsigned char code LZF[][6]={	/*自定义字符*/
{0x00,0x3c,0x3c,0x7e,0xff,0x00},
{0x00,0x3c,0x3c,0x66,0xff,0x5a},
{0x08,0x24,0x28,0x28,0x24,0x08},
{0x10,0x48,0x4c,0x50,0x48,0x0c}
};

main()
{
        unsigned int a,i,j,k;
	LCD_Init();
        for(;;)
        {
                clr();
                Printf(2,0,"Hello! Welcome to SED1520!Let us write a string!/\"My name is LiuZhongfeng!\"");        
                for(a=0;a<30000;a++);
        }
}

⌨️ 快捷键说明

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