您好,欢迎访问三七文档
includereg52.h#includemath.hvoidlcd_init(void);voidclr_lcd(void);voidsend_com(unsignedcharcmd);voidsend_data(unsignedchardat);voidchek_busy(void);voidset_xy(unsignedcharxpos,unsignedcharypos);voidprint(unsignedcharx,unsignedchary,char*str);voidprintstr(unsignedcharxpos,unsignedcharypos,unsignedcharstr[],unsignedchark);unsignedcharcodebuf[4]={0xbb,0xb6,0xd3,0xad};//欢迎#defineDATAP2#defineCONTROLP0#defineE7//并行的使能信号sbitE=P0^7#defineRW6//并行的读写信号sbitRW=P0^6#defineRS5//并行的指令数据选择信号sbitRS=P0^5#definePSB4//并/串行接口选择sbitPSB=P0^4#defineRST3//复位低电平有效sbitRST=P0^3#defineSETB(x,y)(x|=(1y))#defineCLRB(x,y)(x&=(~(1y)))#defineCHKB(x,y)(x&(1y))/********************测忙碌**********************///测忙碌子程序//RS=0,RW=1,E=H,D0-D7=状态字/************************************************/voidchek_busy(void){unsignedchartemp1;//状态信息(判断是否忙)CLRB(CONTROL,RS);//RS=0;SETB(CONTROL,RW);//RW=1;SETB(CONTROL,E);//E=1;do{temp1=DATA;DATA=0xFF;}while(temp1&0x80);SETB(CONTROL,E);//E=1;DATA=0xFF;}/********************写命令**********************///写命令子程序///************************************************/voidsend_com(unsignedcharcmd)/*写命令*/{chek_busy();CLRB(CONTROL,RS);//RS=0;CLRB(CONTROL,RW);//RW=0;DATA=cmd;SETB(CONTROL,E);//E=1;CLRB(CONTROL,E);//E=0;}/********************写数据**********************///写数据子程序///************************************************/voidsend_data(unsignedchardat){chek_busy();SETB(CONTROL,RS);//RS=1;CLRB(CONTROL,RW);//RW=0;DATA=dat;SETB(CONTROL,E);//E=1;CLRB(CONTROL,E);//E=0;}/********************初始化**********************///复位、通讯方式选择/************************************************/voidlcd_init(void){SETB(CONTROL,RST);//复位RST=1SETB(CONTROL,PSB);//通讯方式为并口PSB=1//send_com(0x34);//34H--扩充指令操作send_com(0x30);//功能设置,一次送8位数据,基本指令集send_com(0x0C);//0000,1100整体显示,游标off,游标位置offsend_com(0x01);//0000,0001清DDRAMsend_com(0x02);//0000,0010DDRAM地址归位send_com(0x80);//1000,0000设定DDRAM7位地址000,0000到地址计数器AC}/*******************************************************************///设置显示位置xpos(1~16),tpos(1~4)/*******************************************************************/voidset_xy(unsignedcharxpos,unsignedcharypos){switch(ypos){case1:send_com(0X80|xpos);break;case2:send_com(0X90|xpos);break;case3:send_com(0X88|xpos);break;case4:send_com(0X98|xpos);break;default:break;}}/*******************************************************************///在指定位置显示字符串/*******************************************************************/voidprint(unsignedcharx,unsignedchary,char*str){unsignedcharlcd_temp;set_xy(x,y);lcd_temp=*str;while(lcd_temp!=0x00){send_data(lcd_temp);lcd_temp=*(++str);}}/********************写字符串******************///写字符串子程序//xpos1取0~7共八列,ypos1取0~3共四行。/**********************************************/voidprintstr(unsignedcharxpos,unsignedcharypos,unsignedcharstr[],unsignedchark){unsignedcharn;switch(ypos){case1:xpos|=0x80;break;//第一行case2:xpos|=0x90;break;//第二行case3:xpos|=0x88;break;//第三行case4:xpos|=0x98;break;//第四行default:break;}send_com(xpos);//此处的Xpos已转换为LCM的显示寄存器实际地址for(n=0;nk;n++){send_data(str[n]);//显示汉字时注意码值,连续两个码表示一个汉字}}/********************清屏************************///清屏/************************************************/voidclr_lcd(void){send_com(0x01);//send_com(0x34);//send_com(0x30);}/**************主函数***********************/voidmain(){lcd_init();//设定液晶工作方式printstr(1,1,buf,4);print(5,1,光临);print(0,3,SEEGU百度空间);while(1){}}#defineuintunsignedint#defineucharunsignedchar#definex10x80#definex20x88#definey0x80#definecomm0#definedat1//#definebusyP5&0x80;//lcdbusybitsbitRS=P3^0;//H=data;L=command;sbitRW=P3^1;//H=read;L=write;sbitE=P3^2;//inputenable;//sbitPSB=P2^4;//H=并口;L=串口;sbitRST0=P3^5;//ResetSignal低电平有效ucharshuma[11]=0123456789.;voiddelay(uintus)//delaytime{while(us--);}voiddelay1(uintms){uinti,j;for(i=0;ims;i++)for(j=0;j15;j++)delay(10);}voidchk_busy(void){P5=0xff;RS=0;delay(10);RW=1;delay(10);E=1;while(P5&0x80);delay(10);E=0;delay(10);}voidwr_lcd(uchardat_comm,ucharcontent){chk_busy();if(dat_comm){RS=1;//datadelay(10);RW=0;//write}else{RS=0;//commanddelay(10);RW=0;//write}P5=content;//outputdataorcommdelay(10);E=1;delay(10);E=0;}voidcler(){uchari,j;for(j=0;j32;j++){for(i=0;i8;i++){wr_lcd(comm,0x34);wr_lcd(comm,y+j);wr_lcd(comm,x1+i);wr_lcd(comm,0x30);wr_lcd(dat,0);wr_lcd(dat,0);}}for(j=32;j64;j++){for(i=0;i8;i++){wr_lcd(comm,0x34);wr_lcd(comm,y+j-32);wr_lcd(comm,x2+i);wr_lcd(comm,0x30);wr_lcd(dat,0);wr_lcd(dat,0);}}wr_lcd(comm,0x36);}voidhzkdis(unsignedcharcode*s){while(*s0){wr_lcd(dat,*s);s++;}}voidzifu(unsignedcharshuma){wr_lcd(dat,shuma);}voidclrram(void){wr_lcd(comm,0x30);wr_lcd(comm,0x01);}voidceshi(){wr_lcd(comm,0x30);//清显示并设地址指针为00Hdelay(5);wr_lcd(comm,0x80);//设置显示的位置hzkdis(西南交大---SWJTU);/*wr_lcd(comm,0x90);//设置显示的位置hzkdis(6403实验室C8051F);//设置显示内容*/wr_lcd(comm,0x88);//设置显示的位置hzkdis(温度:);//设置显示内容wr_lcd(comm,0x98);//设置显示的位置hzkdis(测量值:);//设置显示内容}voidinit_lcd(void){RST0=1;wr_lcd(comm,0x30);/*30---基本指令动作*/wr_lcd(comm,0x01);/*清屏,地址指针指向00H*/wr_lcd(comm,0x06);/*光标的移动方向*/wr_lcd(comm,0x0c);/*开显示,关游标*/cler();}/*********************************
本文标题:12864程序
链接地址:https://www.777doc.com/doc-7025792 .html