文档库 最新最全的文档下载
当前位置:文档库 › 12864画圆矩形

12864画圆矩形

#include
#include
#include
#define uchar unsigned char
typedef unsigned int uint;
#define BASIC_SET 0x30 //基本指令集
#define EXTEND_SET 0x34 //扩展指令集
#define DRAW_ON 0x36 //绘图显示开
#define DRAW_OFF 0x34
sbit RS = P3^5;
sbit RW = P3^6;
sbit E = P3^4;
//sbit RES= P2^3;
sbit PSB= P3^7;
#define DataPort P0

/*****************************
大致7us
*****************************/
void DelayUs2x(uchar t)
{
while(--t);
}

/****************************
大致1ms
*****************************/
uchar code table[]="圆正弦矩形";
void DelayMs(unsigned char t)
{
uint i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void Check_Busy()
{
DataPort=0xff;
RS=0;
RW=1;
E=0;
E=1;
while((DataPort&0x80)==0x80); //忙则等待
E=0;
RS = 0;
RW = 1;
}

void Write_Cmd(uchar Cmd)
{
Check_Busy();
RS=0;
RW=0;
//E=0;
DataPort=Cmd;
DelayMs(5);
E=1;
E=1;
E=1;
//DelayMs(5);
E=0;
}
void Write_Data(uchar Data)
{
Check_Busy();
RS=1;
RW=0;
//E=0;
DataPort=Data;
DelayMs(5);
E=1;
E=1;
E=1;
//DelayMs(5);
E=0;
}
uchar Read_Data()
{
uchar LCD_DATA;
Check_Busy();
DataPort=0xff;
RS=1;
RW=1;
E=0;
E=1;
LCD_DATA=DataPort;
//DelayUs2x(5);
E=0;
//DelayUs2x(5);
return LCD_DATA;
}
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if(X==0)
{X=0x80;}
else if(X==1)
{X=0x90;}
else if(X==2)
{X=0x88;}
else if(X==3)
{X=0x98;}
pos=X+Y;
// WriteCommandLCD(pos,1);
Write_Cmd(pos);
}

/*------------------------------------------------
显示字符串
x:横坐标值,范围0~8
y:纵坐标值,范围1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,unsigned char *s)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
while((*s>0)&&(*s!='\0'))
{
Write_Data(*s);
s++;
}
}

void Init_ST7920()
{
//DelayMs(40); //大于40MS的延时程序
PSB=1; //设置为8BIT并口工作模式
// DelayMs(1); //延时
// RES=0; //复位
// DelayMs(1); //延时
// RES=1; //复位置高
DelayMs(2);
//Write_Cmd(0x30); //选择基本指令集
//DelayUs2x(50); //延时大于100us
Write_Cmd(0x30); //选择8bit数据流
DelayMs(02); //延时大于37us
Write_Cmd(0x0c); //开显示(无游标、不反白)
DelayMs(2); //延时大于100us
Write_Cmd(0x01); //清除显示,并且设定地址指针为00H
DelayMs(2); //延时大于10ms
Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及

指定显示的移位,光标从右向左加1位移动
DelayMs(2); //延时大于100uss
}

void LCD_Clear()
{
Write_Cmd(0x01);
DelayMs(15);
}


void LCD_Clear_GDRAM(void) //清绘图
{
uchar i,j;
for(j=0;j<32;j++)
{
Write_Cmd(EXTEND_SET);
Write_Cmd(0x80+j);
_nop_();
_nop_();
Write_Cmd(0x80);
Write_Cmd(0x30); //基本指令集,绘图关
for(i=0;i<32;i++)
Write_Data(0x00); // 写入0x00
}
Write_Cmd(DRAW_ON);
Write_Cmd(BASIC_SET);
}

/* void gui_clear() //清绘图RAM函数
{
uchar i , j , k;
Write_Cmd(0x34);//扩展指令集,8位数据传输 ,1
Write_Cmd(0x34);//绘图显示关闭 ,1
for(i = 0; i < 2; i ++)//分上下两屏写
{
for(j = 0; j < 32; j ++)
{
Write_Cmd(0x80 + j);//写y坐标 ,1
_nop_();
_nop_();
if(i == 0) //写x坐标
{
Write_Cmd(0x80); // ,1
_nop_();
_nop_();
}
else //写下半屏
{
Write_Cmd(0x88); // ,1
_nop_();
_nop_();
}
for(k = 0; k < 16; k ++)//写一整行数据
{
Write_Data(0x00);//写高字节
Write_Data(0x00);//写低字节
_nop_();
_nop_();
}
}
}
Write_Cmd(0x36);//打开绘图显示 ,1
Write_Cmd(0x30);//打开基本指令集 ,1
} */
/*------------------------------------------------
打点
x:横坐标值,范围0~127
y:纵坐标值,范围0~63
------------------------------------------------*/
void LCD_SET_Dot(uchar x,uchar y,bit Mode)
{
uchar x_byte,x_bit;
uchar y_byte,y_bit;
uchar LCD_H,LCD_L;
x&=0x7f;//防止x坐标超过127
y&=0x3f;//防止y坐标超过63
x_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_bit<8)
{
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);
}
//画水平线
void v_Lcd12864DrawLineX_f( unsigned char X0, unsigned char X1, unsigned char Y, unsigned char Color )
{ unsigned char Temp ;
if( X0 > X1 )
{
Temp = X1 ;
X1 = X0 ;
X0 = Temp ;
}
for( ; X0 <= X1 ; X0++ )
LCD_SET_Dot( X0, Y, Color ) ;
}


