51单片机用12864显示唐诗.docx
《51单片机用12864显示唐诗.docx》由会员分享,可在线阅读,更多相关《51单片机用12864显示唐诗.docx(9页珍藏版)》请在冰豆网上搜索。
![51单片机用12864显示唐诗.docx](https://file1.bdocx.com/fileroot1/2023-2/8/da801394-aaf3-4cd5-b21d-4d873f0cd92a/da801394-aaf3-4cd5-b21d-4d873f0cd92a1.gif)
51单片机用12864显示唐诗
51单片机用12864显示唐诗
51单片机用12864显示唐诗
#include
#include
#include
#include"nn.h"
#defineucharunsignedchar
#defineuintint
#defineLCD_databusP0//LCD12864的8位数据口
sbitRS=P2^2;//RS为0---命令;1----数据
sbitRW=P2^1;//RW为1---写;0---读
sbitEN=P2^0;//使能端
sbitCS1=P2^4;//片选1低电平有效,控制左半屏
sbitCS2=P2^3;//片选1低电平有效,控制右半屏
sbitkey1=P3^0;
sbitkey2=P3^1;
voiddelay_ms(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voiddelay(uinti)
{
while(--i);
}
voidRead_busy()//读“忙”函数-----数据线的最高位DB71则busy
{
P0=0x00;
RS=0;
RW=1;
EN=1;
while(P0&0x80);
EN=0;
}
voidwrite_LCD_command(ucharvalue)//写命令函数
{
Read_busy();//对LCD的每次读写都要读忙
RS=0;//选择命令
RW=0;//读操作
LCD_databus=value;
EN=1;//EN由1----0锁存有效数据
_nop_();
_nop_();
EN=0;
}
voidwrite_LCD_data(ucharvalue)//写数据函数
{
Read_busy();
RS=1;//选择数据
RW=0;
LCD_databus=value;
EN=1;//EN由1----0锁存有效数据
_nop_();
_nop_();
EN=0;
}
voidSet_page(ucharpage)//设置“页”LCD12864共8页,一页是8行点阵点
{
page=0xb8|page;//页的首地址为0xB8
write_LCD_command(page);
}
voidSet_line(ucharstartline)//设置显示的起始行
{
startline=0xC0|startline;//起始行地址为0xC0
write_LCD_command(startline);//设置从哪行开始:
共0--63;一般从0行开始显示
}
voidSet_column(ucharcolumn)//设置显示的列
{
column=column&0x3f;//列的最大值为64
column=0x40|column;//列的首地址为0x40
write_LCD_command(column);//规定显示的列的位置
}
voidSetOnOff(ucharonoff)//显示开关函数:
0x3E是关显示,0x3F是开显示
{
onoff=0x3e|onoff;//onoff:
1---开显示;0---关显示
write_LCD_command(onoff);
}
voidSelectScreen(ucharscreen)//选择屏幕
{
switch(screen)
{
case0:
CS1=0;CS2=0;break;//全屏
case1:
CS1=0;CS2=1;break;//左半屏
case2:
CS1=1;CS2=0;break;//右半屏
default:
break;
}
}
voidClearScreen(ucharscreen)//清屏函数
{
uchari,j;
SelectScreen(screen);//0--全屏;1---左半屏;2---右半屏
for(i=0;i<8;i++)//控制页数0-7,共8页
{
Set_page(i);
Set_column(0);
for(j=0;j<64;j++)//控制列数0-63,共64列
{
write_LCD_data(0x00);//写入0,地址指针自加1
}
}
}
voidinit_LCD()//LCD的初始化
{
SetOnOff
(1);//开显示
SelectScreen(0);
ClearScreen(0);//清屏
Set_line(0);//开始行:
0
}
voidDisplay_HZ(ucharscreen,ucharpage,ucharcolumn,uchar*p)
{
uchari;
SelectScreen(screen);
Set_page(page);//写上半页:
16*8
Set_column(column*16);//控制列
for(i=0;i<16;i++)//控制16列的数据输出
{
write_LCD_data(p[i]);//汉字的上半部分
}
Set_page(page+1);//写下半页:
16*8
Set_column(column*16);//控制列
for(i=0;i<16;i++)//控制16列的数据输出
{
write_LCD_data(p[i+16]);//汉字的下半部分
}
}
voiddisp1()
{ClearScreen(0);
Display_HZ(1,0,0,zi);//guanjianzhizheng
Display_HZ(1,0,1,yun);//guanjianzhizheng
Display_HZ(1,0,2,san);//guanjianzhizheng
Display_HZ(1,0,3,hao);//guanjianzhizheng
Display_HZ(1,2,0,sheng);//guanjianzhizheng
Display_HZ(1,2,1,dang);//guanjianzhizheng
Display_HZ(1,2,2,zuo);//guanjianzhizheng
Display_HZ(1,2,3,ren);//guanjianzhizheng
Display_HZ(2,2,0,jie);//guanjianzhizheng
Display_HZ(1,4,0,si);//guanjianzhizheng
Display_HZ(1,4,1,yi);//guanjianzhizheng
Display_HZ(1,4,2,wei);//guanjianzhizheng
Display_HZ(1,4,3,gui);//guanjianzhizheng
Display_HZ(2,4,0,xiong);//guanjianzhizheng
Display_HZ(1,6,0,zhi);//guanjianzhizheng
Display_HZ(1,6,1,jin);//guanjianzhizheng
Display_HZ(1,6,2,sii);//guanjianzhizheng
Display_HZ(1,6,3,xiang);//guanjianzhizheng
Display_HZ(2,6,0,yu);
//while
(1);
}
voiddisp2()
{ClearScreen(0);
Display_HZ(1,0,0,bu);//guanjianzhizheng
Display_HZ(1,0,1,ken);//guanjianzhizheng
Display_HZ(1,0,2,guo);//guanjianzhizheng
Display_HZ(1,0,3,jiang);//guanjianzhizheng
Display_HZ(2,0,0,dong);//guanjianzhizheng
}
voidKey1()
{
if(key1==0)
{
delay_ms(10);
if(key1==0)
{
while(!
key1)
{
while(!
key1)
{
disp2();
}
}
}
}
}
voidKey2()
{
if(key2==0)
{
delay_ms(10);
if(key2==0)
{
while(!
key2)
{
while(!
key2)
{
disp1();
}
}
}
}
}
voidmain()
{
init_LCD();//初始12864
ClearScreen(0);//清屏
Set_line(0);//显示开始行
disp1();
while
(1)
{
Key1();
Key2();
}
}