您的位置 首页 电子

TFT画出恣意斜率的直线

在之前共享的几篇关于TFT的应用程序中,大家可以看出程序的主要框架没有发生大的变化;变化的只是将带你用的函数做了更为灵活的改变!所以

在之前同享的几篇关于TFT的使用程序中,我们可以看出程序的首要结构没有发生大的改动;改动的仅仅将带你用的函数做了更为灵敏的改动!

所以我们学习程序时,一定要能从长篇的程序中找出精华,并及时复制下来做堆集,终究可以灵敏的使用!!

这篇文章或许是关于TFT的最终一遍文章了!再有也仅仅TFT的使用程序!

只需我们能把握之前的几篇文章,就可以自己灵敏的使用了!!!

#include
#defineuint unsigned int
#define uchar unsigned char
#defineTYPE_LCD_DATA1
#defineTYPE_LCD_COMMAND0
#defineLCD_SIZE_X128
#defineLCD_SIZE_Y160
#defineDATAP0
sbit LCD_RST= P2^7;//RST 引脚界说
sbit LCD_RD= P3^2;//RD引脚界说
sbit LCD_WR= P3^3;//WR引脚界说
sbit LCD_RS= P2^5;//RS引脚界说
sbit LCD_CS= P2^6;//CS引脚界说
uint colors[]=
{
0xf800,0x07e0,0x001f,0xffe0,0x0000,0x07ff,0xf81f,0xffff ,0x7bef
};
void delay_ms(uint ms)
{
unsigned char k;
while (ms–)
{
for (k = 0; k < 228; k++)
;
}
}

void LCD_Write(uchar type, uint value)
{
LCD_CS = 0;
LCD_RS = type;// 0: command1: data

LCD_WR= 0;
DATA = (uchar)value;;
LCD_WR = 1;
LCD_CS = 1;
}

void LCD_Wirte_Data16(uint value)// color data
{
LCD_CS = 0;
LCD_RS = 1;
LCD_WR= 0;
DATA = (uchar)value;
LCD_WR= 1;
LCD_WR= 0;
DATA = (uchar)(value>>8);
LCD_WR = 1;
LCD_CS = 1;
}

void Reg_Write(uint reg,uint value)
{
LCD_Write(TYPE_LCD_COMMAND,reg);
LCD_Write(TYPE_LCD_DATA,value);
}

void LCD_SetRamAddr(uint xStart, uint xEnd, uint yStart, uint yEnd)
{
Reg_Write(0x09, xStart);
Reg_Write(0x10, yStart);
Reg_Write(0x11, xEnd);
Reg_Write(0x12, yEnd);
Reg_Write(0x18, xStart);
Reg_Write(0x19, yStart);
LCD_Write(TYPE_LCD_COMMAND,0x22);// 0x22
}

void LCD_init(void)
{
uint num;
Reg_Write(0x0001,0x0002); //MODE_SEL1
Reg_Write(0x0002,0x0012);//MODE_SEL2
Reg_Write(0x0003,0x0000);//MODE_SEL3
Reg_Write(0x0004,0x0010);//MODE_SEL3
LCD_SetRamAddr(0,127, 0,159);
for(num=20480;num>0;num–)
LCD_Wirte_Data16(0xffff);

Reg_Write(0x0005,0x0008);//VCO_MODE
Reg_Write(0x0007,0x007f);//VCOMHT_CTRL
Reg_Write(0x0008,0x0017);//VCOMLT_CTRL
Reg_Write(0x0009,0x0000);//write SRAM window start X point
Reg_Write(0x0010,0x0000);//write SRAM window start y point
Reg_Write(0x0011,0x0083);//write SRAM window end x point
Reg_Write(0x0012,0x009f);//write SRAM window end y point
Reg_Write(0x0017,0x0000);//SRAM contrl
Reg_Write(0x0018,0x0000);//SRAM x position
Reg_Write(0x0019,0x0000);//SRAM y position
Reg_Write(0x0006,0x00c5);//DAC_OP_CTRL2
delay_ms(10); //延时

}

//打点函数
void Dadian(uint Xposition, uint Yposition,uint n)
{
uintnum ;
LCD_SetRamAddr(Xposition,Xposition,Yposition ,Yposition);
for(num=2;num>0;num–)
LCD_Wirte_Data16(colors[n]);
}

void Xiexian(uint x1,uint y1,uint x2,uint y2,uint color)
{
uint t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy;
uint row,col;
delta_x = x2-x1;//核算坐标增量
delta_y = y2-y1;
col=x1;
row=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++)
{//画线输出
Dadian(col, row, color);
xerr+=delta_x;
yerr+=delta_y;
if(xerr>distance)
{
xerr-=distance;
col+=incx;
}
if(yerr>distance)
{
yerr-=distance;
row+=incy;
}
}
}
void main()
{
P2 = 0xff;
P0 = 0xff;
LCD_init();
while(1)
{
Xiexian(0,0,100,160,0);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/dianzi/257935.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部