📄 lcd.c
字号:
#include "stdio.h"
#include "stdlib.h"
#include "d:\lhy\Ascii.dat"
ioport unsigned int port301;
ioport unsigned int port300;
#define Lcd_wc port301
#define Lcd_wd port300
#define Lcd_rs port300
#define Lcd_rd port301
#define SYSTEM_SET 0x40 /*初始化设置*/
#define SCROLL 0x44 /*显示域设置*/
#define SLEEP_IN 0x53 /*休闲模式设置*/
#define DISP_ON 0x59 /*显示状态设置
显示开*/
#define DISP_OFF 0x58 /*显示关*/
#define OVLAY 0x5b /*显示合成设置*/
#define HDOT_SCR 0x5a /*点位移设置*/
#define CSRFORM 0x5d /*光标形状设置*/
#define CGRAM_ADR 0x5c /*CGRAM首址设置*/
#define CSRDIR_RG 0x4c /*光标移动方式设置
光标右移*/
#define CSRDIR_LF 0x4d /*光标左移*/
#define CSRDIR_UP 0x4e /*光标上移*/
#define CSRDIR_DW 0x4f /*光标下移*/
#define CSRW 0x46 /*光标指针设置*/
#define CSRR 0x47 /*读取光标指针*/
#define MWRITE 0x42 /*数据写入设置*/
#define MREAD 0x43 /*数据读取设置*/
#define Lcd_AP 0x28 /*显示屏一行所占字节数*/
#define wait() while((Lcd_rs&0x0040)==0x0040)
#define Horizontal 0x00 /*横线*/
#define Vertical 0x01 /*竖线*/
#define Normal 0x0 /*正常显示标志*/
#define Abnormal 0x1 /*反显标志*/
#define Lcd_Out8x16(Row,Column,Data_Add,Flag) Lcd_Cctw(Row,Column,8,16,Data_Add,Flag)
/*输出8x16点阵字符*/
#define Lcd_Out16x16(Row,Column,Data_Add,Flag) Lcd_Cctw(Row,Column,16,16,Data_Add,Flag)
/*输出16x16点阵字符*/
#define Lcd_Out16x32(Row,Column,Data_Add,Flag) Lcd_Cctw(Row,Column,16,32,Data_Add,Flag)
/*输出16x32点阵字符*/
#define Lcd_Out24x24(Row,Column,Data_Add,Flag) Lcd_Cctw(Row,Column,24,24,Data_Add,Flag)
/*输出24x24点阵字符*/
#define Lcd_Out32x32(Row,Column,Data_Add,Flag) Lcd_Cctw(Row,Column,32,32,Data_Add,Flag)
/*输出32x32点阵字符*/
typedef volatile unsigned int pointer;
/*定义指针类型*/
/************************************************/
/* */
/* Lcd_Clear: 清显示区子程序 */
/* */
/* Data_Add1: 显示区首址 */
/* Data_Add2: 显示区末址 */
/* Data: 显示数据 */
/* */
/************************************************/
void Lcd_Clear(unsigned int Data_Add1,unsigned int Data_Add2,unsigned int Data)
{unsigned int Counter;
Lcd_wc=CSRDIR_RG; wait();
Lcd_wc=CSRW; wait();
Lcd_wd=0x00; wait();
Lcd_wd=0x00; wait();
Lcd_wc=MWRITE; wait();
for(Counter=Data_Add1;Counter<Data_Add2;Counter++)
{
Lcd_wd=Data;
wait();
}
Lcd_wc=CSRDIR_DW; wait();
}
/*==============================================*/
/************************************************/
/* */
/* Lcd_Init: 初始化液晶子程序 */
/* */
/************************************************/
void Lcd_Init( void )
{unsigned int i;
Lcd_wc=SYSTEM_SET; wait();
for(i=0;i<8;i++)
{
Lcd_wd=Lcd_Systab[i];
wait();
}
Lcd_wc=SCROLL; wait();
for(i=0;i<10;i++)
{
Lcd_wd=Lcd_Scrtab[i];
wait();
}
Lcd_wc=HDOT_SCR; wait();
Lcd_wd=0x00; wait();
Lcd_wc=OVLAY; wait();
Lcd_wd=0x08; wait();
Lcd_Clear(0,0x8000,0x00);
Lcd_wc=DISP_ON; wait();
Lcd_wd=0x54; wait();
Lcd_wc=CSRFORM; wait();
Lcd_wd=0x07; wait();
Lcd_wd=0x87; wait();
Lcd_wc=CSRDIR_DW; wait();
}
/************************************************/
/* */
/* Lcd_Pointer: 画点子程序 */
/* */
/* Dot_X: x坐标(0-319) */
/* Dot_Y: y坐标(0-239) */
/* */
/************************************************/
void Lcd_Point(unsigned int Dot_X,unsigned int Dot_Y)
{
unsigned int Cursor_X,Cursor_Y;
unsigned int Counter;
unsigned int Read_Data;
Counter=Dot_X&0x0007;
Cursor_X=(Dot_Y*Lcd_AP+(Dot_X>>3)+0x4000)&0x00ff;
Cursor_Y=((Dot_Y*Lcd_AP+(Dot_X>>3)+0x4000)&0xff00)>>8;
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MREAD; wait();
Read_Data=Lcd_rd&0x00ff;
wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MWRITE; wait();
Lcd_wd=(0x80>>Counter)|Read_Data;
wait();
}
/*==============================================*/
/************************************************/
/* */
/* Lcd_Dot: 画点子程序 */
/* */
/* Dot_X: x坐标(0-319) */
/* Dot_Y: y坐标(0-239) */
/* Dot_Type: 点格式 */
/* */
/************************************************/
void Lcd_Dot(unsigned int Dot_X,unsigned int Dot_Y,unsigned int Dot_Type)
{
unsigned int Cursor_X,Cursor_Y;
unsigned int Counter,Loop;
unsigned int Read_Data;
Counter=0x8-(Dot_X&0x0007);
Dot_Type=Dot_Type<<Counter;
Cursor_X=(Dot_Y*Lcd_AP+(Dot_X>>3)+0x4000)&0x00ff;
Cursor_Y=((Dot_Y*Lcd_AP+(Dot_X>>3)+0x4000)&0xff00)>>8;
Lcd_wc=CSRDIR_RG; wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MREAD; wait();
Read_Data=Lcd_rd&0x00ff;
wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MWRITE; wait();
Lcd_wd=((Dot_Type&0xff00)>>8)|Read_Data;
wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X+1; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MREAD; wait();
Read_Data=Lcd_rd&0x00ff;
wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X+1; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MWRITE; wait();
Lcd_wd=(Dot_Type&0x00ff)|Read_Data;
wait();
}
/*==============================================*/
/************************************************/
/* */
/* Lcd_Line: 划线子程序 */
/* */
/* (X0,Y0): 起始点坐标 */
/* (X1,Y1): 终止点坐标 */
/* */
/* X0,X1: 0-319 */
/* Y0,Y1: 0-239 */
/* */
/************************************************/
void Lcd_Line(unsigned int X0,unsigned int Y0,unsigned int X1,unsigned int Y1)
{
unsigned int Cursor_X,Cursor_Y;
unsigned int Counter,Dot_Type;
unsigned int Read_Data;
double k;
if(X0==X1)
{
if (Y0>Y1) {Counter=Y0;Y0=Y1;Y1=Counter;}
if (Y1>=239) Y1=239;
for (Counter=Y0;Counter<=Y1;Counter++)
Lcd_Point(X0,Counter);
}
else if(Y0==Y1)
{
if (X0>X1) {Counter=X0;X0=X1;X1=Counter;}
if (X1>=319) X1=319;
Cursor_X=(Y0*Lcd_AP+(X0>>3)+0x4000)&0x00ff;
Cursor_Y=((Y0*Lcd_AP+(X0>>3)+0x4000)&0xff00)>>8;
Dot_Type=0xff>>(X0&0x07);
Lcd_wc=CSRDIR_RG; wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MREAD; wait();
Read_Data=Lcd_rd&0x00ff;
wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MWRITE; wait();
Lcd_wd=Dot_Type|Read_Data;
wait();
for (Counter=(X0>>3);Counter<((X1>>3)-1);Counter++)
{
Lcd_wd=0xff;
wait();
}
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X+((X1-X0)>>3);
wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MREAD; wait();
Read_Data=Lcd_rd&0x00ff;
wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X+((X1-X0)>>3);
wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MWRITE; wait();
Dot_Type=0xff<<(8-X1&0x07);
Lcd_wd=Dot_Type|Read_Data;
wait();
}
else if(abs((int)X1-(int)X0)>fabs((int)Y1-(int)Y0))
{
k=((double)Y1-(double)Y0)/((double)X1-(double)X0);
if (X0>X1)
{
for(Cursor_X=X1;Cursor_X<=X0;Cursor_X++)
{
Cursor_Y=(int)((double)(Cursor_X-X1)*k+(double)Y1);
Lcd_Point(Cursor_X,Cursor_Y);
}
}
else
{
for(Cursor_X=X0;Cursor_X<=X1;Cursor_X++)
{
Cursor_Y=(int)((double)(Cursor_X-X0)*k+(double)Y0);
Lcd_Point(Cursor_X,Cursor_Y);
}
}
}
else
{
k=((double)X1-(double)X0)/((double)Y1-(double)Y0);
if (Y0>Y1)
{
for(Cursor_Y=Y1;Cursor_Y<=Y0;Cursor_Y++)
{
Cursor_X=(int)((double)(Cursor_Y-Y1)*k+(double)X1);
Lcd_Point(Cursor_X,Cursor_Y);
}
}
else
{
for(Cursor_Y=Y0;Cursor_Y<=Y1;Cursor_Y++)
{
Cursor_X=(int)((double)(Cursor_Y-Y0)*k+(double)X0);
Lcd_Point(Cursor_X,Cursor_Y);
}
}
}
}
/*==============================================*/
/************************************************/
/* */
/* Lcd_Cctw: 写点阵字符子程序 */
/* */
/* Row: 行坐标<0-29(240/8-1)> */
/* Column: 列坐标<0-39(320/8-1)> */
/* (Dot1,Dot2): 点阵格式 */
/* (eg:8x8,8x16,16x16,16x32,...) */
/* Data_Add: 点阵数据首址 */
/* Flag: 反显标志 */
/* Flag=Normal 正常显示 */
/* Flag=Abnormal 反白显示 */
/* */
/************************************************/
void Lcd_Cctw(unsigned int Row,unsigned int Column,unsigned int Dot1,unsigned int Dot2,pointer *Data_Add,unsigned int Flag)
{
unsigned int Cursor_X,Cursor_Y;
unsigned int Counter1,Counter2;
Cursor_X=((Row<<3)*Lcd_AP+Column+0x4000)&0x00ff;
Cursor_Y=(((Row<<3)*Lcd_AP+Column+0x4000)&0xff00)>>8;
Lcd_wc=CSRDIR_DW; wait();
for(Counter2=0;Counter2<(Dot1>>3);Counter2++)
{
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MWRITE; wait();
for(Counter1=0;Counter1<Dot2;Counter1++)
{
if(Flag==Abnormal)
{
Lcd_wd=~(*(Data_Add++));
wait();
}
else
{
Lcd_wd=*(Data_Add++);
wait();
}
}
Cursor_X++;
}
}
/*==============================================*/
/************************************************/
/* */
/* Lcd_Character: 显示8x8点阵Ascii码 */
/* */
/* Row: 行坐标<0-29(240/8-1)> */
/* Column: 列坐标<0-39(320/8-1)> */
/* Character: Ascii码 */
/* */
/************************************************/
void Lcd_Character(unsigned int Row,unsigned int Column,unsigned int Character)
{
unsigned int Cursor_X,Cursor_Y;
Cursor_X=(Row*Lcd_AP+Column+0x0000)&0x00ff;
Cursor_Y=((Row*Lcd_AP+Column+0x0000)&0xff00)>>8;
Lcd_wc=CSRDIR_RG; wait();
Lcd_wc=CSRW; wait();
Lcd_wd=Cursor_X; wait();
Lcd_wd=Cursor_Y; wait();
Lcd_wc=MWRITE; wait();
Lcd_wd=Character; wait();
}
/*==============================================*/
/************************************************/
/* */
/* Lcd_Ascii: 显示8x16点阵Ascii码 */
/* */
/* Row: 行坐标<0-29(240/8-1)> */
/* Column: 列坐标<0-39(320/8-1)> */
/* Character: Ascii码 */
/* Flag: 反显标志 */
/* Flag=Normal 正常显示 */
/* Flag=Abnormal 反白显示 */
/* */
/************************************************/
void Lcd_Ascii(unsigned int Row,unsigned int Column,unsigned int Character,unsigned int Flag)
{
pointer *Data_Add;
Data_Add=Ascii+((Character-0x20)<<4);
Lcd_Cctw(Row,Column,8,16,Data_Add,Flag);
}
/*==============================================*/
/************************************************/
/* */
/* Lcd_Out_Bmp: 显示位图子程序 */
/* Dot_X: x坐标(0-319) */
/* Dot_Y: y坐标(0-239) */
/* (Dot1,Dot2): 位图点阵格式 */
/* Data_Add: 位图数据首地址 */
/* Flag: 反显标志 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -