您好,欢迎访问三七文档
#includereg52.h//包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#includeintrins.h#includemath.h#includestdio.h#includeabsacc.h#includectype.h#includestdlib.hsbitRS=P2^4;//定义端口sbitRW=P2^5;sbitEN=P2^6;sbitP0_0=P0^0;sbitP0_1=P0^1;sbitP0_2=P0^2;sbitP0_3=P0^3;sbitP0_4=P0^4;sbitP0_5=P0^5;sbitP0_6=P0^6;sbitP0_7=P0^7;sbitP1_0=P1^0;sbitP1_1=P1^1;sbitP1_2=P1^2;sbitP1_3=P1^3;sbitP1_4=P1^4;sbitP1_5=P1^5;sbitP1_6=P1^6;sbitP1_7=P1^7;sbitP2_0=P2^0;sbitP2_1=P2^1;sbitP2_2=P2^2;sbitP2_3=P2^3;sbitP2_4=P2^4;sbitP2_5=P2^5;sbitP2_6=P2^6;sbitP2_7=P2^7;sbitP3_0=P3^0;sbitP3_1=P3^1;sbitP3_2=P3^2;sbitP3_3=P3^3;sbitP3_4=P3^4;sbitP3_5=P3^5;sbitP3_6=P3^6;sbitP3_7=P3^7;sbitB_0=B^0;sbitB_1=B^1;sbitB_2=B^2;sbitB_3=B^3;sbitB_4=B^4;sbitB_5=B^5;sbitB_6=B^6;sbitB_7=B^7;#defineRS_CLRRS=0#defineRS_SETRS=1#defineRW_CLRRW=0#defineRW_SETRW=1#defineEN_CLREN=0#defineEN_SETEN=1#defineDataPortP0unsignedcharcodeC[]={'0','1','2','3','4','5','6','7','8','9'};/*------------------------------------------------uS延时函数,含有输入参数unsignedchart,无返回值unsignedchar是定义无符号字符变量,其值的范围是0~255这里使用晶振12M,精确延时请使用汇编,大致延时长度如下T=tx2+5uS------------------------------------------------*/voidDelayUs2x(unsignedchart){while(--t);}/*------------------------------------------------mS延时函数,含有输入参数unsignedchart,无返回值unsignedchar是定义无符号字符变量,其值的范围是0~255这里使用晶振12M,精确延时请使用汇编------------------------------------------------*/voidDelayMs(unsignedchart){while(t--){//大致延时1mSDelayUs2x(245);DelayUs2x(245);}}/*------------------------------------------------判忙函数------------------------------------------------*/bitLCD_Check_Busy(void){DataPort=0xFF;RS_CLR;RW_SET;EN_CLR;_nop_();EN_SET;return(bit)(DataPort&0x80);}/*------------------------------------------------写入命令函数------------------------------------------------*/voidLCD_Write_Com(unsignedcharcom){while(LCD_Check_Busy());//忙则等待RS_CLR;RW_CLR;EN_SET;DataPort=com;_nop_();EN_CLR;}/*------------------------------------------------写入数据函数------------------------------------------------*/voidLCD_Write_Data(unsignedcharData){while(LCD_Check_Busy());//忙则等待RS_SET;RW_CLR;EN_SET;DataPort=Data;_nop_();EN_CLR;}/*------------------------------------------------清屏函数------------------------------------------------*/voidLCD_Clear(void){LCD_Write_Com(0x01);DelayMs(5);}/*------------------------------------------------写入字符串函数------------------------------------------------*/voidLCD_Write_String(unsignedcharx,unsignedchary,unsignedchar*s){if(y==0){LCD_Write_Com(0x80+x);//表示第一行}else{LCD_Write_Com(0xC0+x);//表示第二行}while(*s){LCD_Write_Data(*s);s++;}}/*------------------------------------------------写入字符函数------------------------------------------------*/voidLCD_Write_Char(unsignedcharx,unsignedchary,unsignedcharData){if(y==0){LCD_Write_Com(0x80+x);}else{LCD_Write_Com(0xC0+x);}LCD_Write_Data(Data);}/*------------------------------------------------读入角度函数------------------------------------------------*/floatREAD_Angle(void){inti;floatm;floatangle;for(i=7;i=0;i--)B_0=P1_6;m=(B-1024)/819;angle=asin(m);return(angle);}/*------------------------------------------------初始化函数------------------------------------------------*/voidLCD_Init(void){LCD_Write_Com(0x38);/*显示模式设置*/DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);LCD_Write_Com(0x08);/*显示关闭*/LCD_Write_Com(0x01);/*显示清屏*/LCD_Write_Com(0x06);/*显示光标移动设置*/DelayMs(5);LCD_Write_Com(0x0C);/*显示开及光标设置*/}/*------------------------------------------------LCD显示函数------------------------------------------------*/voidLCD_Display(unsignedi){floatangle;intm,n,c,d,e,an;angle=READ_Angle();an=(int)angle*1000;m=an%10;LCD_Write_Char(8,i,C[m]);an=an/10;n=an%10;LCD_Write_Char(7,i,C[n]);an=an/10;c=an%10;LCD_Write_Char(6,i,C[c]);an=an/10;d=an%10;LCD_Write_Char(4,i,C[d]);e=an/10;LCD_Write_Char(3,i,C[e]);LCD_Write_Char(5,i,'.');}/*------------------------------------------------主函数------------------------------------------------*/voidmain(void){LCD_Init();LCD_Clear();//清屏while(1){LCD_Write_String(1,0,X:);LCD_Write_String(1,1,Y:);while(1);}}
本文标题:LCD液晶显示程序
链接地址:https://www.777doc.com/doc-1682624 .html