#includereg52.h#includeMATH.H#defineucharunsignedchartypedefunsignedintuint;#defineBASIC_SET0x30//基本指令集#defineEXTEND_SET0x34//扩展指令集#defineDRAW_ON0x36//绘图显示开#defineDRAW_OFF0x34sbitRS=P2^4;sbitRW=P2^5;sbitE=P2^6;sbitRES=P2^3;sbitPSB=P2^1;#defineDataPortP0/*****************************大致7us*****************************/voidDelayUs2x(uchart){while(--t);}/****************************大致1ms*****************************/voidDelayMs(unsignedchart){while(t--){DelayUs2x(245);DelayUs2x(245);}}voidCheck_Busy(){DataPort=0xff;RS=0;RW=1;E=1;while((DataPort&0x80)==0x80);//忙则等待E=0;}voidWrite_Cmd(ucharCmd){Check_Busy();RS=0;RW=0;DataPort=Cmd;E=1;E=0;}voidWrite_Data(ucharData){Check_Busy();RS=1;RW=0;DataPort=Data;E=1;E=0;}ucharRead_Data(){ucharLCD_DATA;Check_Busy();DataPort=0xff;RS=1;RW=1;E=1;LCD_DATA=DataPort;DelayUs2x(5);E=0;DelayUs2x(5);returnLCD_DATA;}/*------------------------------------------------显示字符串x:横坐标值,范围0~8y:纵坐标值,范围1~4------------------------------------------------*/voidLCD_PutString(unsignedcharx,unsignedchary,unsignedchar*s){switch(y){case1:Write_Cmd(0x80+x);break;case2:Write_Cmd(0x90+x);break;case3:Write_Cmd(0x88+x);break;case4:Write_Cmd(0x98+x);break;default:break;}while(*s0){Write_Data(*s);s++;}}voidInit_ST7920(){DelayMs(40);//大于40MS的延时程序PSB=1;//设置为8BIT并口工作模式DelayMs(1);//延时RES=0;//复位DelayMs(1);//延时RES=1;//复位置高DelayMs(10);//Write_Cmd(0x30);//选择基本指令集//DelayUs2x(50);//延时大于100usWrite_Cmd(0x30);//选择8bit数据流DelayUs2x(20);//延时大于37usWrite_Cmd(0x0c);//开显示(无游标、不反白)DelayUs2x(50);//延时大于100usWrite_Cmd(0x01);//清除显示,并且设定地址指针为00HDelayMs(15);//延时大于10msWrite_Cmd(0x06);//指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动DelayUs2x(50);//延时大于100us}voidLCD_Clear(){Write_Cmd(0x01);DelayMs(15);}/*清绘图*/voidLCD_Clear_GDRAM(void){uchari,j;for(j=0;j32;j++){Write_Cmd(EXTEND_SET);Write_Cmd(0x80+j);Write_Cmd(0x80);Write_Cmd(0x30);//基本指令集,绘图关for(i=0;i32;i++)Write_Data(0x00);//写入0x00}Write_Cmd(DRAW_ON);Write_Cmd(BASIC_SET);}/*------------------------------------------------打点x:横坐标值,范围0~127y:纵坐标值,范围0~63------------------------------------------------*/voidLCD_SET_Dot(ucharx,uchary,bitMode){ucharx_byte,x_bit;uchary_byte,y_bit;ucharLCD_H,LCD_L;x&=0x7f;//防止x坐标超过127y&=0x3f;//防止y坐标超过63x_byte=x/16;//那一个地址x_bit=x%16;y_byte=y/32;y_bit=y%32;Write_Cmd(EXTEND_SET);Write_Cmd(DRAW_OFF);Write_Cmd(0x80+y_bit);//垂直Write_Cmd(0x80+x_byte+8*y_byte);//水平Read_Data();LCD_H=Read_Data();LCD_L=Read_Data();Write_Cmd(0x80+y_bit);//垂直Write_Cmd(0x80+x_byte+8*y_byte);//水平if(x_bit8){if(Mode){Write_Data(LCD_H|(0x01(7-x_bit)));Write_Data(LCD_L);}else{Write_Data(LCD_H&(~(0x01(7-x_bit))));Write_Data(LCD_L);}}else{if(Mode){Write_Data(LCD_H);Write_Data(LCD_L|(0x01(15-x_bit)));}else{Write_Data(LCD_H);Write_Data(LCD_L&(~(0x01(15-x_bit))));}}Write_Cmd(DRAW_ON);}//画水平线voidv_Lcd12864DrawLineX_f(unsignedcharX0,unsignedcharX1,unsignedcharY,unsignedcharColor){unsignedcharTemp;if(X0X1){Temp=X1;X1=X0;X0=Temp;}for(;X0=X1;X0++)LCD_SET_Dot(X0,Y,Color);}//画垂直线:voidv_Lcd12864DrawLineY_f(unsignedcharX,unsignedcharY0,unsignedcharY1,unsignedcharColor){unsignedcharTemp;if(Y0Y1){Temp=Y1;Y1=Y0;Y0=Temp;}for(;Y0=Y1;Y0++)LCD_SET_Dot(X,Y0,Color);}/*------------------------------------------------画直线x:横坐标值,范围0~127y:纵坐标值,范围0~63------------------------------------------------*/voidLCD_SET_Line(ucharx1,uchary1,ucharx2,uchary2,bitMode){ucharx_add,y_add,y_temp,Line_K;x1&=0x7f;x2&=0x7f;y1&=0x3f;y2&=0x3f;if(x2x1){if(y2y1||y2==y1){Line_K=(y2-y1)/(x2-x1);for(x_add=0;x_add=(x2-x1);x_add++){y_temp=y1+(Line_K*(x_add+x1));LCD_SET_Dot(x_add+x1,y_temp,Mode);}}else{Line_K=(y1-y2)/(x2-x1);for(x_add=0;x_add=(x2-x1);x_add++){y_temp=y1-(Line_K*(x_add+x1));LCD_SET_Dot(x_add+x1,y_temp,Mode);}}}elseif(x2==x1){for(y_add=0;y_add=(y2-y1);y_add++){LCD_SET_Dot(x1,y1+y_add,Mode);}}else{}}voidv_Lcd12864DrawLine_f(unsignedcharStartX,unsignedcharStartY,unsignedcharEndX,unsignedcharEndY,unsignedcharColor){intt,distance;/*根据屏幕大小改变变量类型(如改为int型)*/intx=0,y=0,delta_x,delta_y;charincx,incy;delta_x=EndX-StartX;delta_y=EndY-StartY;if(delta_x0){incx=1;}elseif(delta_x==0){v_Lcd12864DrawLineY_f(StartX,StartY,EndY,Color);return;}else{incx=-1;}if(delta_y0){incy=1;}elseif(delta_y==0){v_Lcd12864DrawLineX_f(StartX,EndX,StartY,Color);return;}else{incy=-1;}delta_x=abs(delta_x);delta_y=abs(delta_y);if(delta_xdelta_y){distance=delta_x;}else{distance=delta_y;}LCD_SET_Dot(StartX,StartY,Color);/*DrawLine*/for(t=0;t=distance+1;t++){LCD_SET_Dot(StartX,StartY,Color);x+=delta_x;y+=delta_y;if(xdistance){x-=distance;StartX+=incx;}if(ydistance){y-=distance;StartY+=incy;}}}/*------------------------------------------------画矩形x:横坐标值,范围0~127y:纵坐标值,范围0~63------------------------------------------------*/voidLCD_SET_Box(ucharx1,uchary1,ucharx2,uchary2,bitMode){LCD_SET_Line(x1,y1,x2,y1,Mode);LCD_SET_Line(x1,y2,x2,y2,Mode);LCD_SET_Line(x1,y1,x1,y2,Mode);LCD_SET_Line(x2,y1,x2,y2,Mode);}/*画正弦曲线*/voidfsin(){floatx,y;ucharx1,y1;for(x=0;x(4*3.1415);x+=0.1)//4*3.1415)=125.6127{y=sin(x);x1=10*x;y1=31-(10*y+0.5);LCD_SET_Dot(x1,y1,1);}}//y1=31-(10*y+0.5);