240160b.c
来自「基于C8051F020单片机的LCD显示程序」· C语言 代码 · 共 480 行 · 第 1/2 页
C
480 行
// 函数:void LcdClr(void)
//-----------------------------------------------------------------------------
/******************************************************************************
调用方式(Function):LcdClr()
函数描述(Description):清显示 32K RAM区(清屏)子程序
入口参数(Parameters):无
返回值(Returns):无
创建时间:16:07,PM,2004.4.19
修改时间:无
修改原因:无
******************************************************************************/
void LcdClr(void){
Uint i1=32768;
WriteCmd( 0x4c ); // 光标移动方向定义:自动右移0x4c
WriteCmd( 0x46 ); // 光标Locate,定位0x46
WriteData( 0 ); // 写入参数CSRL设置参数光标指针低8位
WriteData( 0 ); // 写入参数CSRH设置参数光标指针高8位
WriteCmd( 0x42 ); // 数据写入指令,代码0x42
while(i1--) WriteData( 0x00); // 写入数据0
}
//-----------------------------------------------------------------------------
// 函数:void Locatexy(Uchar x,Uchar y)
//-----------------------------------------------------------------------------
/******************************************************************************
调用方式(Function):Locatexy(x,y)
函数描述(Description):光标位定位,用于在1区图形方式下
入口参数(Parameters):
x...水平方向字节单位坐标,即可以以半个汉字宽度设定水平坐标
取值范围:0...39
y...垂直方向以行线定位的坐标,可以定位到点单位
取值范围:0...239
左上角约定坐标为(0,0)
返回值(Returns):无
创建时间:16:07,PM,2004.4.20
修改时间:无
修改原因:无
******************************************************************************/
void Locatexy(Uchar x,Uchar y) {
Uint temp;
temp = (Uint)y*paraP9+x;
WriteCmd( CsrW ); // 光标Locate,定位
WriteData( (Uchar)(temp & 0xff) ); // 写入参数CSRL设置参数光标指针低8位
WriteData( (Uchar)(temp /256 ) ); // 写入参数CSRH设置参数光标指针高8位
}
//-----------------------------------------------------------------------------
// 函数:Uchar dprintf(Uchar x,Uchar y,char code *ptr,bit attrib)
//-----------------------------------------------------------------------------
/******************************************************************************
调用方式(Function):dprintf(x,y,*ptr,attrib)
函数描述(Description):ASCII(8*16) 及 汉字(16*16) 混合字符串显示函数
入口参数(Parameters):
x,y显示字符串的左上角xy坐标
x...8点一字节位置,取值范围:0...29
y...一条扫描线定位,取值范围:0...159
*ptr...字符串指针,本函数所指的是flash字串
attrib:属性参数,为0:反显;为:1正常显示
返回值(Returns):输出字串长度,留意汉字一个算两个
其它假定:调用时汉字必须在字库数组中已经存在,否则将输出不正确的结果
创建时间:16:07,PM,2004.4.20
修改时间:无
修改原因:无
******************************************************************************/
/********************************************************/
/* ASCII(8*16) 及 汉字(16*16) 显示函数 */
/********************************************************/
Uchar dprintf(Uchar x,Uchar y,char code *ptr,bit attrib)
{
Uchar c1,c2;
Uchar i,j,uLen,uRow,uCol,temp;
Uint k;
uRow = x;
uCol = y;
uLen=0;
WriteCmd( CsrDirD ); // 光标移动方向定义:自动下移
Locatexy(uRow,uCol); //起点定位
while ( (Uchar)ptr[uLen] >= 0x10 ){uLen++;}; //探测字串长度
i=0;
while(i<uLen)
{
c1 = ptr[i];
c2 = ptr[i+1];
//ascii字符与汉字内码的区别在于128做分界,大于界线的为汉字码
if(c1 <=128) // ASCII
{
if (c1 >= 0x10) {
WriteCmd( mWrite ); // 写数据(命令)
for(j=0;j<16;j++)
if(attrib)WriteData( ASC_MSK[(c1-0x20)*16 +j ]);
else WriteData(~(ASC_MSK[(c1-0x20)*16 +j ]));
}
uRow++; // 列数加1
}
else // 中文
{
for(j=0;j<sizeof(Cdotlib)/sizeof(Cdotlib[0]);j++) // 查找定位
{
if((c1 == Cdotlib[j].Index[0]) && (c2 == Cdotlib[j].Index[1]))
break;
}
for(k=0;k<2;k++) // 分16行输出
{
Locatexy(uRow+k,uCol);
WriteCmd( mWrite ); // 写数据(命令)
for(temp=0;temp<16;temp++)
if(attrib) WriteData( Cdotlib[j].Msk[temp*2 + k]);
else WriteData(~( Cdotlib[j].Msk[temp*2 + k]));
}
uRow +=2; // 光标右移一大格
i++;
}
if(uRow >= 30) // 光标后移,原来为40
{
uCol += 16;
uRow = 0;
if( uCol >= 160 ) uCol = 0; //共有160点行
}
Locatexy(uRow,uCol);
i++;
}
return uLen; //返回字串长度,汉字按2字节计算
}
//x...8点一字节位置,取值范围:0...29
//y...一条扫描线定位,取值范围:0...159
void MyGraph(Uchar x,Uchar y,Uchar Weight,Uchar Height,Uchar code *ptr,bit attrib){
Uchar uRow,uCol,temp,nbyte;
Uint k;
uRow = x;
uCol = y;
WriteCmd( CsrDirD ); // 光标移动方向定义:自动下移
Locatexy(uRow,uCol); //起点定位
nbyte=(Uchar)(Weight/8);
for(k=0;k<nbyte;k++) // 分16行输出
{
Locatexy(uRow+k,uCol);
WriteCmd( mWrite ); // 写数据(命令)
for(temp=0;temp<Height;temp++)
if(attrib) WriteData(ptr[temp*nbyte+k]);
else WriteData(~(ptr[temp*nbyte+k]));
}
}
/*====================================================
; 绘点子程序,携入参数X坐标的最高位决定写或擦点
====================================================*/
void Point(Uint Px,Uchar Py,Uchar attr){
Uint tempPtr;
Uchar tempD,tempP;
tempPtr = (Uint)Py * paraP9 + (Px & 0x7fff)/8; // 去除最高位(显示属性)
WriteCmd( CsrDirD ); // CSRDIR 代码(光标自动下移)
WriteCmd( CsrW ); // 设置光标地址
WriteData( (Uchar)(tempPtr & 0xff) );
WriteData( (Uchar)(tempPtr /256) );
WriteCmd( mRead ); // 读显示ram指令
tempD = ReadfromLcd(); // 读取当前显示数据
tempP = 1<<(Uchar)(7-Px & 0x0007);
// 根据预定属性决定写点或擦除
if( attr )tempD |= tempP; // 画点
else tempD &= ~tempP; // 消点
WriteCmd( CsrW ); // 重新设置光标地址
WriteData( (Uchar)(tempPtr & 0xff) );
WriteData( (Uchar)(tempPtr /256) );
WriteCmd( mWrite ); // 代码0x42,数据写入指令
WriteData( tempD ); // 写入合成数据
}
void SYSCLK_Init(void){
Uchar i; // delay counter
OSCXCN = 0x66; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 254; i++) ; // wait for osc to start
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
void PORT_Init(void)
{
XBR0=0x00; // 1:Enable; UART0,SMBUS
XBR1=0x00;
XBR2=0x40; // Enable crossbar and weak pull-ups
P0MDOUT |=0xf0; // enable P07~P04 as push-pull outputs,4个按键(P03~P00)为开漏
P1MDOUT |=0xff; // enable P1 as push-pull outputs
P2MDOUT |=0xfc; //0xfc // enable P27~P22 as push-pull outputs,P21(SCLK),P20(SDA)配置为开漏
P3MDOUT |=0x3f; // // enable P3.5~P3.0 as push-pull outputs,P3.7(IRQ),P3.6(Ctime)为开漏
P74OUT |=0xf3; // //
}
void main(void){
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init(); // 切换到外部晶振
// OSCICN=0x14; // 内部晶振16M运行,2M:0x14;4M:0x15;8M:0x16;16M:0x17
PORT_Init(); // 端口初始化
LcdInit();
LcdClr();
EL_ON=1; //开背光
dprintf(0,50,"2004/04/25重庆东电",0);
MyGraph(23,111,48,48,Ico_RP,1); //显示一副图片
while(1);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?