#ifndef __LCD_H_
#define __LCD_H_
#include "common.h"
#include "debugserial.h"
#include "delay.h"
#include "sdram.h"
#define White 0xFFFFFF
#define Black 0x000000
#define Blue 0xFF0000
#define Red 0x0000FF
#define Green 0x00FF00
#define Yellow 0x00FFFF
#define purple 0xff00ff
#define pixel_clock 9000000 //像素时钟设置为9M
#define lcd_h_size 480 //根据数据手册典型值设置
#define lcd_h_pulse 2
#define lcd_h_back_proch 43
#define lcd_h_front_porch 8
#define lcd_v_size 272
#define lcd_v_pulse 10
#define lcd_v_back_proch 12
#define lcd_v_front_porch 4
#define lcd_show_delay 10000
#define LCD_VRAM_BASE_ADDR ((unsigned long)SDRAM_BASE_ADDR + 0x00010000)//显示基地址
#define LCD_CURSOR_BASE_ADDR ((unsigned long)0x20088800)
void lcd_init(u32 defaultColor);
void lcd_clear(u32 color);
void lcd_set_backlight(u8 enable);
void lcd_draw_point(u32 x,u32 y,u32 color);
u32 lcd_read_point(u32 x,u32 y);
void lcd_draw_line(u16 x1, u16 y1, u16 x2, u16 y2,u32 color);
void lcd_draw_rect(u16 x1, u16 y1, u16 x2, u16 y2,u32 color);
void lcd_draw_cicle(u16 x0,u16 y0,u8 r,u32 color);
void lcd_test(u32 color);
#endif
#include "lcd.h"
//lcd io初始化
static void lcd_in_init(void)
{
LPC_SC->PCONP |= (1<<15);//打开时钟
/* R */
LPC_IOCON->P0_4 = 0x27; // LCD_VD_0
LPC_IOCON->P0_5 = 0x27; // LCD_VD_1
LPC_IOCON->P4_28 = 0x27; // LCD_VD_2
LPC_IOCON->P4_29 = 0x27; // LCD_VD_3
LPC_IOCON->P2_6 = 0x27; // LCD_VD_4
LPC_IOCON->P2_7 = 0x27; // LCD_VD_5
LPC_IOCON->P2_8 = 0x27; // LCD_VD_6
LPC_IOCON->P2_9 = 0x27; // LCD_VD_7
/* G */
LPC_IOCON->P0_6 = 0x27; // LCD_VD_8
LPC_IOCON->P0_7 = 0x27; // LCD_VD_9
LPC_IOCON->P1_20 = 0x27; // LCD_VD_10
LPC_IOCON->P1_21 = 0x27; // LCD_VD_11
LPC_IOCON->P1_22 = 0x27; // LCD_VD_12
LPC_IOCON->P1_23 = 0x27; // LCD_VD_13
LPC_IOCON->P1_24 = 0x27; // LCD_VD_14
LPC_IOCON->P1_25 = 0x27; // LCD_VD_15
/* B */
LPC_IOCON->P0_8 = 0x27; // LCD_VD_16
LPC_IOCON->P0_9 = 0x27; // LCD_VD_17
LPC_IOCON->P2_12 = 0x27; // LCD_VD_18
LPC_IOCON->P2_13 = 0x27; // LCD_VD_19
LPC_IOCON->P1_26 = 0x27; // LCD_VD_20
LPC_IOCON->P1_27 = 0x27; // LCD_VD_21
LPC_IOCON->P1_28 = 0x27; // LCD_VD_22
LPC_IOCON->P1_29 = 0x27; // LCD_VD_23
LPC_IOCON->P2_2 = 0x27; // LCD_DCLK
LPC_IOCON->P2_5 = 0x27; // LCD_LP -- HSYNC
LPC_IOCON->P2_3 = 0x27; // LCD_FP -- VSYNC
LPC_IOCON->P2_4 = 0x27; // LCD_ENAB_M -- LCDDEN
LPC_IOCON->P2_0 = 0x27; // LCD_PWR
}
void lcd_init(u32 defaultColor)
{
u32 *pDst = (u32 *)LCD_VRAM_BASE_ADDR;//显示数据基地址
u32 i= 0;
//打开LCD时钟
LPC_SC->PCONP |= 1<<0;
lcd_in_init();//初始化IO
//初始化背光引脚
LPC_IOCON->P2_1 = 0x00;
LPC_IOCON->P2_1 |= (2<<3);//控制背光
P2dir(1) = 1;
P2high(1) = 1;
LPC_LCD->CRSR_CTRL &=~(1<<0);//禁止光标
LPC_LCD->CTRL = 0;//禁止并清零LCD
LPC_LCD->CTRL &= ~(7<<1);//清零面板设置
LPC_LCD->CTRL |= (5<<1);//设置面板tft 24bpp
LPC_LCD->CTRL |= (1<<5);//tft模式
LPC_LCD->CTRL &= ~(1<<7);//单面板
LPC_LCD->CTRL &= ~(1<<8);//RGB颜色
LPC_LCD->CTRL &= ~(1<<9);//小端数据存储
LPC_LCD->CTRL &= ~(1<<10);//像素排序小端
LPC_LCD->CTRL &= ~(1<<11);//信号先维持低电平
LPC_SC->LCD_CFG = ApbClock / pixel_clock;//设置像素时钟 根据数据手册来
LPC_LCD->POL |=(1<<26);//像素时钟分频旁路
LPC_LCD->POL &= ~(1<<5);//模块选择cclk时钟
LPC_LCD->POL |= (1<<11);//FB管脚低电平有效
LPC_LCD->POL |= (1<<12);//LP管脚低电平有效
LPC_LCD->POL &= ~(1<<13);//像素信号于时钟上升沿驱动
LPC_LCD->POL &= ~(1<<14);//高电平输出使能
LPC_LCD->POL &= ~(0x3FF <<16);//清除h_szie
LPC_LCD->POL |= (lcd_h_size-1)<<16;//设置h_size
//设置水平时序控制
LPC_LCD->TIMH = 0;
LPC_LCD->TIMH |= (lcd_h_back_proch - 1)<<24;//水平后延
LPC_LCD->TIMH |= (lcd_h_front_porch - 1)<<16;//水平前沿
LPC_LCD->TIMH |= (lcd_h_pulse - 1)<<8; //水平同步脉冲宽度
LPC_LCD->TIMH |= ((lcd_h_size/16) - 1)<<2;//每一行的像素点
//设置竖直时序控制
LPC_LCD->TIMV = 0;
LPC_LCD->TIMV |= (lcd_v_back_proch)<<24;//竖直后延
LPC_LCD->TIMV |= (lcd_v_front_porch)<<16;//竖直前沿
LPC_LCD->TIMV |= (lcd_v_pulse - 1)<<10;//竖直脉冲宽度
LPC_LCD->TIMV |= lcd_v_size - 1; //像素
// 设置显示缓冲区地址
LPC_LCD->UPBASE = LCD_VRAM_BASE_ADDR & (~(7<<0)) ;
LPC_LCD->LPBASE = LCD_VRAM_BASE_ADDR & (~(7<<0)) ;
for( i = 0; (lcd_h_size * lcd_v_size) > i; i++)
{
*pDst++ = defaultColor;//初始化颜色
}
i = lcd_show_delay;
while(i--);
LPC_LCD->CTRL |= (1<<0);//使能lcd
LPC_LCD->CTRL |= (1<<11);//使能lcd功率
}
void lcd_clear(u32 color)
{
u32 *pDst = (u32 *)LCD_VRAM_BASE_ADDR;//显示数据基地址
u32 i= 0;
for( i = 0; (lcd_h_size * lcd_v_size) > i; i++)
{
*pDst++ = color;//初始化颜色
}
}
void lcd_set_backlight(u8 enable)
{
if(enable)P2high(1) = 1;
else P2low(1) = 1;
}
void lcd_draw_point(u32 x,u32 y,u32 color)
{
u32 *pDst = (u32 *)LCD_VRAM_BASE_ADDR;//显示数据基地址
if(x < lcd_h_size && y < lcd_v_size)
{
pDst = pDst+x + (y*lcd_h_size);
*pDst = color;
}
}
u32 lcd_read_point(u32 x,u32 y)
{
u32 *pDst = (u32 *)LCD_VRAM_BASE_ADDR;//显示数据基地址
u32 color = 0x00000000;
if(x < lcd_h_size && y < lcd_v_size)
{
pDst = pDst+x + (y*lcd_h_size);
color = *pDst;
}
return color;
}
//x1,y1:起点坐标
//x2,y2:终点坐标
void lcd_draw_line(u16 x1, u16 y1, u16 x2, u16 y2,u32 color)
{
u16 t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1; //计算坐标增量
delta_y=y2-y1;
uRow=x1;
uCol=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++ )//画线输出
{
lcd_draw_point(uRow,uCol,color);//画点
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
//画矩形
//(x1,y1),(x2,y2):矩形的对角坐标
void lcd_draw_rect(u16 x1, u16 y1, u16 x2, u16 y2,u32 color)
{
lcd_draw_line(x1,y1,x2,y1,color);
lcd_draw_line(x1,y1,x1,y2,color);
lcd_draw_line(x1,y2,x2,y2,color);
lcd_draw_line(x2,y1,x2,y2,color);
}
//在指定位置画一个指定大小的圆
//(x,y):中心点
//r :半径
void lcd_draw_cicle(u16 x0,u16 y0,u8 r,u32 color)
{
int a,b;
int di;
a=0;b=r;
di=3-(r<<1); //判断下个点位置的标志
while(a<=b)
{
lcd_draw_point(x0+a,y0-b,color); //5
lcd_draw_point(x0+b,y0-a,color); //0
lcd_draw_point(x0+b,y0+a,color); //4
lcd_draw_point(x0+a,y0+b,color); //6
lcd_draw_point(x0-a,y0+b,color); //1
lcd_draw_point(x0-b,y0+a,color);
lcd_draw_point(x0-a,y0-b,color); //2
lcd_draw_point(x0-b,y0-a,color); //7
a++;
//使用Bresenham算法画圆
if(di<0)di +=4*a+6;
else
{
di+=10+4*(a-b);
b--;
}
}
}
void lcd_test(u32 color)
{
u32 x = lcd_h_size/2;
u32 y = lcd_v_size/2;
u32 i = 0, j = 0;
while(1)
{
if(i >= x && j >= y)break;
if(i < x)i++;
if(j < y)j++;
lcd_draw_rect(x-i,y-j,x+i,y+j,color);
DelayMs(10);
}
}
原文:http://www.cnblogs.com/dengxiaojun/p/4338709.html