不好意思,想想还是重新写个帖吧,原来的改不了了。
之前写了一个LPC213的12864液晶的程序,但显示乱码,经过几天的努力已成功了,现在发上来分享给大家!
/********************************************************************
* 文件名 : 液晶12864显示.c
* 描述 : 该程序实现了LPC2131对液晶12864的控制。
* 创建人 : 刘欢,2010年7月23日
* 联系方式: QQ:282679703
* 版本号 : v1.0(实现了简单的字符和字符串的显示)
* 说明 : P1.16~P1.23接液晶D1-D8,
* P0.29接RS,P0.30接RW,P0.31,直接显示内容版
***********************************************************************/
#include "config.h"
#define rs 1<<29 //设数据/命令选择为P0.29
#define rw 1<<30 //设数据/命令选择为P0.30
#define e ((uint32)1)<<31 //设数据/命令选择为P0.31
#define uchar unsigned char
#define uint unsigned int
#define FIRST_ADDR 0 //定义字符/汉字显示起始位置
unsigned char CGRAM[]={
0x08,0x20,0x1c,0x10,0x1c,0x1c,0xff,0x9e,0x7f,0x1e,0x1c,0x1f,0x3e,0x1f,0x3e,0x1f,
0x77,0x1f,0x41,0x3f,0x00,0x7e,0x00,0xfe,0x83,0xfc,0x7f,0xf8,0x3f,0xf0,0x0f,0xc0,
};
unsigned char BMP1[];
unsigned char BMP2[];
unsigned char BMP3[];
unsigned char BMP4[];
unsigned char BMP5[];
unsigned char BMP6[];
unsigned char BMP7[];
unsigned char BMP8[];
unsigned char Num[]={'0','1','2','3','4','5','6','7','8','9','.'};
/********************************************************************
* 名称 : Delay()
* 功能 : 延时,显示字符用(因为是显示字符最佳时间了)
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Delay()
{
int i,j;
for(i=22;i>0;i--)
for(j=100;j>0;j--);
}
/********************************************************************
* 名称 : DelayBMP()
* 功能 : 延时,显示图片用(因为是显示字符最佳时间了)
* 输入 : 无
* 输出 : 无
***********************************************************************/
void DelayBMP()
{
int j;
for(j=155;j>0;j--);
}
/********************************************************************
* 名称 : Delay1()
* 功能 : 可控延时
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Delay1(uint t)
{ unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1000;j++)
;
}
/********************************************************************
* 名称 : Enable(uchar del)
* 功能 : 12864命令函数,显示字符用(因为是显示字符最佳时间了)
* 输入 : 输入的命令值
* 输出 : 无
***********************************************************************/
void Enable(uint32 del)
{
del=del<<16;
IO1PIN=IO1PIN&0xff00ffff|del; //写命令
Delay();
IO0CLR=rs;
IO0CLR=rw;
IO0CLR=e;
IO0SET=e;
Delay();
}
/********************************************************************
* 名称 : EnableBMP(uchar del)
* 功能 : 12864命令函数,显示图片用(最快刷屏时间)
* 输入 : 输入的命令值
* 输出 : 无
***********************************************************************/
void EnableBMP(uint32 del)
{
del=del<<16;
IO1PIN=IO1PIN&0xff00ffff|del; //写命令
DelayBMP();
IO0CLR=rs;
IO0CLR=rw;
IO0CLR=e;
IO0SET=e;
DelayBMP();
}
/********************************************************************
* 名称 : Init()
* 功能 : 12864初始化
* 输入 : 无
* 输出 : 无
***********************************************************************/
int Init_12864() ////初始化
{
PINSEL1=PINSEL1&0x03ffffff|0x03000000;
PINSEL2=PINSEL2&(~0x08);
IO1DIR=IO1PIN&0xff00ffff|0x00ff0000;
IO0DIR=((uint32)0x7<<29); //设端口位输出功能
IO1PIN=IO1PIN&0xff00ffff|0x00ff0000;
IO0PIN=IO0PIN&0x00ffffff|0x03000000; //初始化端口
Enable(0x38);
Delay();
Enable(0x38);
Delay();
Enable(0x38);
Delay();
Enable(0x38);
Enable(0x08);
Enable(0x01);
Enable(0x06);
Enable(0x0c);
return(0);
}
/********************************************************************
* 名称 : CLR()
* 功能 : 清屏
* 输入 : 无
* 输出 : 无
***********************************************************************/
void CLR()
{
Enable(0x01);
Delay1(20);
}
/********************************************************************
* 名称 : Write(uchar del)
* 功能 : 12864写数据函数
* 输入 : 需要写入12864的数据
* 输出 : 无
***********************************************************************/
void Write(char del)
{
IO1PIN=(del<<16)|(IO1PIN&0xff00ffff); //写数据
Delay();
IO0SET=rs;
IO0CLR=rw;
IO0CLR=e;
Delay();
IO0SET=e;
Delay();
}
/********************************************************************
* 名称 : WriteBMP(uchar del)
* 功能 : 12864写数据函数(显示图片用,最快刷屏时间)
* 输入 : 需要写入12864的数据
* 输出 : 无
***********************************************************************/
void WriteBMP(char del)
{
IO1PIN=(del<<16)|(IO1PIN&0xff00ffff); //写数据
DelayBMP();
IO0SET=rs;
IO0CLR=rw;
IO0CLR=e;
DelayBMP();
IO0SET=e;
DelayBMP();
}
/********************************************************************
* 名称 : DisStr16()
* 功能 : 12864显示16*16字符串函数
* 输入 : hang显示的行数,lie显示的列数,*p显示的内容
* 输出 : 无
***********************************************************************/
void DisStr16(uchar hang,uchar lie,char *p)
{
uint32 a,i=0;
if(lie>8) lie=1;
if(hang==1) a=0x80;
if(hang==2) a=0x90;
if(hang==3) a=0x88;
if(hang==4) a=0x98;
a=a+lie-1;
Enable(a);
while(1)
{
if((i/2+lie)>8) break;
if(*p=='\0') break;
Write(*p);
p++;
i++;
}
IO1PIN=IO1PIN&0xff00ffff|0x00010000;
}
/********************************************************************
* 名称 : DisStr8()
* 功能 : 12864显示8*16字符串函数
* 输入 : hang显示的行数,lie显示的列数,*p显示的内容
* 输出 : 无
***********************************************************************/
void DisStr8(uchar hang,uchar lie,char *p)
{
uint32 a,i=0;
if(lie<1) lie=1;
if(lie>16) lie=16;
lie=lie-1;
if(hang==1) a=0x80;
if(hang==2) a=0x90;
if(hang==3) a=0x88;
if(hang==4) a=0x98;
a=a+lie/2;
Enable(a);
if(lie%2==1) Write(0x20);
while(1)
{
if((i+lie)>15) break;
if(*p=='\0') break;
Write(*p);
p++;
i++;
}
IO1PIN=IO1PIN&0xff00ffff|0x00010000;
}
/********************************************************************
* 名称 : DisChar()
* 功能 : 显示单个数字字符函数
* 输入 : hang显示的行数,lie显示的列数,sign显示的内容
* 输出 : 无
***********************************************************************/
void DisChar(uchar hang,uchar lie,uint sign)
{
uint32 a;
if(lie<1) lie=1;
if(lie>16) lie=16;
lie=lie-1;
if(hang==1) a=0x80;
if(hang==2) a=0x90;
if(hang==3) a=0x88;
if(hang==4) a=0x98;
a=a+lie/2;
Enable(a);
if(lie%2==1) Write(0x20);
Write(sign+0x30);
IO1PIN=IO1PIN&0xff00ffff|0x00010000;
}
/********************************************************************
* 名称 : DisNum()
* 功能 : 显示单个数字字符函数
* 输入 : hang显示的行数,lie显示的列数,sign显示的内容
* 输出 : 无
***********************************************************************/
void DisNum(uchar hang,uchar lie,uint num1,uint num2)
{
uint32 a;
if(lie<1) lie=1;
if(lie>16) lie=16;
lie=lie-1;
if(hang==1) a=0x80;
if(hang==2) a=0x90;
if(hang==3) a=0x88;
if(hang==4) a=0x98;
a=a+lie/2;
Enable(a);
if(lie%2==1) Write(0x20);
Write(num1+0x30);
Write(num2+0x30);
IO1PIN=IO1PIN&0xff00ffff|0x00010000;
}
/********************************************************************
* 名称 : WCGRAM()
* 功能 : 写自定义一个字符代码到CGRAM
* 输入 : 无
* 输出 : 无
***********************************************************************/
void WCGRAM()
{
int i;
Enable(0x30);
Enable(0x40);
for(i=0;i<16;i++)
{Enable(CGRAM[i*2]);
Enable(CGRAM[i*2+1]);
}
}
/********************************************************************
* 名称 : CLRGDRAM()
* 功能 : 清整个GDRAM空间
* 输入 : 无
* 输出 : 无
***********************************************************************/
void CLRGDRAM()
{
uchar x,y,i;
CLR();
for(i=0;i<9;i+=8) //0时上半屏,8时下半屏
for(y=0;y<32;y++) //列地址(0~32) (0~32)
for(x=0;x<8;x++) //行地址(0~7) (8~15)
{ EnableBMP(0x36); //8位数据总线,扩展指令集,绘图显示开
EnableBMP(y+0x80); //行地址
EnableBMP(x+0x80+i); //列地址
EnableBMP(0x30);
WriteBMP(0x00);
WriteBMP(0x00);
}
CLR();
}
/********************************************************************
* 名称 : RCGRAM()
* 功能 : 调用自定义的CGRAM
* 输入 : 无
* 输出 : 无
***********************************************************************/
void RCGRAM()
{
Enable(0x87);
Enable(0x00);
Enable(0x00);
}
/********************************************************************
* 名称 : DisImg(uint xs,uint ys,uint xe,uint ye,uchar *img)
* 功能 : 显示图片 xs为图片显示的起始横坐标,xs=1-16
xe为图片显示的结束横坐标,xe=2-17
宽单位为8个像素点,图片大小必须为的宽必须为8的倍数,
且小于128,如宽为16时,xe-xs=2 即宽度为2*8=16个像素点
ys为图片显示的起始纵坐标,ys=1-64
ye为图片显示的结束纵坐标,ye=2-65
高单位为1个像素,图片高小于64,
如高为19,ye-ys=19 即高度为 19个像素点
* 输入 : 无
* 输出 : 无
***********************************************************************/
void DisImg(uint xs,uint ys,uint xe,uint ye,uchar *img)
{
uint j=0;
uchar x,y,xxs,xxe,i=0,k=0;
xs-=1;ys-=1;xe-=1;ye-=1;
xxs=xs/2;xxe=xe/2;
for(y=ys;y<=ye;y++)
{
for(x=xxs;x<xxe;x++)
{
if(y>=32) i=8,k=32;
EnableBMP(0x36); //8位数据总线,扩展指令集,绘图显示开
EnableBMP(y+0x80-k); //行地址
EnableBMP(x+0x80+i); //列地址
EnableBMP(0x30);
if(xs%2==1) WriteBMP(0x00);
WriteBMP(img[j++]);
WriteBMP(img[j++]);
}
}
}
/********************************************************************
* 名称 : main()
* 功能 : 主函数
* 输入 : 无
* 输出 : 无
***********************************************************************/
int main(void)
{
Init_12864();
//CLRGDRAM();
while(1)
{
CLRGDRAM();
DisImg(1,1,17,65,BMP2);
Delay1(10000);
CLRGDRAM();
DisImg(15,1,17,17,BMP1);
Delay1(10000);
CLRGDRAM();
DisStr16(1,2,"电~~子0801班");
DisStr16(2,2,"电子设计竞赛");
DisStr8(3,2,"I am Liu Huan");
DisChar(4,2,8);
DisNum(4,3,7,7);
Delay1(10000);
}
}
unsigned char BMP1[]={
/*B*/
/* (16 X 16 )*/
0X7F,0XF8,0X7F,0XFC,0X70,0X1C,0X70,0X0E,0X70,0X0E,0X70,0X1C,0X7F,0XF8,0X7F,0XF8,
0X7F,0XFC,0X70,0X0E,0X70,0X0E,0X70,0X0E,0X78,0X3E,0X7F,0XFC,0X7F,0XF8,0X00,0X00,
};
unsigned char BMP2[]={
/*火影图片*/
/*-- 宽度x高度=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X00,0XFF,0X80,0XE4,0X0C,0X03,0X3F,0XF7,0XFF,0XFC,0X00,0X3F,
0XFC,0X00,0X01,0XC0,0X00,0X00,0X00,0XF5,0X0A,0X03,0X3F,0XEC,0X3F,0XF8,0X00,0X3F,
0XFC,0X00,0X01,0X80,0X02,0X01,0X03,0X79,0X16,0X02,0X3F,0XE9,0X3F,0XF8,0X00,0X3F,
0XFC,0X00,0X00,0X00,0X03,0X07,0X66,0XFB,0X14,0X02,0X7F,0XCB,0X5F,0XFC,0X00,0X3F,
0XFC,0X00,0X00,0X00,0X03,0X87,0XFF,0XFF,0X20,0X02,0X7F,0XCC,0X5F,0XFC,0X00,0X3F,
0XFC,0X00,0X00,0X00,0X03,0XDF,0XFF,0XF0,0X00,0X03,0X7F,0XA7,0X9F,0XFC,0X00,0X3F,
0XFC,0X00,0X00,0X00,0X01,0XFF,0XFF,0XF0,0X00,0X03,0X7F,0XA0,0X3F,0XF0,0X00,0X3F,
0XFC,0X00,0X00,0X00,0X01,0XFF,0XFF,0XD0,0X40,0X01,0X7F,0XFD,0XFF,0XF4,0X01,0XBF,
0XFC,0X00,0X00,0X00,0X01,0XFF,0XFF,0XF0,0X00,0X01,0X7F,0XFF,0XFD,0XF8,0X01,0X3F,
0XFC,0X00,0X00,0X00,0X00,0XFF,0XFF,0XC0,0X00,0X00,0X7F,0XFF,0XF0,0X00,0X02,0X3F,
0XFC,0X00,0X00,0X00,0X00,0XF9,0X7F,0XE0,0X03,0X00,0X00,0X00,0X00,0X00,0X0C,0XBF,
0XFC,0X00,0X00,0X00,0X00,0X7D,0XFF,0X80,0X01,0X80,0X00,0X00,0X00,0X0F,0XE8,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X7F,0XFF,0X00,0X00,0XA8,0X00,0X00,0X02,0X03,0XE1,0XBF,
0XFC,0X00,0X00,0X00,0X00,0X3E,0X30,0X00,0X03,0X4F,0X81,0XFF,0XFC,0X01,0XE1,0XBF,
0XFC,0X00,0X00,0X00,0X00,0X0F,0XE0,0X00,0X03,0X0F,0X00,0XFF,0XC4,0X45,0XE1,0XBF,
0XFC,0X00,0X00,0X00,0X00,0X1F,0X00,0X00,0X03,0X1F,0X40,0X47,0XEC,0X0F,0XE9,0XBF,
0XFC,0X00,0X00,0X00,0X00,0X30,0X00,0X00,0X03,0X0F,0X40,0X6F,0XFE,0X0F,0XF9,0XBF,
0XFC,0X00,0X00,0X00,0X00,0X20,0X00,0X00,0X03,0X0F,0XC3,0XFF,0XFF,0X0F,0XF3,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X3F,0XE0,0XFF,0XFF,0XDF,0XE3,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X1F,0XFB,0XFF,0XFF,0XFF,0XE6,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X8F,0XFF,0XFF,0XFF,0XFF,0XCC,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC7,0XFF,0XFF,0XFF,0XFF,0XCC,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X77,0XFF,0XFF,0XFF,0XFF,0XDC,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X37,0XFF,0XFF,0XFF,0XFF,0X18,0X3F,
0XFC,0X00,0X00,0X00,0X02,0X00,0X00,0X00,0X00,0X33,0XFF,0XFF,0XFF,0XFF,0XB8,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X13,0XFF,0XFF,0XFF,0XFF,0XFE,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X07,0XFF,0XFF,0XFF,0XFF,0XFF,0X3F,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X1D,0XFF,0XFF,0XFF,0XFF,0X3F,0XBF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X1F,0XFF,0XFD,0X3F,0XFF,0X7F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X3C,0XFF,0XFC,0X3F,0XFF,0X7F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XFC,0XFF,0XFE,0X7F,0XFE,0X7F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XF8,0X7F,0XFF,0XFF,0XFC,0X4F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0XF8,0X3F,0XFF,0XFF,0XF8,0X4F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XF8,0X1F,0XFF,0XFF,0XF9,0X07,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XF0,0X0F,0XFF,0XFF,0XE7,0X07,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XF8,0X07,0XFF,0XFF,0XC3,0X07,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XF8,0X63,0XFF,0XFF,0X81,0X0F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XFC,0X50,0XFF,0XFE,0X00,0X0F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XFC,0X40,0X3F,0XFC,0X00,0X1F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XFC,0X41,0X0F,0XF0,0X00,0X3F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XFE,0X01,0XC0,0X00,0X38,0X7F,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XFF,0X27,0XF0,0X00,0X7C,0XFF,0XFF,
0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XFF,0XA7,0XFC,0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