微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > TFT画出矩形

TFT画出矩形

时间:11-26 来源:互联网 点击:
51单片机为平台,用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
};

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); //延时

}

voidLCD_clear(uchar n)
{
uint num;

LCD_SetRamAddr(0,127, 0,159);

for(num=20480;num>0;num--)//160*128=20480
{
LCD_Wirte_Data16(colors[n]);
}
}

voidLCD_Juxing(uint xStart, uint xEnd, uint yStart, uint yEnd,uchar n)
{
uint num,count;

LCD_SetRamAddr(xStart,xEnd, yStart,yEnd);
count=(xEnd-xStart)*(yEnd- yStart);
for(num=count;num>0;num--)//160*128=20480
{
LCD_Wirte_Data16(colors[n]);
}
}
void main()
{
P2 = 0xff;
P0 = 0xff;
LCD_init();
while(1)
{
LCD_Juxing(10, 120,10,150,3);
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top