12864(st7920控制器)画点、线子程序
扫描二维码
随时随地手机看文章
/*------------------ 画点子程序 -----------------------------*/
//X Y为预想的坐标系(X:0-127,Y:0-63 按格算),Color 0灭,1亮,2翻转
void DrawPoint(unsigned char X,unsigned char Y,unsigned char Color)
{
unsigned char Row,Tier,Tier_bit ;
unsigned char ReadOldH,ReadOldL ;
wr_comm(0x34); // 选扩充指令 并关显示
wr_comm(0x36); // 选扩充指令,开显示
Tier=X>>4 ; // 右移4位,即除16 并取整,得对应要写入12864的横坐标DX
Tier_bit=X&0x0f ; // 得该x,位于所在DX中的第几位(0-15),0x0f高4位都为0,
// 按位与,消去了X中的高4位,相当于X%16,得到在DX中的位数。
if(Y<32)
{
Row=Y ;
}
else //y坐标大于31,要上屏显示,DX也相应加8
{
Row=Y-32 ;
Tier+=8 ;
}
wr_comm(Row+0x80); //写入(LCD上的)垂直坐标(页坐标)
wr_comm(Tier+0x80); //写入(LCD上的)水平坐标(列坐标)
ReadByte(); // 预读
ReadOldH=ReadByte(); //读出该处的GDRAM中数据,高8位
ReadOldL=ReadByte(); //低8位
wr_comm(Row+0x80);
wr_comm(Tier+0x80);
if(Tier_bit<8) //x在DX中处于低8位(按预想坐标看),但在LCD内坐标系中属于高8位,对应ReadOldH
{
switch(Color)
{
case 0 : ReadOldH&=(~(0x01<<(7-Tier_bit))); break ; //位转换LCD对应位
case 1 : ReadOldH|=(0x01<<(7-Tier_bit)); break ; //将特定位置1,点亮
case 2 : ReadOldH^=(0x01<<(7-Tier_bit)); break ; //按位异或翻转特定位
default : break ;
}
wr_data(ReadOldH); //先写入高8位
wr_data(ReadOldL);
}
else
{
switch(Color)
{
case 0 : ReadOldL&=(~(0x01<<(15-Tier_bit))); break ;
case 1 : ReadOldL|=(0x01<<(15-Tier_bit)); break ;
case 2 : ReadOldL^=(0x01<<(15-Tier_bit)); break ;
default : break ;
}
wr_data(ReadOldH);
wr_data(ReadOldL);
}
wr_comm(0x30); //设为基本指令集关绘图显示
}
//画水平线:
void DrawLineX( 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++ )
DrawPoint( X0, Y, Color ) ;
}
//画垂直线:
void DrawLineY( 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++)
DrawPoint( X, Y0, Color) ;
}
//采用Bresenham画线算法
void DrawLine_any( 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 )
{
DrawLineY( StartX, StartY, EndY, Color ) ;
return ;
}
else
{
incx = -1 ;
}
if( delta_y > 0 )
{
incy = 1 ;
}
else if(delta_y == 0 )
{
DrawLineX( 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 ;
}
DrawPoint( StartX, StartY, Color ) ;
/* Draw Line*/
for( t = 0 ; t <= distance+1 ; t++ )
{
DrawPoint( StartX, StartY, Color ) ;
x += delta_x ;
y += delta_y ;
if( x > distance )
{
x -= distance ;
StartX += incx ;
}
if( y > distance )
{
y -= distance ;
StartY += incy ;
}
}
}