- void Draw_Circle_k(unsigned int x0,unsigned int y0,char r,char m)
- {
- int a,b;
- int di;
- a=0;b=r;
- di=3-(r<<1);
- while(a<=b)
- {
- if(m==3) LCD_DrawPoint(x0-b,y0-a);
- if(m==0)LCD_DrawPoint(x0+b,y0-a);
- if(m==1)LCD_DrawPoint(x0-a,y0+b);
- if(m==2)LCD_DrawPoint(x0-a,y0-b);
- if(m==4)LCD_DrawPoint(x0+b,y0+a);
- if(m==5)LCD_DrawPoint(x0+a,y0-b);
- if(m==6)LCD_DrawPoint(x0+a,y0+b);
- if(m==7)LCD_DrawPoint(x0-b,y0+a);
- a++;
- if(di<0)di +=4*a+6;
- else
- {
- di+=10+4*(a-b);
- b--;
- }
- }
- }
至于要绘制那段弧,则是通过参数m来确定。
绘制基线和弧线的程序为:
- LCD_DrawLine(0, base+1, 319, base+1);
- Draw_Circle_k(Xcent,base+1,70,2);
- Draw_Circle_k(Xcent,base+1,70,3);
- Draw_Circle_k(Xcent,base+1,70,0);
- Draw_Circle_k(Xcent,base+1,70,5);
- Draw_Circle_k(Xcent,base+1,140,2);
- Draw_Circle_k(Xcent,base+1,140,3);
- Draw_Circle_k(Xcent,base+1,140,0);
- Draw_Circle_k(Xcent,base+1,140,5);
- Draw_Circle_k(Xcent,base+1,216,2);
- Draw_Circle_k(Xcent,base+1,216,5);
其中,所涉及的参数如下:
Ymax = 239;
Xmax = 319;
Xcent = Xmax / 2;
base = 218;
3. 刻度与指针的绘制
在刻度与指针的制作中,涉及绘制直线绘制函数,其内容为:
- void LCD_DrawLine(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
- {
- unsigned int t;
- int xerr=0,yerr=0,delta_x,delta_y,distance;
- int incx,incy,uRow,uCol;
- delta_x=x2-x1;
- delta_y=y2-y1;
- uRow=x1;
- uCol=y1;
- if(delta_x>0)incx=1;
- else if(delta_x==0) incx=0;
- else {incx=-1;delta_x=-delta_x;}
- if(delta_y>0)incy=1;
- else if(delta_y==0)incy=0;
- else{incy=-1;delta_y=-delta_y;}
- if( delta_x>delta_y)distance=delta_x;
- else distance=delta_y;
- for(t=0;t<=distance+1;t++ )
- {
- LCD_DrawPoint(uRow,uCol);
- xerr+=delta_x ;
- yerr+=delta_y ;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- }
- }
若将直线绘制函数与三角函数相配合就可以快速地实现刻度和指针的绘制,其程序内容为:
- for(i= 46;i < 136; i+=2)
- {
- if (i % 10 == 0)
- {
- LCD_DrawLine(200*cos(radians(i))+Xcent,base - 200*sin(radians(i)) , 215*cos(radians(i))+Xcent,base - 215*sin(radians(i)));
- }
- else
- {
- LCD_DrawLine(210*cos(radians(i))+Xcent,base - 210*sin(radians(i)) , 215*cos(radians(i))+Xcent,base - 215*sin(radians(i)));
- }
- }
- POINT_COLOR=RED;
- LCD_DrawLine(Xcent, 40, Xcent, base+1);
4. 模拟效果
在未使用步进电机的情况下,可以模拟的方式来制作探测指针的扫描摆动效果。
此外,通过随机函数的使用还可以模拟超声波的检测效果。
实现模拟效果的程序如下:
- POINT_COLOR=RED;
- for(i= 2;i < 180; i+=2)
- {
- shuanxin();
- LCD_ShowNum(40,base+8,i,3);
- j=1+(int)(450.0*rand()/(RAND_MAX+1.0));
- LCD_ShowNum(240,base+8,j,3);
- POINT_COLOR=RED;
- LCD_DrawLine(Xcent,base +1 , 158*cos(radians(i))+Xcent,base - 158*sin(radians(i)));
- delay_ms(400);
- POINT_COLOR=BLACK;
- LCD_DrawLine(Xcent,base +1 , 158*cos(radians(i))+Xcent,base - 158*sin(radians(i)));
- }
经程序的测试,其实现的模拟效果如图3和图4所示。
图3 模拟效果1
图4 模拟效果2
模拟效果演示: