12864A液晶驱动1616汉字+816数字字母.docx
《12864A液晶驱动1616汉字+816数字字母.docx》由会员分享,可在线阅读,更多相关《12864A液晶驱动1616汉字+816数字字母.docx(29页珍藏版)》请在冰豆网上搜索。
![12864A液晶驱动1616汉字+816数字字母.docx](https://file1.bdocx.com/fileroot1/2023-2/8/3c6a1873-0595-437d-92db-c22cfb9ef8fd/3c6a1873-0595-437d-92db-c22cfb9ef8fd1.gif)
12864A液晶驱动1616汉字+816数字字母
//适用于12864A无字库液晶(左右屏幕)
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
/******************************************************************/
sbitcs1=P2^1;
sbitcs2=P2^2;
sbitd_i=P2^0;
sbitenb=P2^4;
sbitr_w=P2^3;
sbitrst=P2^5;
sbitkey=P3^7;
sbitbusy_bit=P2^7;
sbitrst_bit=P2^4;
//SMG12864A产品引脚说明及演示连线
//PIN1:
VSS[电源地]
//PIN2:
VDD[电源正极]
//PIN3:
Vo[LCD偏压输入]
//PIN4:
d_i[数据/命令选择端1:
数据0:
命令]接P2^0
//PIN5:
r_w[读写信号选择端1:
读操作0:
写操作]接P2^3
//PIN6:
enb[使能信号输入高有效]接P2^4
//PIN7:
DB0[DataI/O]接P1^0
//PIN8:
DB1[DataI/O]接P1^1
//PIN9:
DB2[DataI/O]接P1^2
//PIN10:
DB3[DataI/O]接P1^3
//PIN11:
DB4[DataI/O]接P1^4
//PIN12:
DB5[DataI/O]接P1^6
//PIN13:
DB6[DataI/O]接P1^7
//PIN14:
DB7[DataI/O]接P1^8
//PIN15:
CS1[片选1信号输入高有效]接P2^1
//PIN16:
CS2[片选2信号输入高有效]接P2^2
//PIN17:
RST[复位信号输入(H:
正常工作,L:
复位)]接P2^5
//PIN18:
VEE[LCD驱动负压输出(-5V)]
//PIN19:
BLA[背光源正极]
//PIN20:
BLK[背光源负极]
uintcodetable1[]={0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf};//页选项(共8页)
uintcodetable2[]={0x40,0x50,0x60,0x70};//中文列选项(半屏各4个字,四个字的Y地址首列)
uintcodetable3[]={0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78};//中文列选项(半屏各8个字母,八个字母的Y地址首列)
voidinit();//初始化函数
voidwrite_com1(unsignedchard);//左半屏的写命令程序
voidwrite_data1(unsignedchard);//左半屏的写数据程序
voidwrite_com2(unsignedchard);//右半屏的写命令程序
voidwrite_data2(unsignedchard);//右半屏的写数据程序
voidwrite_ch_add1(uintz);//写汉字的页地址(上半个汉字的地址)(共四行八列32个汉字,1-32编号)
voidwrite_ch_add2(uintz);//写汉字的页地址(下半个汉字的地址)(共四行八列32个汉字,1-32编号)
voidwrite_ch(uintadd,ucharz[]);//在确定的位置写字库中的汉字
voidwrite_en_add1(uintz);//写字母的页地址(上半个字母的地址)(共四行十六列64个字母,1-64编号)
voidwrite_en_add2(uintz);//写字母的页地址(下半个字母的地址)(共四行十六列64个字母,1-64编号)
voidwrite_en(uintadd,ucharz[]);//在确定的位置写字库中的字母
ucharread1(void);//读左半屏图形数据子程序
ucharread2(void);//读右半屏图形数据子程序
uchar*read_ch(uint);//读在x位置上的汉字的字模(32个字节)
uchar*read_en(uint);//读在x位置上的字母的字模(16个字节)
voidcheck_busy1(void);//控制器1忙检测子程序
voidcheck_busy2(void);//控制器2忙检测子程序
voidbutton(uintz);//开关显示数据改变件,z=1为开;z=0为关;
voidclc(void);//清屏
voiddelay(ucharz);//延时程序
voidtest();//测试程序,屏幕中按顺序写0-9,a-z,A-Z//需要时调出
uintbutton_flag;//button_flag=0时,关闭写程程序,数据(关闭后注意要打开)
ucharread_zimo[32];//read_zimo为液晶读回来的字模,需要定义为全局变量,注意:
程序执行中,不能改变rom中的值,所以a不能定义为code
//主函数
voidmain()
{
uchar*zz;
init();
clc();
write_ch(1,xiao);
write_ch(2,feng);
write_en(5,en_ll);
write_en(6,en_o);
write_en(7,en_v);
write_en(8,en_e);
write_ch(5,xiao);
write_ch(6,mei);
while
(1);
}
voidinit()
{
button_flag=1;
rst=0;
rst=1;
write_com1(0xc0);
write_com1(0x3f);
write_com2(0xc0);
write_com2(0x3f);
}
voidwrite_com1(unsignedchard)
{
check_busy1();
if(button_flag!
=0)
{
P1=d;
d_i=0;
r_w=0;
cs1=1;
cs2=0;
enb=1;
enb=0;
cs1=0;
}
}
voidwrite_data1(unsignedchard)
{
check_busy1();
if(button_flag!
=0)
{
P1=d;
d_i=1;
r_w=0;
cs1=1;
cs2=0;
enb=1;
enb=0;
cs1=0;
}
}
voidwrite_com2(unsignedchard)
{
check_busy2();
if(button_flag!
=0)
{
P1=d;
d_i=0;
r_w=0;
cs1=0;
cs2=1;
enb=1;
enb=0;
cs2=0;
}
}
voidwrite_data2(unsignedchard)
{
check_busy2();
if(button_flag!
=0)
{
P1=d;
d_i=1;
r_w=0;
cs2=1;
cs1=0;
enb=1;
enb=0;
cs2=0;
}
}
voidclc(void)
{
uinti,j;
rst=0;
rst=1;
write_com1(0xc0);
write_com1(0x3f);
for(i=0;i<9;i++)
{
write_com1(table1[i]);
for(j=64;j>0;j--)
{
write_data1(0x00);
}
}
write_com2(0xc0);
write_com2(0x3f);
for(i=0;i<9;i++)
{
write_com2(table1[i]);
for(j=64;j>0;j--)
{
write_data2(0x00);
}
}
}
voidwrite_ch_add1(uintz)
{
uinta;
a=z%8;
if(a==1||a==2||a==3||a==4)
{
write_com1(table1[(z/8)*2]);//写一个汉字上半部分的页地址
write_com1(table2[a-1]);//写一个汉字上半部分的Y起始地址
}
else
{
if(z%8==0)
{
a=a+8;
write_com2(table1[((z/8)-1)*2]);
write_com2(table2[a-5]);
}
else
{
write_com2(table1[(z/8)*2]);
write_com2(table2[a-5]);
}
}
}
voidwrite_ch_add2(uintz)
{
uinta;
a=z%8;
if(a==1||a==2||a==3||a==4)
{
write_com1(table1[(z/8)*2+1]);
write_com1(table2[a-1]);
}
else
{
if(z%8==0)
{
a=a+8;
write_com2(table1[((z/8)-1)*2+1]);
write_com2(table2[a-5]);
}
else
{
write_com2(table1[(z/8)*2+1]);
write_com2(table2[a-5]);
}
}
}
voidwrite_ch(uintadd,ucharz[])
{
uinti;
if((add%8)==1||(add%8)==2||(add%8)==3||(add%8)==4)
{
write_ch_add1(add);
for(i=0;i<=15;i++)
{
write_data1(z[i]);
}
write_ch_add2(add);
for(i=16;i<=31;i++)
{
write_data1(z[i]);
}
}
else
{
write_ch_add1(add);
for(i=0;i<=15;i++)
{
write_data2(z[i]);
}
write_ch_add2(add);
for(i=16;i<=31;i++)
{
write_data2(z[i]);
}
}
}
voidwrite_en_add1(uintz)
{
uinta;
a=z%16;
if(a==1||a==2||a==3||a==4||a==5||a==6||a==7||a==8)
{
write_com1(table1[(z/16)*2]);//写一个字母上半部分的页地址
write_com1(table3[a-1]);//写一个字母上半部分的Y起始地址
}
else
{
if(z%16==0)
{
a=a+16;
write_com2(table1[((z/16)-1)*2]);
write_com2(table3[a-9]);
}
else
{
write_com2(table1[(z/16)*2]);
write_com2(table3[a-9]);
}
}
}
voidwrite_en_add2(uintz)
{
uinta;
a=z%16;
if(a==1||a==2||a==3||a==4||a==5||a==6||a==7||a==8)
{
write_com1(table1[(z/16)*2+1]);
write_com1(table3[a-1]);
}
else
{
if(z%16==0)
{
a=a+16;
write_com2(table1[((z/16)-1)*2+1]);
write_com2(table3[a-9]);
}
else
{
write_com2(table1[(z/16)*2+1]);
write_com2(table3[a-9]);
}
}
}
voidwrite_en(uintadd,ucharz[])
{
uinti;
if((add%16)==1||(add%16)==2||(add%16)==3||(add%16)==4||(add%16)==5||(add%16)==6||(add%16)==7||(add%16)==8)
{
write_en_add1(add);
for(i=0;i<=7;i++)
{
write_data1(z[i]);
}
write_en_add2(add);
for(i=8;i<=15;i++)
{
write_data1(z[i]);
}
}
else
{
write_en_add1(add);
for(i=0;i<=7;i++)
{
write_data2(z[i]);
}
write_en_add2(add);
for(i=8;i<=15;i++)
{
write_data2(z[i]);
}
}
}
uchar*read_en(uintadd)
{
uinti;
uchar*x;
//uchara[16];//注意:
程序执行中,不能改变rom中的值,所以a不能定义为code
if((add%8)==1||(add%8)==2||(add%8)==3||(add%8)==4)
{
write_ch_add1(add);
for(i=1;i<=8;i++)
{
read_zimo[i-1]=read1();
}
write_ch_add2(add);
for(i=9;i<=16;i++)
{
read_zimo[i-1]=read1();
}
}
else
{
write_ch_add1(add);
for(i=1;i<=8;i++)
{
read_zimo[i-1]=read2();
}
write_ch_add2(add);
for(i=9;i<=16;i++)
{
read_zimo[i-1]=read2();
}
}
x=read_zimo;
returnx;
}
uchar*read_ch(uintadd)
{
uinti;
uchar*x;
//uchara[32];//注意:
程序执行中,不能改变rom中的值,所以a不能定义为code
if((add%8)==1||(add%8)==2||(add%8)==3||(add%8)==4)
{
write_ch_add1(add);
for(i=1;i<=16;i++)
{
read_zimo[i-1]=read1();
}
write_ch_add2(add);
for(i=17;i<=32;i++)
{
read_zimo[i-1]=read1();
}
}
else
{
write_ch_add1(add);
for(i=1;i<=16;i++)
{
read_zimo[i-1]=read2();
}
write_ch_add2(add);
for(i=17;i<=32;i++)
{
read_zimo[i-1]=read2();
}
}
x=&read_zimo[0];
returnx;
}
ucharread1(void)//读左半屏图形数据子程序
{
uchard;
check_busy1();//检测液晶显示控制器是否空闲
P1=0xff;
d_i=1;
r_w=1;
cs1=1;
cs2=0;
enb=1;
delay
(2);
d=P1;
enb=0;
cs1=0;
returnd;
}
ucharread2(void)//读右半屏图形数据子程序
{
uchard;
check_busy2();//检测液晶显示控制器是否空闲
P1=0xff;
d_i=1;
r_w=1;
cs1=0;
cs2=1;
enb=1;
delay
(2);
d=P1;
enb=0;
cs2=0;
returnd;
}
voidcheck_busy1(void)//控制器1忙检测子程序
{
uinti;
P1=0xff;
d_i=0;
r_w=1;
cs1=1;
cs2=0;
enb=1;
for(i=0;i<20;i++)//
if((P1&0x80)!
=0x80)break;//D7=0空闲退出
enb=0;
cs1=0;
}
voidcheck_busy2(void)//控制器2忙检测子程序
{
uinti;
P1=0xff;
d_i=0;
r_w=1;
cs1=0;
cs2=1;
enb=1;
for(i=0;i<20;i++)//
if((P1&0x80)!
=0x80)break;//D7=0空闲退出
enb=0;
cs2=0;
}
voiddelay(ucharz)
{
uinti,j;
for(i=z;z>0;z--)
for(j=5;j>0;j--);
}
voidtest()
{
write_en(1,en_1);
write_en(2,en_2);
write_en(3,en_3);
write_en(4,en_4);
write_en(5,en_5);
write_en(6,en_6);
write_en(7,en_7);
write_en(8,en_8);
write_en(9,en_9);
write_en(10,en_0);
write_en(11,en_a);
write_en(12,en_b);
write_en(13,en_c);
write_en(14,en_d);
write_en(15,en_e);
write_en(16,en_f);
write_en(17,en_g);
write_en(18,en_h);
write_en(19,en_i);
write_en(20,en_j);
write_en(21,en_k);
write_en(22,en_l);
write_en(23,en_m);
write_en(24,en_k);
write_en(25,en_o);
write_en(26,en_p);
write_en(27,en_q);
write_en(28,en_r);
write_en(29,en_s);
write_en(30,en_t);
write_en(31,en_u);
write_en(32,en_v);
write_en(33,en_w);
write_en(34,en_x);
write_en(35,en_y);
write_en(36,en_z);
write_en(37,en_aa);
write_en(38,en_bb);
write_en(39,en_cc);
write_en(40,en_dd);
write_en(41,en_ee);
write_en(42,en_ff);
write_en(43,en_gg);
write_en(44,en_hh);
write_en(45,en_ii);
write_en(46,en_jj);
write_en(47,en_kk);
write_en(48,en_ll);
write_en(49,en_mm);
write_en(50,en_nn);
write_en(51,en_oo);
write_en(52,en_pp);
write_en(53,en_qq);
write_en(54,en_rr);
write_en(55,en_ss);
write_en(56,en_tt);
write_en(57,en_uu);
write_en(58,en_vv);
write_en(59,en_ww);
write_en(60,en_xx);
write_en(61,en_yy);
write_en(62,en_zz);
}//需要时调出
//////////////////////////////////////////字库
#defineucharunsignedchar
#defineuintunsignedint
ucharcodezhou[]=
{0x00,0x00,0xFE,0x82,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0x82,0xFE,0x00,0x00,0x00,
0x80,0x60,0x1F,0x00,0x00,0x1E,0x12,0x12,0x12,0x1E,0x40,0x80,0x7F,0x00,0x00,0x00};/*"周",0*/
ucharcodeyu[]=
{
0x10,0x0C,0x24,0x24,0x24,0x24,0x25,0xE6,0x24,0x24,0x24,0x24,0x24,0x14,0x0C,0x00,
0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00};/*"宇",0*/
ucharcodefeng[]=
{0x