void LCD12864Init(void) //LCD12864字库初始化程序
{
Delay_nms(100); //大于40MS的延时程序
PSB = 1; //设置为8BIT并口工作模式
Delay_nus(1);
RST = 0;
Delay_nms(100); //大于40MS的延时程序
RST = 1; //复位置高
Delay_nms(100);
Delay_nms(50);
LCD12864_Write(COMMAND,0x30); //Extended Function Set :8BIT设置,RE=0: basic instruction set, G=0 :graphic display OFF
Delay_nms(120); //大于100uS的延时程序
LCD12864_Write(COMMAND,0x30); //Function Set
Delay_nms(40); //大于37uS的延时程序
LCD12864_Write(COMMAND,0xC0); //Display on Control
Delay_nms(120); //大于100uS的延时程序
LCD12864_Write(COMMAND,0x10); //Cursor Display Control光标设置
Delay_nms(120); //大于100uS的延时程序
LCD12864_Write(COMMAND,0x0C); //Display Control,D=1,显示开
Delay_nms(120); //大于100uS的延时程序
LCD12864_Write(COMMAND,0x01); //Display Clear
Delay_nms(15); //大于10mS的延时程序
LCD12864_Write(COMMAND,0x06); //Enry Mode Set,光标从右向左加1位移动
Delay_nms(120); //大于100uS的延时程序
}
void LCD12864WaitForReady(void)
{
unsigned char temp;
#ifdef _Use_LCD12864OnlyWrite
Delay_nus(30);
#else
LCD12864_SetReadData;
LCD12864_RS = 0;
LCD12864_RW = 1;
do
{
LCD12864_EN = Enable;
Delay_nus(1);
temp = LCD12864_Data_PIN;
Delay_nus(1);
LCD12864_EN = Disable;
}while (temp & 0x80); //RW=1,读PD7,为0表示空闲;
LCD12864_EN = Disable;
LCD12864_SetWriteData;
#endif
}
void LCD12864_Write(unsigned char cmdordata,unsigned char Data) //传送数据或者命令,当cmdordata=0是,传送命令,当cmdordata=1,传送数据.
{
LCD12864WaitForReady();
LCD12864_EN = 0;
LCD12864_RW = 0;
Delay_nus(1);
if(cmdordata == 0)
{
LCD12864_RS = 0;
}
else
{
LCD12864_RS = 1;
}
Delay_nus(1);
LCD12864_Data_Port = Data;
LCD12864_EN = 1;
Delay_nus(1);
LCD12864_EN = 0;
}
unsigned char LCD12864_Read(void)
{
unsigned char data;
LCD12864WaitForReady();
LCD12864_SetReadData;
LCD12864_RS = 1;
Delay_nus(1);
LCD12864_RW = 1;
Delay_nus(1);
LCD12864_EN = 1;
Delay_nus(1);
data = LCD12864_Data_PIN;
LCD12864_EN = 0;
LCD12864_SetWriteData;
return data;
}
void LCD12864_Clean(void)
{
unsigned char x,y;
LCD12864_Write(COMMAND,GRAPH_ON);
LCD12864Init(); //设置扩展指令集,按手册说明,仅设置了绘图位,
LCD12864_Write(COMMAND,GRAPH_ON); //需要两次,本次设置扩展指令集。
for (y=0;y<32;y++)
{
LCD12864_Write(COMMAND,0x80+y); //设置y=0000+000x,y+1则往下一行
LCD12864_Write(COMMAND,0x80); //设置x=0000 0000
for (x=0;x<16;x++)
{
LCD12864_Write(DATA,0x00); //高字节数据
LCD12864_Write(DATA,0x00); //低字节数据
}
}
}
void Serial_DDRAM_Address_Set(unsigned char ucDDramAdd)
{
LCD12864_Write(COMMAND,BASIC_FUNCTION); //基本指令集
LCD12864_Write(COMMAND,ucDDramAdd); //设定DDRAM地址到地址计数器AC
}
void Overturn_Char(unsigned char x,unsigned char y,unsigned char wide,unsigned char cor)
{
unsigned char i,base_x,colour;
if(cor == 1) colour = 0xFF;
if(cor == 0) colour = 0x00;
LCD12864_Write(COMMAND,GRAPH_OFF);
if(y == 1 || y == 2) base_x = 0x80;
if(y == 3 || y == 4) base_x = 0x88;
if(x % 2)
{
for(i = 0;i < wide; i ++)
{
LCD12864_Write(COMMAND,0X80 - 16 + y * 16 + i);
LCD12864_Write(COMMAND,base_x+(x/2));
LCD12864_Write(DATA,colour);
LCD12864_Write(DATA,0x00);
}
}
else
{
for(i = 0;i < wide; i ++)
{
LCD12864_Write(COMMAND,0X80 - 16 + y * 16 + i);
LCD12864_Write(COMMAND,base_x+(x/2)-1);
LCD12864_Write(DATA,0x00);
LCD12864_Write(DATA,colour);
}
}
LCD12864_Write(COMMAND,GRAPH_ON);
}
void Overturn_Font(unsigned char x,unsigned char y,unsigned char wide,unsigned char cor)
{
unsigned char i,base_x,colour;
if(cor == 1) colour = 0xFF;
if(cor == 0) colour = 0x00;
LCD12864_Write(COMMAND,GRAPH_OFF);
if(y == 1 || y == 2) base_x = 0x80;
if(y == 3 || y == 4) base_x = 0x88;
for(i = 0;i < wide;i ++)
{
LCD12864_Write(COMMAND,0X80 - 16 + y * 16 + i); //- 16 + y * 16 + i);
LCD12864_Write(COMMAND,base_x+x-1);
LCD12864_Write(DATA,colour);
LCD12864_Write(DATA,colour);
}
LCD12864_Write(COMMAND,GRAPH_ON);
}
void GUI_Point(unsigned char y,unsigned char x,unsigned colour)
{
unsigned char GDRAM_hbit,GDRAM_lbit;
unsigned char x_Dyte,x_byte; //定义列地址的字节位,及在字节中的哪1位
unsigned char y_Dyte,y_byte;
x--;y--; //定义为上下两个屏(取值为0,1),行地址(取值为0~31)
x_Dyte=y/16; //计算在16个字节中的哪一个
x_byte=y&0x0f; //计算在该字节中的哪一位
y_Dyte=x/32; //0为上半屏,1为下半屏
y_byte=x&0x1f; //计算在0~31当中的哪一行
LCD12864_Write(COMMAND,0x80+y_byte); //设定行地址(y坐标)
LCD12864_Write(COMMAND,0x80+x_Dyte+8*y_Dyte); //设定列地址(x坐标),并通过8*y_Dyte选定上下屏
LCD12864_Read();
GDRAM_hbit=LCD12864_Read(); //读取当前显示高8位数据
GDRAM_lbit=LCD12864_Read(); //读取当前显示低8位数据
Delay_nus(1);
if(colour==1)
{
LCD12864_Write(COMMAND,0x80+y_byte); //设定行地址(y坐标)
LCD12864_Write(COMMAND,0x80+x_Dyte+8*y_Dyte); //设定列地址(x坐标),并通过8*y_Dyte选定上下屏
Delay_nus(1);
if(x_byte<8) //判断其在高8位,还是在低8位
{
LCD12864_Write(DATA,GDRAM_hbit|(0X01<<(7-x_byte))); //显示GDRAM区高8位数据
LCD12864_Write(DATA,GDRAM_lbit); //显示GDRAM区低8位数据
}
else
{
LCD12864_Write(DATA,GDRAM_hbit);
LCD12864_Write(DATA,GDRAM_lbit|(0x01<<(15-x_byte)));
}
}
else
{
LCD12864_Write(COMMAND,0x80+y_byte); //设定行地址(y坐标)
LCD12864_Write(COMMAND,0x80+x_Dyte+8*y_Dyte); //设定列地址(x坐标),并通过8*y_Dyte选定上下屏
Delay_nus(1);
if(x_byte<8) //判断其在高8位,还是在低8位
{
LCD12864_Write(DATA,GDRAM_hbit & ~(0X01<<(7-x_byte))); //显示GDRAM区高8位数据
LCD12864_Write(DATA,GDRAM_lbit); //显示GDRAM区低8位数据
}
else
{
LCD12864_Write(DATA,GDRAM_hbit);
LCD12864_Write(DATA,GDRAM_lbit & ~(0x01<<(15-x_byte)));
}
}
}
void GUI_Line(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char colour)
{
int temp;
int dx,dy; //定义起点到终点的横、纵坐标增加值
int s1,s2,status,i;
int Dx,Dy,sub;
dx = x1 - x0;
if(dx >= 0) //X的方向是增加的
s1 = 1;
else //X的方向是降低的
s1 = -1;
dy = y1 - y0; //判断Y的方向是增加还是降到的
if(dy >= 0)
s2 = 1;
else
s2 =- 1;
Dx = abs(x1-x0); //计算横、纵标志增加值的绝对值
Dy = abs(y1-y0);
if(Dy > Dx)
{ //以45度角为分界线,靠进Y轴是status=1,靠近X轴是status=0
temp = Dx;
Dx = Dy;
Dy = temp;
status = 1;
}
else
status = 0;
sub = Dy + Dy - Dx; //第1次判断下个点的位置
for(i = 0;i < Dx;i ++)
{
GUI_Point(x0,y0,colour); //画点
if(sub >= 0)
{
if(status == 1) //在靠近Y轴区,x值加1
x0 += s1;
else //在靠近X轴区,y值加1
y0 += s2;
sub -= (Dx + Dx); //判断下下个点的位置
}
if(status == 1)
y0 += s2;
else
x0 += s1;
sub += Dy + Dy;
}
}
void plotC(int x,int y,int xc,int yc,unsigned char colour)
{
GUI_Point(xc+x,yc+y,colour);
GUI_Point(xc+x,yc-y,colour);
GUI_Point(xc-x,yc+y,colour);
GUI_Point(xc-x,yc-y,colour);
GUI_Point(xc+y,yc+x,colour);
GUI_Point(xc+y,yc-x,colour);
GUI_Point(xc-y,yc+x,colour);
GUI_Point(xc-y,yc-x,colour);
}
void GUI_Circle(int xc,int yc,int r,unsigned char colour)
{
int x,y,d;
y = r;
d = 3 - (r + r);
x = 0;
while(x <= y)
{
plotC(x,y,xc,yc,colour);
if(d < 0)
d += (x + x + x + x) + 6;
else
{
d+=((x - y) + (x - y) + (x - y) + (x - y)) + 10;
y = y - 1;
}
x = x + 1;
}
}
void GUI_Full(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char colour)
{
unsigned char i,j;
for(j = y0;j <= y1;j ++)
for(i = x0;i <= x1;i ++)
GUI_Point(i,j,colour);
}
上一篇:带字符型12864单片机显示程序
下一篇:单片机做红外遥控解码器
推荐阅读最新更新时间:2024-03-16 13:50