#include<AT89X52.H> #include <INTRINS.H> #include<math.h> #define uchar unsigned char #define uint unsigned int // 绘图坐标限制 #define glcd_XMAX 240 #define glcd_XMIN 0 #define glcd_YMAX 128 #define glcd_YMIN 0 #define uchar unsigned char #define uint unsigned int #define L240DAT P1 sbit L240CD=P3^3; sbit L240RD=P3^4; sbit L240WR=P3^5; sbit L240BC=P1^0; sbit L240BD=P1^1; sbit RST=P3^2; sbit CLK=P3^0; sbit DAT= P3^1; sbit CS= P3^7; void L_Delay() { uchar i; for ( i=0; i<48; i++ ) ; } void S_Delay() //在传送指令或字节时,CLK要持续8μs的高电平. { char i; for ( i=0; i<8; i++ ) ; } / void line(int x1, int y1, int x2, int y2, unsigned char show) { int dy ; int dx ; int stepx, stepy, fraction; dy = y2 - y1; //Y长度 dx = x2 - x1; //X长度 if (dy < 0) //Y2<Y1: { dy = -dy; stepy = -1; //Y负向 } else { stepy = 1; //Y正向 } if (dx < 0) { dx = -dx; //X负向 stepx = -1; } else { stepx = 1; //X正向 } dy <<= 1; //DY左移一位*2 dx <<= 1; if((x2<240&x1>0)|(x1<240&x2>0)) {writepoint(x1,y1,show);} if (dx > dy) { fraction = dy - (dx >> 1); while (x1 != x2) { if (fraction >= 0) { y1 += stepy; fraction -= dx; } x1 += stepx; fraction += dy; writepoint(x1,y1,show); } } else { fraction = dx - (dy >> 1); while (y1 != y2) { if (fraction >= 0) { x1 += stepx; fraction -= dy; } y1 += stepy; fraction += dx; writepoint(x1,y1,show); } } writepoint(x1,y1,show); } void circle(int x, int y, int radius, unsigned char show) { int xc = 0; int yc ; int p ; clear_graph(); yc=radius; p = 3 - (radius<<1); while (xc <= yc) { writepoint(x + xc, y + yc, show);//在X+XC(圆点附近值最大)上下画两个点 writepoint(x + xc, y - yc, show); writepoint(x - xc, y + yc, show);//在X-XC上下画两个点 writepoint(x - xc, y - yc, show); writepoint(x + yc, y + xc, show);//在X+YC(最右侧数值最小)上下画两个点 writepoint(x + yc, y - xc, show); writepoint(x - yc, y + xc, show);//在X-YC(最右侧数值最小)上下画两个点 writepoint(x - yc, y - xc, show); if (p < 0) p += (xc++ << 2) + 6; else p += ((xc++ - yc--)<<2) + 10; } Delay(500) ; } void fangbo (uchar F,int A) //周期为T,振幅为A,能显示240/T个周期 { uchar T0; int j=0; clear_graph(); l240wc(0x9C);//98开图,9c开文本和图,94开文本 af(A,F); T0=1000/F; line(00,64,240,64,1); for(j=0;j<240/T0;j++) { line(j*T0,64-A,(2*j+1)*T0/2,64-A,1); line((2*j+1)*T0/2,64+A,(j+1)*T0,64+A,1); } } void juchibo(uchar F,int A) { int i=0; float T0; clear_graph(); l240wc(0x9c); T0=1000/F; af(A,F); line(00,64,240,64,1); for(i=0;i<240/T0;i++) { line(i*T0,64,(i+1)*T0,64-A,1); line((i+1)*T0,64-A,(i+1)*T0,64,1); } } //********正弦函数显示************************** //四个参数分别为:x,y为原点坐标(精确位置);t为周期;f为振幅。 void zhengxian (uchar x,uchar y,uchar t,uchar A) { uchar i=0,j=0,m=0,p=0,q=0; m=x; clear_graph(); l240wc(0x9c); af(A,t); A=45; t=30; line(00,64,240,64,1);//x轴,从x到y+f+10 writepoint(x,y-(uchar)A*sin(0),1); //坐标原点(x,y) { for(i=0;i<=230-m;i++) // m=x,230-x个点 { if(i!=0) //i!=0时画点(),x为原点横坐标,p=0???? { p=q ; writepoint(x,p,1); } else p=y; //开始i=0时, j++; x++; //横坐标加一,每个周期内第j个采样值,指针递增 q=y-(unsigned char)A*sin(6.28*j/t);//q=y-f*sin(jw);振幅;2πj/t=jw line(x,p,x,q,1);//横坐标为0,纵坐标从0到振幅的直线 if(j==t) //如果采样个数达到周期,重新开始下一周期 j=0; } } } uchar kbscan(void) {uchar sccode,j,k=0; P1=0xf0; if((P1&0xf0)!=0xf0) {Delay(30); if((P1&0xf0)!=0xf0) {sccode=0xfe; for(j=0;j<4;j++) {P1=sccode; if((P1&0xf0)!=0xf0) {k=~P1; while((P1&0XF0)!=0XF0); return (k); } else sccode=(sccode<<1)|0x01; } } } return(0); } uchar bian(uchar key) {switch(key) {case 0x81: return 0;break; case 0x82: return 1;break; case 0x84: return 2;break; case 0x88: return 3;break; case 0x41: return 4;break; case 0x42: return 5;break; case 0x44: return 6;break; case 0x48: return 7;break; case 0x21: return 8;break; case 0x22: return 9;break; case 0x24: return 10;break; case 0x28: return 11;break; case 0x11: return 12;break; case 0x12: return 13;break; case 0x14: return 14;break; case 0x18: return 15; default: break; } } main() { uchar key; int fb=1,sjb=0,zxb=0; uchar f1=40,f2=200,f3=100; RST=0; Delay(10); RST=1; Delay(100); intl240(); ///初始化必有 clear_c(); while(1) { key=bian(kbscan()); Write7279(0xC8,key); switch (key) { case 1:if(fb==1) //1键频率加100 {f1=jiaf(f1); P2=f1; fangbo(f1,35); } else if(sjb==1) {f2=jiaf(f2); P2=f2; juchibo(f2,40); } else {f3=jiaf(f3); P2=f3; zhengxian(00,64,f3,45); } break; case 2:if(fb==1) //2键频率减100 {f1=jianf(f1); P2=f1; fangbo(f1,35); } else if(sjb==1) {f2=jianf(f2); P2=f2; juchibo(f2,40); } else if(zxb==1) {f3=jianf(f3); P2=f3; zhengxian(00,64,f3,45); } break; case 4:fb=1; sjb=0; zxb=0; P0=0x01; //201方波 fangbo(f1,35); break; case 5:fb=0; sjb=1; zxb=0; P0=0x02; //202锯齿波 juchibo(f2,40); break; case 6:fb=0; sjb=0; zxb=1; P0=0x03; //203正弦波 zhengxian(00,64,f3,45); break; case 15:clear_c(); clear_graph(); l240wc(0x9c); default: break; } } }