#include "LCD12864.h"
unsigned char code picture_human[] = {0x60,0x60,0xF0,0xF0,0xF0,0x60,0x60,0x60};//8*8小人图形
unsigned char code picture[]={0x80,0xC0,0xE0,0xF0,0xF0,0xE0,0xC0,0x80};//三角光标
unsigned char code pictureclr[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//清三角光标
//////////////串口写一字节///////
void LCD12864_Uart_WritByte(unsigned char byte)
{
unsigned char bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++)
{
LCD12864_RW_PORT = (byte & 0x80);
byte = (byte << 1);
LCD12864_E_PORT = 0; //时钟
LCD12864_E_PORT = 1; //时钟
}
}
void LCD12864_Uart_COM_Write( unsigned char byte)
{
LCD12864_RS_PORT = 1; //CS
//////////////5个1
LCD12864_Uart_WritByte(0xf8); //11111 000;
///////////高4位/////////////////////
LCD12864_Uart_WritByte(byte&0xf0);
LCD12864_Uart_WritByte((byte<<4)&0xf0);
LCD12864_RS_PORT = 0; //CS
}
void LCD12864_Uart_Data_Write( unsigned char byte)
{
LCD12864_RS_PORT = 1; //CS
lcd_delay_ms(1);
//////////////5个1
LCD12864_Uart_WritByte(0xfa); //11111 010;
///////////高4位/////////////////////
LCD12864_Uart_WritByte(byte&0xf0);
lcd_delay_ms(1);
///////////低4位///////////
LCD12864_Uart_WritByte((byte<<4)&0xf0);
LCD12864_RS_PORT = 0; //CS
}
//*************************************************************************************
//写连续字符函数
//*************************************************************************************
void LCD12864_Uart_write_word(unsigned char *s)
{
for(;*s!=0;s++)
{
LCD12864_Uart_Data_Write(*s);
}
}
//********************************************************************
//1MS为单位的延时程序,不准确
//********************************************************************
void lcd_delay_ms(unsigned char x)
{
unsigned char j;
while(x--){
for(j=0;j<125;j++)
{;}
}
}
//********************************************************************
//LCD12864初始化
//********************************************************************
void LCD12864_Uart_Init()
{
// LCD_PSB = 0;
lcd_delay_ms(10); /*适当延时待LCD自动复位完成*/
LCD12864_Uart_COM_Write(0x30); /*基本指令模式*/
lcd_delay_ms(1);
LCD12864_Uart_COM_Write(0x04); /*光标右移一位*/
lcd_delay_ms(1);
LCD12864_Uart_COM_Write(0x0c); /*显示开,关光标*/
lcd_delay_ms(1);
LCD12864_Uart_COM_Write(0x01); /*显示清屏*/
lcd_delay_ms(10);
}
void LCD12864_Uart_Setposition(unsigned char xpos,unsigned char ypos)
{
unsigned char xy_pos;
if((xpos>=8)||(ypos>=4) ) return; /*X位置超出显示范围退出*/
if(ypos==0) xy_pos = 0x80 + xpos;
else if(ypos==1) xy_pos = 0x90 + xpos; /*计算转换地址*/
else if(ypos==2) xy_pos = 0x88 + xpos;
else if(ypos==3) xy_pos = 0x98 + xpos;
LCD12864_Uart_COM_Write(xy_pos); /*写地址*/
}
void LCD12864_Uart_String(unsigned char x,unsigned char y,unsigned char* str)
{
LCD12864_Uart_COM_Write(0x30);
LCD12864_Uart_Setposition(x,y);
lcd_delay_ms(1);
LCD12864_Uart_write_word(str);
}
//清空GDRAM,总共就是写1KB的0x00。
void LCD12864_clearGDR(void)
{
unsigned char x,y;
for(y=0;y<32;y++)
{
for(x=0;x<16;x++)
{
LCD12864_Uart_COM_Write(0x34);
LCD12864_Uart_COM_Write(y+0x80); //行地址
LCD12864_Uart_COM_Write(x+0x80); //列地址
LCD12864_Uart_COM_Write(0x30);
LCD12864_Uart_Data_Write(0);
LCD12864_Uart_Data_Write(0x00);
}
}
// LCD12864_Uart_COM_Write(0x34);
LCD12864_Uart_COM_Write(0x36);
}
/*******************************************************
*-函数名称 :
*-函数作用 :画一张指定大小的图片
*-参数 :位置:position_x,position_y 高度:datH 宽度datL,图片数组picture
*-返回值 :
*-备注 :
*******************************************************/
void LCD12864_DrawPicture(unsigned char position_x,unsigned char position_y,\
unsigned char datH,unsigned char datL,unsigned char* picture)
{
unsigned char zuobiao_x0=0,zuobiao_x1=0,zuobiao_y0=0,zuobiao_y1=0;
unsigned char datL_left=0,datL_right=0,datL_size=0;
unsigned int dat=0,dat0=0,dat1=0;
unsigned char i=0,j=0;
zuobiao_x0 = position_x/16; //列地址的分量
zuobiao_x1 = position_x%16; //字节中的位置
datL_left = 16 - zuobiao_x1;
//变换数据并画点
LCD12864_Uart_COM_Write(0x34); //执行扩展指令,关画图显示
for(j=0;j<datH;j++)
{
zuobiao_y0 = (position_y+j)/32; //在上行0,在下行1
zuobiao_y1 = (position_y+j)%32; //行地址
LCD12864_Uart_COM_Write(0x80 + zuobiao_y1); //行地址
LCD12864_Uart_COM_Write(0x80 + zuobiao_y0*8 + zuobiao_x0); //列地址
if(datL>datL_left)//图片大小超出了剩余容量
{
datL_right = (datL - datL_left)%16;
datL_size = (datL - datL_left)/16;
dat = (dat>>datL_left)<<datL_left;
LCD12864_Uart_COM_Write(0x80 + zuobiao_y1); //行地址
LCD12864_Uart_COM_Write(0x80 + zuobiao_y0*8 + zuobiao_x0); //列地址
if(datL>8)
{
//写头
dat1 = ((unsigned int)(*(picture+j*(datL/8)+0))<<8)|(*(picture+j*(datL/8)+1));
dat0 = (unsigned int)(dat1>>zuobiao_x1);
dat |= dat0;
LCD12864_Uart_Data_Write((unsigned char)(dat>>8));
LCD12864_Uart_Data_Write((unsigned char)dat);
dat = dat1<<datL_left;
//写体
for(i=0;i<datL_size;i++)
{
dat1 = ((unsigned int)(*(picture+j*(datL/8)+2*(i+1)))<<8)|(*(picture+j*(datL/8)+2*(i+1)+1));
dat0 = dat1>>zuobiao_x1;
dat |= dat0;
LCD12864_Uart_Data_Write((unsigned char)(dat>>8));
LCD12864_Uart_Data_Write((unsigned char)dat);
dat = dat1<<datL_left;
}
//写尾
//读当前地址数据
if(datL_right>0)
{
if(datL/8%2) //剩一个8位
{
dat = dat|((unsigned int)(*(picture+j*(datL/8)+2*(datL_size+1)))<<(datL_left-8));
}
LCD12864_Uart_COM_Write(0x80 + zuobiao_y1); //行地址
LCD12864_Uart_COM_Write(0x80 + zuobiao_y0*8 + zuobiao_x0 + datL_size + 1); //列地址
dat1 = ((unsigned int)(dat1<<(zuobiao_x1+(datL/8%2)*8)))>>(zuobiao_x1+(datL/8%2)*8);
dat |= dat1;
LCD12864_Uart_COM_Write(0x80 + zuobiao_y1); //行地址
LCD12864_Uart_COM_Write(0x80 + zuobiao_y0*8 + zuobiao_x0 + datL_size + 1); //列地址
LCD12864_Uart_Data_Write((unsigned char)(dat>>8));
LCD12864_Uart_Data_Write((unsigned char)dat);
}
}
else
{
//写头
dat0 = ((unsigned char)(*(picture+j)))>>(8-datL_left);
dat |= dat0;
LCD12864_Uart_Data_Write((unsigned char)(dat>>8));
LCD12864_Uart_Data_Write((unsigned char)dat);
dat =((unsigned int)(*(picture+j)))<<(8+datL_left);
LCD12864_Uart_COM_Write(0x80 + zuobiao_y1); //行地址
LCD12864_Uart_COM_Write(0x80 + zuobiao_y0*8 + zuobiao_x0 + 1); //列地址
dat1 = ((unsigned int)(dat1<<(datL-datL_left)))>>(datL-datL_left);
dat |= dat