上一篇讲了画矩形及多边形,以及实块,由于资源问题没能将画圆及圆形实块等加上,这次分开说一下。
效果图:(由于液晶像素点不是正方形,所以看起来圆有点椭,呵呵!)
//lcd3510.c
#include <iom8v.h>
#include "mytype.h"
#include "math.h"
#include "lcd3510.h"
void DelayXms(uint16 x)
{
uint8 i;
while(x--)
{
i="1275";
while(i--);
}
}
/**************************************************************
************
液晶硬件复位
*****************************************************************************
*/
void LcdReset(void)
{
ClrLcdRst();
DelayXms(5);
SetLcdRst();
DelayXms(5);
}
/****************************************************************************
**********************
发送命令
*****************************************************************************
*************************/
void LcdSendCommand(uint8 cmd)
{
uint8 i;
SetSdataOut();
ClrLcdCs();
ClrLcdSclk();
ClrLcdSdata();
SetLcdSclk();
for(i=0;i<8;i++)
{
ClrLcdSclk();
if(cmd & 0x80)
{
SetLcdSdata();
}
else
{
ClrLcdSdata();
}
SetLcdSclk();
cmd<<=1;
}
}
/****************************************************************************
*
发送数据
*****************************************************************************
***********/
void LcdSendData(uint32 Data)
{
uint32 i;
SetSdataOut();
ClrLcdCs();
ClrLcdSclk();
SetLcdSdata();
SetLcdSclk();
for(i=0;i<8;i++)
{
ClrLcdSclk();
if(Data & 0x80)
{
SetLcdSdata();
}
else
{
ClrLcdSdata();
}
SetLcdSclk();
Data<<=1;
}
}
/***************************************************************************
液晶端口初始化
************************************************************************/
void LcdPortInit(void)
{
DDR4|=((1<<LCD_RST)|(1<<LCD_CS));
DDR2|=((1<<LCD_SDATA)|(1<<LCD_SCLK));
SetLcdRst();
SetLcdCs();
SetLcdSclk();
}
/**********************************************************************
液晶初始化
*************************************************************************/
void LcdInit(void)
{
uint8 i;
LcdPortInit();
LcdReset();
LcdSendCommand(0x01); //soft reset
SetLcdCs();
DelayXms(5);
LcdSendCommand(0xc6); //initial escape
SetLcdCs();
LcdSendCommand(0xb9); //refresh set
LcdSendData(0x00);
SetLcdCs();
LcdSendCommand(0xb6); //display control
LcdSendData(0x80);
LcdSendData(0x80);
LcdSendData(0x81);
LcdSendData(84);
LcdSendData(69);
LcdSendData(82);
LcdSendData(67);
SetLcdCs();
LcdSendCommand(0xb3); //gray scale position set
LcdSendData(1);
LcdSendData(2);
LcdSendData(4);
LcdSendData(8);
LcdSendData(16);
LcdSendData(30);
LcdSendData(40);
LcdSendData(50);
LcdSendData(60);
LcdSendData(70);
LcdSendData(80);
LcdSendData(90);
LcdSendData(100);
LcdSendData(110);
LcdSendData(127);
SetLcdCs();
LcdSendCommand(0xb5); //gamma curve set
LcdSendData(0x01);
SetLcdCs();
LcdSendCommand(0xbd); //common driver output select
LcdSendData(0x00);
SetLcdCs();
LcdSendCommand(0xbe); //power control
LcdSendData(0x04);
SetLcdCs();
LcdSendCommand(0x11); //sleep out
SetLcdCs();
LcdSendCommand(0xba); //voltage control
LcdSendData(127);
LcdSendData(3);
SetLcdCs();
LcdSendCommand(0xb7); //temperature gradient set
for(i=0; i<14; i++)
{
LcdSendData(0x00);
}
SetLcdCs();
LcdSendCommand(0x29); //display ON
SetLcdCs();
LcdSendCommand(0x03); //booster voltage ON
SetLcdCs();
DelayXms(5);
LcdSendCommand(0x20); //display inversion OFF
SetLcdCs();
LcdSendCommand(0x25); //write contrast
LcdSendData(63);
SetLcdCs();
}
/* ************************************************************************
液晶清屏
*****************************************************************************
/
void LcdClr(void)
{
uint8 x, y;
LcdSendCommand(0x2a); //column address set
LcdSendData(0);
LcdSendData(97);
SetLcdCs();
LcdSendCommand(0x2b); //page address set
LcdSendData(0);
LcdSendData(66);
SetLcdCs();
LcdSendCommand(0x2c); //memory write
for(y=0;y<67;y++)
{
for(x=0;x<98;x+=2)
{
LcdSendData(0);
LcdSendData(0);
LcdSendData(0);
}
}
SetLcdCs();
}
/****************************************************************************
***************
画点
*****************************************************************************
**************/
void LcdPointWrite(uint8 x, uint8 y,uint8 rr,uint8 gg,uint8 bb)
{
uint8 b1,b2;
b1=~(((rr<<4)&0xf0)|(gg&0x0f));
b2=~(((bb<<4)&0xf0)|(rr&0x0f));
LcdSendCommand(0x2a); //column address set
LcdSendData(x);
LcdSendData(x);
SetLcdCs();
LcdSendCommand(0x2b); //page address set
LcdSendData(y);
LcdSendData(y);
SetLcdCs();
LcdSendCommand(0x2c); //memory write
LcdSendData(b1);
LcdSendData(b2);
SetLcdCs();
}
/***************************************************************************
画线
*****************************************************************************
/
void LCD_DrawLine(uint8 x1, uint8 y1, uint8 x2, uint8 y2,uint8 rr,uint8
gg,uint8 bb)
{
uint8 x,y;
if(x1<x2)
for(x=x1;x<=x2;x++)
{
if(y1<=y2)
{ y="y1"+(uint16)((x-x1)*(y2-y1))/(x2-x1);
LcdPointWrite(x,y,rr,gg,bb);}
else
{y=y1-(uint16)((x-x1)*(y1-y2))/(x2-x1);
LcdPointWrite(x, y,rr,gg,bb);}
}
else if(x1==x2)
{ x="x1";
if(y1<=y2)
{for(y=y1;y<=y2;y++)
LcdPointWrite(x, y,rr,gg,bb);}
else {
for(y=y2;y<=y1;y++)
LcdPointWrite(x, y,rr,gg,bb);}
}
else for(x=x2;x<=x1;x++)
{
if(y1>=y2)
{ y="y1-"(uint16)((x1-x)*(y1-y2))/(x1-x2);
LcdPointWrite(x, y,rr,gg,bb);}
else
{y=y1+(uint16)((x1-x)*(y2-y1))/(x1-x2);
LcdPointWrite(x, y,rr,gg,bb);}
}
if(y1<y2)
for (y=y1;y<=y2;y++)
{
if(x1<=x2)
{x=x1+(uint16)((y-y1)*(x2-x1))/(y2-y1);
LcdPointWrite(x, y,rr,gg,bb);}
else
{x=x1-(uint16)((y-y1)*(x1-x2))/(y2-y1);
LcdPointWrite(x, y,rr,gg,bb);}
}
else if(y1==y2)
{y=y1;
if(x1<=x2)
{for(x=x1;x<=x2;x++)
LcdPointWrite(x, y,rr,gg,bb);}
else
{for(x=x2;x<=x1;x++)
LcdPointWrite(x, y,rr,gg,bb);}
}
else
for(y=y2;y<=y1;y++)
{
if(x1<=x2)
{x=x1+(uint16)((y1-y)*(x2-x1))/(y1-y2);
LcdPointWrite(x, y,rr,gg,bb);}
else
{x=x1-(uint16)((y1-y)*(x1-x2))/(y1-y2);
LcdPointWrite(x, y,rr,gg,bb);}
}
}
/****************************************************************************
******
画圆
*****************************************************************************
******/
void LCD_Drawcir(uint8 x1,uint8 y1,uint8 r,uint8 rr,uint8 gg,uint8 bb)
{
uint8 x,y;
if(x1>=r)
x=(x1-r);
else
x="0";
for(x;x<=(x1+r);x++)
{y=sqrt((uint16)(r*r)-(uint16)((x-x1)*(x-x1)))+y1;
if((y-2*sqrt((uint16)(r*r)-(uint16)((x-x1)*(x-x1))))<0)
LcdPointWrite(x, y,rr,gg,bb);
else
{LcdPointWrite(x, y,rr,gg,bb);
LcdPointWrite(x,(y-2*sqrt((uint16)(r*r)-(uint16)((x-x1)*(x-x1))))
,rr,gg,bb); }
}
if(y1>=r)
y=(y1-r);
else
y="0";
for(y;y<=(y1+r);y++)
{x=sqrt((uint16)(r*r)-(uint16)((y-y1)*(y-y1)))+x1;
if((x-2*sqrt((uint16)(r*r)-(uint16)((y-y1)*(y-y1))))<0)
LcdPointWrite(x, y,rr,gg,bb);
else
{ LcdPointWrite(x, y,rr,gg,bb);
LcdPointWrite((x-2*sqrt((uint16)(r*r)-(uint16)((y-y1)*(y-y1)))),y
,rr,gg,bb);}
}
}
/**************************************************************************
圆形实块
*****************************************************************************
*****/
void LCD_Drawfilledcir(uint8 x1,uint8 y1,uint8 r,uint8 rr,uint8 gg,uint8 bb)
{
uint8 x,y;
if(x1>=r)
x=(x1-r);
else
x=0;
for(x;x<=(x1+r);x++)
{y=sqrt((uint16)(r*r)-(uint16)((x-x1)*(x-x1)))+y1;
if((y-2*sqrt((uint16)(r*r)-(uint16)((x-x1)*(x-x1))))<0)
LCD_DrawLine(x,0,x,y,rr,gg,bb);
else
LCD_DrawLine(x,(y-2*sqrt((uint16)(r*r)-(uint16)((x-x1)*(x-
x1)))),x,y,rr,gg,bb);
}
if(y1>=r)
y=(y1-r);
else
y="0";
for(y;y<=(y1+r);y++)
{x=sqrt((uint16)(r*r)-(uint16)((y-y1)*(y-y1)))+x1;
if((x-2*sqrt((uint16)(r*r)-(uint16)((y-y1)*(y-y1))))<0)
LCD_DrawLine(x,0,x,y,rr,gg,bb);
else
LCD_DrawLine((x-2*sqrt((uint16)(r*r)-(uint16)((y-y1)*(y-
y1)))),y,x,y,rr,gg,bb);
}
}
文章评论(0条评论)
登录后参与讨论