#include "2440addr.h"
#include "def.h"
/**********************************************宏定义***********************************************/
#define LCD_X35 //64K色 240*320/3.5英寸TFT真彩液晶屏,每一条水平线上包含240个像素点,共有320条这样的线
#if defined(LCD_X35)
#define LCD_WIDTH 240
#define LCD_HEIGHT 320
#define LCD_PIXCLOCK 4
#define LCD_RIGHT_MARGIN 25
#define LCD_LEFT_MARGIN 0
#define LCD_HSYNC_LEN 4
#define LCD_UPPER_MARGIN 0
#define LCD_LOWER_MARGIN 4
#define LCD_VSYNC_LEN 9
#define LCD_CON5 ( (1 << 11)| (1<<0) | (1 << 8) | (1 << 6) | (1 << 9) | ( 1<< 10))
#endif
#define LCD_XSIZE LCD_WIDTH
#define LCD_YSIZE LCD_HEIGHT
#define SCR_XSIZE LCD_WIDTH
#define SCR_YSIZE LCD_HEIGHT
/**********************************************宏定义***********************************************/
/********************************************LCD控制函数********************************************/
volatile static unsigned short LCD_BUFFER[SCR_YSIZE][SCR_XSIZE]; //定义320行,240列的数组,用于存放显示数据
//延时函数
void delay(int times)
{
int i,j;
for(i=0;i<times;i++)
for(j=0;j<400;j++);
}
//LCD视频和控制信号输出或者停止,1开启视频显示数据输出
static void Lcd_EnvidOnOff(int onoff)
{
if(onoff==1)
rLCDCON1|=1; // ENVID=ON
else
rLCDCON1 =rLCDCON1 & 0x3fffe; // ENVID Off
}
//240×320 TFT LCD全屏填充特定颜色单元或清屏
static void Lcd_ClearScr( U16 c)
{
unsigned int x,y ;
for( y = 0 ; y < SCR_YSIZE ; y++ )
{
for( x = 0 ; x < SCR_XSIZE ; x++ )
{
LCD_BUFFER[y][x] = c ;
}
}
}
//在LCD屏幕上指定坐标点画一个指定大小的图片
static void Pait_Bmp(int x0,int y0,int h,int l,const unsigned char *bmp)
{
int x,y;
U32 c;
int p = 0;
for( y = 0 ; y < l ; y++ )
{
for( x = 0 ; x < h ; x++ )
{
c = bmp[p+1] | (bmp[p]<<8) ;
if ( ( (x0+x) < SCR_XSIZE) && ( (y0+y) < SCR_YSIZE) )
LCD_BUFFER[y0+y][x0+x] = c ;
p = p + 2 ;
}
}
}
//240*320 TFT LCD数据和控制端口初始化
static void Lcd_Port_Init( void )
{
rGPCUP=0xffffffff; //Disable Pull-up register
rGPCCON=0xaaaa02a8; //Initialize VD[7:0],VM,VFRAME,VLINE,VCLK
rGPDUP=0xffffffff; //Disable Pull-up register
rGPDCON=0xaaaaaaaa; //Initialize VD[15:8]
}
//240×320 TFT LCD功能模块初始化
static void LCD_Init(void)
{
#define M5D(n) ((n)&0x1fffff)
#define LCD_ADDR ((U32)LCD_BUFFER)
/*bit[17:8](4:CLKVAL);bit[6:5](11:TFT LCD panel);bit[4:1](1100:16 bpp for TFT)*/
rLCDCON1 = (LCD_PIXCLOCK << 8) | (3 << 5) | (12 << 1);
/*bit[31:24](1:VBPD);bit[23:14](320-1:行数);bit[13:6](5:VFPD);bit[5:0](1:VSPW)*/
rLCDCON2 = (LCD_UPPER_MARGIN << 24) | ((LCD_HEIGHT - 1) << 14) | (LCD_LOWER_MARGIN << 6) | (LCD_VSYNC_LEN << 0);
/*bit[25:19](36:HBPD);bit[18:8](240-1:列数);bit[7:0](19:HFPD)*/
rLCDCON3 = (LCD_RIGHT_MARGIN << 19) | ((LCD_WIDTH - 1) << 8) | (LCD_LEFT_MARGIN << 0);
/*bit[15:8](13:MVAL,只有当LCDCON1 bit[7]MMODE=1才有效);bit[7:0](5:HSPW)*/
rLCDCON4 = (13 << 8) | (LCD_HSYNC_LEN << 0);
#if !defined(LCD_CON5)
/*bit[11](5:6:5 Format);bit[9](VLINE/HSYNC polarity inverted);bit[8](VFRAME/VSYNC inverted)
bit[3](Enalbe PWERN signal),bit[1](half-word swap control bit)*/
# define LCD_CON5 ((1<<11) | (1 << 9) | (1 << 8) | (1 << 3) | (1 << 0))
#endif
rLCDCON5 = LCD_CON5;
/*
LCDBANK: 视频帧缓冲区内存地址30-22位
LCDBASEU: 视频帧缓冲区的开始地址21-1位
LCDBASEL: 视频帧缓冲区的结束地址21-1位
*/
/*bit[29:21]:LCDBANK,bit[20:0]:LCDBASEU*/
rLCDSADDR1 = ((LCD_ADDR >> 22) << 21) | ((M5D(LCD_ADDR >> 1)) << 0);
/*bit[20:0]:LCDBASEL*/
rLCDSADDR2 = M5D((LCD_ADDR + LCD_WIDTH * LCD_HEIGHT * 2) >> 1);
/*PAGEWIDTH:虚拟屏幕一行的字节数,如果不使用虚拟屏幕,设置为实际屏幕的行字节数
OFFSIZE:虚拟屏幕左侧偏移的字节数,如果不使用虚拟屏幕,设置为0
*/
/*bit[21:11]:OFFSIZE; bit[10:0]:PAGEWIDTH*/
rLCDSADDR3 = LCD_WIDTH;
/*屏蔽中断*/
rLCDINTMSK |= 3;
rTCONSEL &= (~7);
/*disable调色板*/
rTPAL = 0x0;
/*禁止LPC3600/LCC3600模式*/
rTCONSEL &= ~((1<<4) | 1);
}
/********************************************LCD控制函数********************************************/
/********************************************LCD绘图函数********************************************/
//绘点函数
void pixel(int x,int y,U16 c)
{
if ( (x < SCR_XSIZE) && (y < SCR_YSIZE) )
LCD_BUFFER[y][x] = c;
}
//绘直线函数
void line(int x1,int y1,int x2,int y2,int color)
{
int dx,dy,e;
dx=x2-x1;
dy=y2-y1;
if(dx>=0)
{
if(dy >= 0) // dy>=0
{
if(dx>=dy) // 1/8 octant
{
e=dy-dx/2;
while(x1<=x2)
{
pixel(x1,y1,color);
if(e>0)
{
y1+=1;
e-=dx;
}
x1+=1;
e+=dy;
}
}
else // 2/8 octant
{
e=dx-dy/2;
while(y1<=y2)
{
pixel(x1,y1,color);
if(e>0)
{
x1+=1;
e-=dy;
}
y1+=1;
e+=dx;
}
}
}
else // dy<0
{
dy=-dy; // dy=abs(dy)
if(dx>=dy) // 8/8 octant
{
e=dy-dx/2;
while(x1<=x2)
{
pixel(x1,y1,color);
if(e>0)
{
y1-=1;
e-=dx;
}
x1+=1;
e+=dy;
}
}
else // 7/8 octant
{
e=dx-dy/2;
while(y1>=y2)
{
pixel(x1,y1,color);
if(e>0)
{
x1+=1;
e-=dy;
}
y1-=1;
e+=dx;
}
}
}
}
else //dx<0
{
dx=-dx; //dx=abs(dx)
if(dy >= 0) // dy>=0
{
if(dx>=dy) // 4/8 octant
{
e=dy-dx/2;
while(x1>=x2)
{
pixel(x1,y1,color);
if(e>0)
{
y1+=1;
e-=dx;
}
x1-=1;
e+=dy;
}
}
else // 3/8 octant
{
e=dx-dy/2;
while(y1<=y2)
{
pixel(x1,y1,color);
if(e>0)
{
x1-=1;
e-=dy;
}
y1+=1;
e+=dx;
}
}
}
else // dy<0
{
dy=-dy; // dy=abs(dy)
if(dx>=dy) // 5/8 octant
{
e=dy-dx/2;
while(x1>=x2)
{
pixel(x1,y1,color);
if(e>0)
{
y1-=1;
e-=dx;
}
x1-=1;
e+=dy;
}
}
else // 6/8 octant
{
e=dx-dy/2;
while(y1>=y2)
{
pixel(x1,y1,color);
if(e>0)
{
x1-=1;
e-=dy;
}
y1-=1;
e+=dx;
}
}
}
}
}
//绘制矩形函数
void Rectangle(int x1,int y1,int x2,int y2,U16 color,int mode)
{
int i;
if(mode==1) //填充
for(i=y1;i<=y2;i++)
line(x1,i,x2,i,color);
if(mode==0) //不填充
{
line(x1,y1,x2,y1,color);
line(x1,y2,x2,y2,color);
line(x1,y1,x1,y2,color);
line(x2,y1,x2,y2,color);
}
}
//绘制圆形函数
void _draw_circle_8(int xc,int yc,int x,int y,U16 c)
{
pixel( xc + x, yc + y, c);
pixel( xc - x, yc + y, c);
pixel( xc + x, yc - y, c);
pixel( xc - x, yc - y, c);
pixel( xc + y, yc + x, c);
pixel( xc - y, yc + x, c);
pixel( xc + y, yc - x, c);
pixel( xc - y, yc - x, c);
}
void Circle(int xc,int yc,int r,U16 c,int mode)
{
int x,y,yi,d;
x = 0;
y = r;
d = 3 - 2 * r;
if(mode) //填充
{
while (x <= y)
{
for (yi = x; yi <= y; yi ++)
_draw_circle_8( xc, yc, x, yi, c);
if (d < 0)
d = d + 4 * x + 6;
else
{
d = d + 4 * (x - y) + 10;
y --;
}
x++;
}
}
else //不填充
{
while (x <= y)
{
_draw_circle_8( xc, yc, x, y, c);
if (d < 0)
d = d + 4 * x + 6;
else
{
d = d + 4 * (x - y) + 10;
y --;
}
x ++;
}
}
}
/********************************************LCD绘图函数********************************************/
//lcd测试程序
void TFT_LCD_Test(void)
{
U16 white;
U16 black;
U16 red;
U16 green;
U16 blue;
int i;
white=(0x1f<<11) | (0x3f<<5) | (0x1f);
black=(0x00<<11) | (0x00<<5) | (0x00);
red=(0x1f<<11) | (0x00<<5) | (0x00);
green=(0x00<<11) | (0x3f<<5) | (0x00);
blue= (0x00<<11) | (0x00<<5) | (0x1f);
Lcd_Port_Init();
LCD_Init();
Lcd_EnvidOnOff(1); //turn on vedio
//线
Lcd_ClearScr(white); //clear screen white
line(20,20,200,200,red);
delay(500000);
//矩形,不填充
Lcd_ClearScr(white); //clear screen white
Rectangle(20,20,200,200,red,0);
delay(500000);
//矩形,填充
Lcd_ClearScr(white); //clear screen white
Rectangle(20,20,200,200,red,1);
delay(500000);
//圆形,不填充
Lcd_ClearScr(white); //clear screen white
Circle( 100, 100, 90, red,0);
delay(500000);
//圆形,填充
Lcd_ClearScr(white); //clear screen white
Circle( 100, 100, 90, red,1);
delay(500000);
//100*100,边缘宽度为10,颜色为RGB循环显示
Lcd_ClearScr(white); //clear screen white
for(i=0;i<6;i++)
{
Rectangle(100,100,200,200,black,1);
Rectangle(100+10,100+10,200-10,200-10,red,1);
delay(100000);
Rectangle(100+10,100+10,200-10,200-10,green,1);
delay(100000);
Rectangle(100+10,100+10,200-10,200-10,blue,1);
delay(100000);
}
}
int main(int argc, char *argv[])
{
TFT_LCD_Test() ;
return 0;
}