//画垂直线:
void v_Lcd12864DrawLineY_f( unsigned char X, unsigned char Y0, unsigned char Y1, unsigned char Color )
{
unsigned char Temp ;
if( Y0 > Y1 )
{
Temp = Y1 ;
Y1 = Y0 ;
Y0 = Temp ;
}
for(; Y0 <= Y1 ; Y0++)
LCD_SET_Dot( X, Y0, Color) ;
}

/*------------------------------------------------
画直线
x:横坐标值,范围0~127
y:纵坐标值,范围0~63
------------------------------------------------*/
void LCD_SET_Line(uchar x1,uchar y1,uchar x2,uchar y2,bit Mode)
{
uchar x_add,y_add,y_temp,Line_K;
x1&=0x7f;
x2&=0x7f;
y1&=0x3f;
y2&=0x3f;
if(x2>x1)
{
if(y2>y1||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);
}
}
}
else if(x2==x1)
{
for(y_add=0;y_add<=(y2-y1);y_add++)
{
LCD_SET_Dot(x1,y1+y_add,Mode);
}
}
else
{


}
}

void v_Lcd12864DrawLine_f( unsigned char StartX, unsigned char StartY, unsigned char EndX, unsigned char EndY, unsigned char Color )
{
int t, distance; /*根据屏幕大小改变变量类型(如改为int型)*/
int x = 0 , y = 0 , delta_x, delta_y ;
char incx, incy ;
delta_x = EndX - StartX ;
delta_y = EndY - StartY ;
if( delta_x > 0 )
{
incx = 1;
}
else if( delta_x == 0 )
{
v_Lcd12864DrawLineY_f( StartX, StartY, EndY, Color ) ;
return ;
}
else
{
incx = -1 ;
}
if( delta_y > 0 )
{
incy = 1 ;
}
else if(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_x > delta_y )
{
distance = delta_x ;
}
else
{
distance = delta_y ;
}
LCD_SET_Dot( StartX, StartY, Color ) ;
/* Draw Line*/
for( t = 0 ; t <= distance+1 ; t++ )
{
LCD_SET_Dot( StartX, StartY, Color ) ;
x += delta_x ;
y += delta_y ;
if( x > distance )
{
x -= distance ;
StartX += incx ;
}
if( y > distance )
{
y -= distance ;
StartY += incy ;
}
}
}

//****************画圆函数*********************************/

