LCD12864.H头文件如下
- /************************************************************
- 串行连接操作程序
- 对外函数列表:
- void LCD_Init( void ); //初始化
- void LCD_PutStr(unsigned char row,unsigned char col,unsigned char *puts); //写入文本
- void LCD_PutBMP(unsigned char *puts); //写入半屏图片
- void Disp_Img(unsigned const char *img) //写整屏图片
- void LCD_ClearTXT( void ); //清除文本
- void LCD_ClearBMP( void ); //清除图片
- ***********************************************************/
- #define uchar unsigned char
- #define uint unsigned int
- #define LCDNOP() NOP();NOP()
- //以下配置IO管脚
- //========================================================*/
- #define OUTLCDSCLK() DDRD |= (1<<6) //设置输出
- #define SETLCDSCLK() PORTD |= (1<<6) //拉高
- #define CLRLCDSCLK() PORTD &=~(1<<6) //拉低
- #define SEL_CS() DDRD |=BIT(3);PORTD |=BIT(3) //片选拉高
- #define CLR_CS() DDRD |=BIT(3);PORTD &= ~BIT(3) //片选拉低
- #define OUTLCDSID() DDRD |= (1<<4) //设置输出
- #define INLCDSID() DDRD &= ~(1 << 4); PORTD |= (1 << 4) //设置输入
- #define SETLCDSID() PORTD |= (1 << 4) //拉高
- #define CLRLCDSID() PORTD &= ~(1 << 4) //拉低
- #define GETLCDSID() ((PIND & BIT(4)) >> 4) //读IO值
- const unsigned char AC_TABLE[]={
- 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行汉字位置
- 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行汉字位置
- 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, //第三行汉字位置
- 0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, //第四行汉字位置
- };
- unsigned char str2[]="欢迎光临sonsan!!";
- unsigned char str1[]="黄河远上白云间,一片孤城万仞山。羌笛何须怨杨柳,春风不度玉门关。";
- //维捷登测试架专用延时函数
- void DelayKey(uint ms)
- {
- uint i;
- while(ms--)
- {
- for(i=1;i<(uint)(1*143-2);i++) //1为1MHZ
- ;
- }
- }
- //串口发送一个字节
- void LCD_WriteByte(unsigned char Dbyte)
- {
- unsigned char i;
- OUTLCDSCLK();
- OUTLCDSID();
- for(i=0;i<8;i++)
- {
- CLRLCDSCLK();
- Dbyte=Dbyte<<1; //左移一位
- if (SREG & 1)
- SETLCDSID();
- else
- CLRLCDSID();
- SETLCDSCLK();
- LCDNOP();
- CLRLCDSCLK();
- }
- return;
- }
- //串口接收一个字节
- //仅在读取数据的时候用到
- //而读出的数据是一次只能读出4bit的
- unsigned char LCD_ReadByte(void)
- {
- unsigned char i,temp1,temp2;
- temp1=temp2=0;
- OUTLCDSCLK();
- INLCDSID();
- for(i=0;i<8;i++)
- {
- temp1=temp1<<1;
- CLRLCDSCLK();
- LCDNOP();
- SETLCDSCLK();
- LCDNOP();
- CLRLCDSCLK();
- if(GETLCDSID()) temp1++;
- }
- for(i=0;i<8;i++)
- {
- temp2=temp2<<1;
- CLRLCDSCLK();
- LCDNOP();
- SETLCDSCLK();
- LCDNOP();
- CLRLCDSCLK();
- if(GETLCDSID()) temp2++;
- }
- return ((0xf0&temp1)+(0x0f&temp2));
- }
- void LCD_CheckBusy( void )
- {
- LCDNOP();
- return;
- do LCD_WriteByte(0xfc); //11111,RW(1),RS(0),0
- while(0x80&LCD_ReadByte()); //BF(.7)=1 Busy
- }
- void LCD_WriteCmd( unsigned char Cbyte )
- {
- SEL_CS();
- LCD_CheckBusy();
- LCD_WriteByte(0xf8); //11111,RW(0),RS(0),0
- LCD_WriteByte(0xf0&Cbyte); //高四位
- DelayKey(5);
- LCD_WriteByte(0xf0&Cbyte<<4);//低四位(先执行<<)
- DelayKey(5);
- CLR_CS();
- }
- void LCD_WriteData( unsigned char Dbyte )
- {
- SEL_CS();
- LCD_CheckBusy();
- LCD_WriteByte(0xfa); //11111,RW(0),RS(1),0
- LCD_WriteByte(0xf0&Dbyte); //高四位
- DelayKey(5);
- LCD_WriteByte(0xf0&Dbyte<<4);//低四位(先执行<<)
- DelayKey(5);
- CLR_CS();
- }
- /*显示屏单字符写入函数*/
- /*void LCD_write_char(unsigned char x,unsigned char y,unsigned char *data) {
- switch (y)
- {
- case 0:
- LCD_WriteCmd(0x80 + x);
- break;
- case 1:
- LCD_WriteCmd(0x90 + x);
- break;
- case 2:
- LCD_WriteCmd(0x88 + x);
- break;
- case 3:
- LCD_WriteCmd(0x98 + x);
- break;
- default:
- break;
- }
- LCD_WriteData(*data);
- }*/
- /**********************************************************
- ;子程序名:void write_single(uchar y,uchar x,uchar *p)
- ;功 能:写单个字符
- ;说 明:直接调用
- ;调 用:write_single(uchar y,uchar x,uchar *p)
- ;**********************************************************/
- void write_single(uchar y,uchar x,uchar *p)
- {
- switch (y)
- {
- case 1:LCD_WriteCmd(0x80+x-1);break;
- case 2:LCD_WriteCmd(0x90+x-1);break;
- case 3:LCD_WriteCmd(0x88+x-1);break;
- case 4:LCD_WriteCmd(0x98+x-1);break;
- default:break;
- }
- LCD_WriteData(*p);
- }
- unsigned char LCD_ReadData( void )
- {
- LCD_CheckBusy();
- LCD_WriteByte(0xfe); //11111,RW(1),RS(1),0
- return LCD_ReadByte();
- }
- void LCD_Init( void )
- {
- DelayKey(2);
- LCD_WriteCmd(0x30); //8BitMCU,基本指令集合
- DelayKey(2);
- LCD_WriteCmd(0x30); //8BitMCU
- DelayKey(5);
- LCD_WriteCmd(0x0C); //显示ON,游标OFF,游标位反白OFF
- DelayKey(5);
- LCD_WriteCmd(0x01); //清屏,AC归0
- DelayKey(5);
- LCD_WriteCmd(0x02); // AC归0,不改变DDRAM内容
- DelayKey(5);
- LCD_WriteCmd(0x06); //写入时,游标右移动
- DelayKey(2);
- //LCD_WriteCmd(0x80);
- }
- //文本区清RAM函数
- void LCD_ClearTXT( void )
- {
- unsigned char i;
- LCD_WriteCmd(0x30); //8BitMCU,基本指令集合
- DelayKey(5);
- LCD_WriteCmd(0x80); //AC归起始位
- DelayKey(5);
- for(i=0;i<64;i++)
- LCD_WriteData(0x20);
- }
- //图形区和文本区显示在两个不同的RAM区
- //图形区清RAM函数
- void LCD_ClearBMP( void )
- {
- unsigned char i,j;
- LCD_WriteCmd(0x34); //8Bit扩充指令集,即使是36H也要写两次
- LCD_WriteCmd(0x36); //绘图ON,基本指令集里面36H不能开绘图
- for(i=0;i<32;i++) //12864实际为256x32
- {
- LCD_WriteCmd(0x80|i); //行位置
- LCD_WriteCmd(0x80); //列位置
- for(j=0;j<32;j++) //256/8=32 byte
- LCD_WriteData(0);
- }
- }
- void LCD_PutStr(unsigned char row,unsigned char col,unsigned char *puts)
- {
- LCD_WriteCmd(0x30); //8BitMCU,基本指令集合
- LCD_WriteCmd(AC_TABLE[8*row+col]); //起始位置
- while(*puts != '\0') //判断字符串是否显示完毕
- {
- if(col==8) //判断换行
- { //若不判断,则自动从第一行到第三行
- col=0;
- row++;
- }
- if(row==4) row=0; //一屏显示完,回到屏左上角
- LCD_WriteCmd(AC_TABLE[8*row+col]);
- LCD_WriteData(*puts); //一个汉字要写两次
- puts++;
- if (*puts != '\0')
- {
- LCD_WriteData(*puts);
- puts++;
- }else{break;}
- col++;
- }
- }
- void LCD_PutBMP(unsigned const char *puts) //必须加const 只能写上下半屏相同的图片
- {
- unsigned int x=0;
- unsigned char i,j;
- LCD_WriteCmd(0x34); //8Bit扩充指令集,即使是36H也要写两次
- LCD_WriteCmd(0x36); //绘图ON,基本指令集里面36H不能开绘图
- for(i=0;i<32;i++) //12864实际为256x32
- {
- LCD_WriteCmd(0x80|i); //行位置
- LCD_WriteCmd(0x80); //列位置
- for(j=0;j<32;j++) //256/8=32 byte
- { //列位置每行自动增加
- LCD_WriteData(puts[x]);
- x++;
- }
- }
- }
- void Disp_Img(unsigned const char *img) //写整屏图片
- { unsigned int j=0;
- unsigned char x,y,i;
- for(i=0;i<9;i+=8)
- for(y=0;y<32;y++)/*原来 为 y<26 ,上下两个半屏不能正常对接显示,导致显示的图片中间有空隙*/
- for(x=0;x<8;x++)
- { LCD_WriteCmd(0x36);//功能设置---8BIT控制界面,扩充指令集
- LCD_WriteCmd(y+0x80); //行地址
- LCD_WriteCmd(x+0x80+i); //列地址
- LCD_WriteCmd(0x30);
- LCD_WriteData(img[j++]);
- LCD_WriteData(img[j++]);
- }
-
- }
- //维捷登测试用点阵显示
- void DisplayDots(unsigned char DotByte)
- {
- unsigned char i,j;
- LCD_WriteCmd(0x34); //8Bit扩充指令集,即使是36H也要写两次
- LCD_WriteCmd(0x36); //绘图ON,基本指令集里面36H不能开绘图
- for(i=0;i<32;i++) //12864实际为256x32
- {
- LCD_WriteCmd(0x80|i); //行位置
- LCD_WriteCmd(0x80); //列位置
- for(j=0;j<32;j++) //256/8=32 byte
- { //列位置每行自动增加
- LCD_WriteData(DotByte);
- }
- DotByte=~DotByte;
- }
- }
- //********************************************************//
- //***** 转换的文件:E:\!Program\!BmpSample\25632.bmp
- //***** 源图形宽度 * 高度:256 * 32
- //***** 调整后宽度 * 高度:256 * 32
- //***** 字模格式:横向取模,冗余模式,字节正序,正色取模,
- //***** 点阵转换时间:2005/03/ 2 16:4:32
- //***** 位图点阵占用1024字节
- //********************************************************//
- flash unsigned char bmp1[]=
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x1F,0x80,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0xC0,0x07,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x01,0x07,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x8E,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x04,0xE0,0x00,0x00,0x00,0x00,0x60,0x00,0x04,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x01,0x88,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x08,0x38,0xE0,0x00,0x00,0x39,0x00,0x00,0x00,
- 0x00,0x00,0x06,0x60,0x00,0x00,0x00,0x0C,0x1C,0xC0,0x00,0x00,0x04,0x80,0x00,0x00,
- 0x00,0x00,0x09,0x80,0x00,0x00,0x00,0x0C,0x30,0xC0,0x00,0x00,0x01,0xB0,0x00,0x00,
- 0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x08,0x7B,0x80,0x08,0x00,0x00,0x48,0x00,0x00,
- 0x00,0x00,0x58,0x00,0x01,0x86,0x00,0x00,0x03,0x80,0x0C,0x00,0x00,0x1E,0x00,0x00,
- 0x00,0x00,0x60,0x00,0x01,0x86,0x00,0x00,0x03,0x00,0x04,0x00,0x00,0x07,0x80,0x00,
- 0x00,0x00,0x80,0x00,0x01,0x86,0x00,0x00,0x03,0x00,0x04,0x00,0x00,0x07,0xC0,0x00,
- 0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x01,0xE0,0x00,
- 0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x78,0x00,
- 0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x78,0x00,
- 0x00,0x78,0x30,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x1E,0x00,
- 0x00,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x07,0x00,0x1F,0x00,
- 0x01,0xE0,0x00,0xC7,0x80,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x06,0x00,0x07,0x00,
- 0x01,0xE0,0x00,0x19,0xC0,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x1C,0x00,0x07,0x00,
- 0x03,0x80,0x00,0x8F,0x80,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x0C,0x00,0x01,0xC0,
- 0x03,0x80,0x00,0x03,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,
- 0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,
- 0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,
- 0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,
- 0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x78,
- 0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,
- 0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,
- 0x1E,0x00,0x3E,0x00,0x00,0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x38,0x00,0x7C,
- 0x1E,0x00,0x36,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x18,0x00,0x7C,
- 0x1E,0x00,0x1E,0x00,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,0x1C,0x00,0x7C,
- 0x1E,0x00,0x1C,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x78,
- 0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,
- 0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,
- 0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,
- 0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,
- 0x07,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,
- 0x07,0x80,0x00,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,
- 0x07,0x80,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xE0,
- 0x07,0xC0,0x00,0x70,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x07,0xC0,
- 0x03,0xE0,0x00,0xF8,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x0C,0x04,0x07,0xC0,
- 0x01,0xE0,0x00,0xF8,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x3E,0x00,0x07,0xC0,
- 0x00,0xF0,0x08,0x78,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x1F,0x80,
- 0x00,0xF8,0x20,0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x1E,0x00,
- 0x00,0x3C,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,
- 0x00,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x00,
- 0x00,0x1F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF8,0x00,
- 0x00,0x0F,0xC0,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x07,0xF0,0x00,
- 0x00,0x07,0xE0,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x0F,0xE0,0x00,
- 0x00,0x01,0xF8,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x1F,0x80,0x00,
- 0x00,0x00,0xFE,0x00,0x00,0x40,0x00,0x07,0x80,0x00,0x1E,0x00,0x00,0x7F,0x00,0x00,
- 0x00,0x00,0x3F,0x80,0x00,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x03,0xF8,0x00,0x00,
- 0x00,0x00,0x0F,0xE0,0x02,0x00,0x00,0x06,0xC0,0x00,0x00,0x00,0x0F,0xE0,0x00,0x00,
- 0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x03,0x80,0x00,0x08,0x00,0x3F,0xC0,0x00,0x00,
- 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0xFE,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xF8,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x03,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xC0,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x7F,0xE0,0x00,0x00,0x00,0x00,0x0F,0xFC,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x07,0xFF,0x80,0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xE0,0x3F,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- };
- #pragma data:data //设置回到数据存储器区
复制代码 |