/
//x0、y0为圆心坐标,r为圆的半径****************************//
//*********************************************************//
void gui_circle(uchar x0 , uchar y0 , uchar r)
{
char a , b;
char di;
if(r > 31 || r == 0)//圆大于液晶屏或者没半径则返回
return;
a = 0;
b = r;
di = 3 - 2 * r;//判断下个点位置的标志
while(a <= b)
{
LCD_SET_Dot( x0 - b , y0 - a,1);//3
LCD_SET_Dot( x0 + b , y0 - a,1); //0
LCD_SET_Dot( x0 - a , y0 + b,1); //1
LCD_SET_Dot( x0 - b , y0 - a,1); //7
LCD_SET_Dot( x0 - a , y0 - b,1); //2
LCD_SET_Dot( x0 + b , y0 + a,1); //4
LCD_SET_Dot( x0 + a , y0 - b,1); //5
LCD_SET_Dot( x0 + a , y0 + b,1); //6
LCD_SET_Dot( x0 - b , y0 + a,1);
a ++;
//***使用bresenham算法画圆********/
if(di < 0)
di += 4 * a + 6;
else
{
di += 10 + 4 * (a - b);
b --;
}
LCD_SET_Dot( x0 + a , y0 + b,1);
}
}
/*------------------------------------------------
画矩形
x:横坐标值,范围0~127
y:纵坐标值,范围0~63
------------------------------------------------*/
void LCD_SET_Box(uchar x1,uchar y1,uchar x2,uchar y2,bit Mode)
{
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);
}
/*画正弦曲线*/
void fsin(uchar m)
{
float x,y;
uchar x1,y1;
for(x=0;x<(4*3.1415);x+=0.1) //4*3.1415)=125.6<127
{
y=sin(x);
x1=10*x;
y1=31-(10*y+0.5+m);
LCD_SET_Dot(x1,y1,1);
}
}
//y1=31-(10*y+0.5);这条语句是对y值进行四舍五入!

void main()
{ uchar i=0;
Init_ST7920();
//gui_clear();
/*LCD_Clear_GDRAM();
v_Lcd12864DrawLine_f( 0, 0, 127, 63, 1 ) ;
v_Lcd12864DrawLine_f( 0, 63, 127, 0 , 1 ) ;
v_Lcd12864DrawLine_f( 12, 0, 127, 63, 1 ) ;
v_Lcd12864DrawLine_f( 52, 63, 127, 0 , 1 ) ;
v_Lcd12864DrawLine_f( 32, 63, 98, 0, 1 ) ;
v_Lcd12864DrawLine_f( 67, 0, 127, 63 , 1 ) ;
DelayMs(10);
CD_Clear_GDRAM();
// gui_clear();
LCD_SET_Line( 0,0,127,0,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,7,127,7,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,15,127,15,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,23,127,23,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,31,127,31,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,39,127,39,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,47,127,47,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,55,127,55,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,63,127,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 0,0,0,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 15,0,15,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 31,0,31,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 47,0,47,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 63,0,63,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 79,0,79,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 95,0,95,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 111,0,111,63,1 ) ;
DelayUs2x(5);
LCD_SET_Line( 127,0,127,63,1 ) ;
DelayMs(10000);
LCD_Clear_GDRAM(); */
fsin(0);
f

sin(1);
gui_circle(100, 15 , 11);
gui_circle(100, 15 , 10);
gui_circle(100, 15 , 9);
LCD_SET_Box(1,1,122,62,1);
LCD_SET_Box(0,0,123,63,1);
LCD_SET_Box(2,2,121,61,1);
LCD_SET_Box(3,3,120,60,0);

while(1)
{ /* Write_Cmd(0x30);
lcd_pos(3,0);
while(table[i]!='\0')
Write_Data(table[i++]);
//gui_clear(); //清绘图RAM函数

//LCD_SET_Box(6,6,123,62,1);

fsin(0);
fsin(1);
fsin(2);
gui_circle(100, 15 , 11);
gui_circle(100, 15 , 10);
gui_circle(100, 15 , 9);
LCD_SET_Box(1,1,122,62,0);
LCD_SET_Box(0,0,123,63,0);
LCD_SET_Box(2,2,121,61,0);
LCD_SET_Box(3,3,120,60,0);
// DelayMs(100);
// LCD_SET_Box(5,5,124,63,1);
// gui_circle(100, 15 , 10);

*/
}

}

相关文档