📄 lcd.c.bak
字号:
#include <P24FJ128GA006.H>
//#include <stdio.h>
#include <string.h>
#include "lcd.h"
#include "bmp.h"
#include "menu.h"
#include "commuse.h"
#include "flash.h"
#include "generic.h"
//--------------------------------------------------------------------------
//
// lcd.c zhouyi 2008.5.10
//
//--------------------------------------------------------------------------
unsigned int TextColor;
unsigned int BackColor;
void PORT_INI()
{
PMMODEbits.MODE = 0b10; // Master 2
PMMODEbits.WAITB = 0b00;
PMMODEbits.WAITM = 0b0001;
PMMODEbits.WAITE = 0b00;
PMAENbits.PTEN15 = 1; // Address line 0 is enabled
PMAENbits.PTEN14 = 1;
PMCONbits.CSF = 0b10;
PMCONbits.CS2P = 1;
PMCONbits.CS1P = 1;
PMCONbits.PTRDEN = 1;
PMCONbits.PTWREN = 1;
PMCONbits.PMPEN = 1;
PMADDR = 0x0000;
LCD_RST_DDR = 0;
LCD_RST_L();
return;
}
//-----------------------------------------------------------------------
//
// write lcd index regster
//
//-----------------------------------------------------------------------
void LCD_WR_REG(unsigned char Lindex,unsigned int val)
{
char Hindex = 0x00;
PMADDR = 0x0000;
PMDIN1 = Hindex;
NOP();
PMDIN1 = Lindex;
PMADDR = 0x8000;
PMDIN1 = (unsigned char)(val >> 8);
NOP();
PMDIN1 = (unsigned char)val;
NOP();
PMADDR |= 0x4000;
}
void LCD_Write_Start()
{
//disable interrupts
PMADDR = 0x0000;
PMDIN1 = 0x00;
NOP();
PMDIN1 = 0x22;
NOP();
PMADDR = 0x8000;
}
void LCD_Write_Data(unsigned int val)
{
PMDIN1 = (unsigned char)(val >> 8);
NOP();
PMDIN1 = (unsigned char)val;
NOP();
}
void LCD_Write_LCD_Data(unsigned char val)
{
PMDIN1 = (unsigned char)val;
NOP();
}
void LCD_Write_End()
{
PMADDR |=0x40;
}
void delay_ms(unsigned int iMs)
{
int loop;
while(iMs--)
{
for(loop=0;loop<1160;loop++)
{
Nop();
}
}
return;
}
void Display_ON()
{
LCD_WR_REG(0x07,0x0173);
}
void Display_OFF()
{
LCD_WR_REG(0x07,0x0000);
}
void LCD_Init()
{
PORT_INI();
LCD_RST_L();
delay_ms(10);
LCD_RST_H();
//delay 10ms
delay_ms(10);
//initializing funciton 1
LCD_WR_REG(0xe5,0x8000); // Set the internal vcore voltage
LCD_WR_REG(0x00,0x0001); // start OSC
LCD_WR_REG(0x2b,0x0010); //Set the frame rate as 80 when the internal resistor is used for oscillator circuit
LCD_WR_REG(0x01,0x0100); //s720 to s1 ; G1 to G320
LCD_WR_REG(0x02,0x0700); //set the line inversion
LCD_WR_REG(0x03,0x1018); //65536 colors
//LCD_WR_REG(0x03,0x1030);
LCD_WR_REG(0x04,0x0000);
LCD_WR_REG(0x08,0x0202); //specify the line number of front and back porch periods respectively
LCD_WR_REG(0x09,0x0000);
LCD_WR_REG(0x0a,0x0000);
LCD_WR_REG(0x0c,0x0000); //select internal system clock
LCD_WR_REG(0x0d,0x0000);
LCD_WR_REG(0x0f,0x0000);
LCD_WR_REG(0x50,0x0000); //0x50 -->0x53 set windows adress
LCD_WR_REG(0x51,0x00ef);
LCD_WR_REG(0x52,0x0000);
LCD_WR_REG(0x53,0x013f);
LCD_WR_REG(0x60,0x2700);
LCD_WR_REG(0x61,0x0001);
LCD_WR_REG(0x6a,0x0000);
LCD_WR_REG(0x80,0x0000);
LCD_WR_REG(0x81,0x0000);
LCD_WR_REG(0x82,0x0000);
LCD_WR_REG(0x83,0x0000);
LCD_WR_REG(0x84,0x0000);
LCD_WR_REG(0x85,0x0000);
LCD_WR_REG(0x90,0x0010);
LCD_WR_REG(0x92,0x0000);
LCD_WR_REG(0x93,0x0003);
LCD_WR_REG(0x95,0x0110);
LCD_WR_REG(0x97,0x0000);
LCD_WR_REG(0x98,0x0000);
//power setting function
LCD_WR_REG(0x10,0x0000);
LCD_WR_REG(0x11,0x0000);
LCD_WR_REG(0x12,0x0000);
LCD_WR_REG(0x13,0x0000);
delay_ms(200);
LCD_WR_REG(0x10,0x17b0);
LCD_WR_REG(0x11,0x0004);
delay_ms(50);
LCD_WR_REG(0x12,0x013e);
delay_ms(50);
LCD_WR_REG(0x13,0x1f00);
LCD_WR_REG(0x29,0x000f);
delay_ms(50);
LCD_WR_REG(0x20,0x0000);
LCD_WR_REG(0x21,0x0000);
//initializing function 2
LCD_WR_REG(0x30,0x0204);
LCD_WR_REG(0x31,0x0001);
LCD_WR_REG(0x32,0x0000);
LCD_WR_REG(0x35,0x0206);
LCD_WR_REG(0x36,0x0600);
LCD_WR_REG(0x37,0x0500);
LCD_WR_REG(0x38,0x0505);
LCD_WR_REG(0x39,0x0407);
LCD_WR_REG(0x3c,0x0500);
LCD_WR_REG(0x3d,0x0503);
//display on
LCD_WR_REG(0x07,0x0173);
Display_ON();
}
void LCD_Set_XY(unsigned int x, unsigned int y)
{
LCD_WR_REG(0x20,x);
LCD_WR_REG(0x21,y);
}
void LCD_Set_Window(unsigned int startX,unsigned int startY,unsigned int endX,unsigned int endY)
{
LCD_Set_XY(startX,startY);
LCD_WR_REG(0x50, startX);
LCD_WR_REG(0x51, endX);
LCD_WR_REG(0x52, startY);
LCD_WR_REG(0x53, endY);
//LCD_Set_XY(endX, endY);
}
void LCD_test()
{
unsigned int i,j;
LCD_Set_Window(0,0,239,319);
LCD_Write_Start();
for(i=0;i<320;i++)
{ for(j=0;j<240;j++)
{
if(i>279)LCD_Write_Data(0x0000);
else if(i>239)LCD_Write_Data(0x001f);
else if(i>199)LCD_Write_Data(0x07e0);
else if(i>159)LCD_Write_Data(0x07ff);
else if(i>119)LCD_Write_Data(0xf800);
else if(i>79)LCD_Write_Data(0xf81f);
else if(i>39)LCD_Write_Data(0xffe0);
else LCD_Write_Data(0xffff);
}
}
LCD_Write_End();
}
void fillrect(unsigned int startX,unsigned int startY,unsigned int endX,unsigned int endY,unsigned int color)
{
int x,y;
int totalPixels;
if((startX < 0)||(startY < 0)||(endX > 239)||(endY > 319))
{
return ;
}
x = (endX >= startX) ? (endX - startX):(startX - endX);
y = (endY >= startY) ? (endY - startY):(startY - endY);
totalPixels = x*y;
LCD_Set_Window(startX,startY, endX,endY);
LCD_Write_Start();
__asm__ volatile( "disi #0x3FFF" ); //disable interrupts
while(totalPixels--)
{
LCD_Write_Data(color);
}
__asm__ volatile( "disi #0x0000" );
LCD_Write_End();
draw_v_line(startY,endY,endX,color);
}
void putpixel(unsigned int x,unsigned int y,unsigned int color)
{
if((x < 0)||(y < 0)||(x > 239)||(y > 319))
{
return ;
}
LCD_Set_Window(x,y,x+1,y+1);
// LCD_Set_XY(x,y);
LCD_Write_Start();
LCD_Write_Data(color);
LCD_Write_End();
return;
}
void LCD_SetTextColor(unsigned int Color)
{
TextColor = Color;
return;
}
void LCD_SetBackColor(unsigned int Color)
{
BackColor = Color;
return;
}
void draw_h_line(int x_start, int x_end, int y,unsigned int color)
{
unsigned int i;
unsigned int real_start = x_start;
unsigned int real_end = x_end;
if(real_start >= real_end)
{
x_start = real_end;
x_end = real_start;
}
for(i = x_start; i <=x_end; i ++)
{
putpixel(i, y, color);
}
return;
}
void draw_v_line(int y_start, int y_end, int x,unsigned int color)
{
unsigned int i;
unsigned int start = y_start;
unsigned int end = y_end;
if(y_start >= y_end)
{
start = y_end;
end = y_start;
}
for(i = start; i <= end; i ++)
{
putpixel(x, i, color);
}
return;
}
void Octant0(unsigned int x_bgn, unsigned int y_bgn, unsigned int x_delta, unsigned int y_delta, int XDirection,unsigned int color)
{
int y_deltax2;
int y_deltax2Minusx_deltax2;
int ErrorTerm;
y_deltax2 = (y_delta <<1);
y_deltax2Minusx_deltax2 = y_deltax2 - (int) ( x_delta <<1 );
ErrorTerm = y_deltax2 - (int) x_delta;
putpixel(x_bgn, y_bgn, color);
while ( x_delta-- )
{
if ( ErrorTerm >= 0 )
{
y_bgn++;
ErrorTerm += y_deltax2Minusx_deltax2;
}
else
{
ErrorTerm += y_deltax2;
}
x_bgn += XDirection; // advance the X coordinate
putpixel(x_bgn, y_bgn, color); // draw a pixel
}
return;
}
void Octant1(unsigned int x_bgn, unsigned int y_bgn,unsigned int x_delta,unsigned int y_delta, int XDirection,unsigned int color)
{
int x_deltax2;
int x_deltax2Minusy_deltax2;
int ErrorTerm;
x_deltax2 = x_delta <<1;
x_deltax2Minusy_deltax2 = x_deltax2 - (int) ( y_delta <<1 );
ErrorTerm = x_deltax2 - (int) y_delta;
putpixel(x_bgn, y_bgn, color);
while ( y_delta-- )
{
if ( ErrorTerm >= 0 )
{
x_bgn += XDirection;
ErrorTerm += x_deltax2Minusy_deltax2;
}
else
{
ErrorTerm += x_deltax2;
}
y_bgn++;
putpixel(x_bgn, y_bgn, color);
}
return;
}
void line(int x_bgn, int y_bgn, int x_end, int y_end,unsigned int color)
{
int x_delta;
int y_delta;
int temp;
if(y_bgn == y_end)
{
draw_h_line(x_bgn, x_end, y_bgn,color);
return;
}
else if(x_bgn == x_end)
{
draw_v_line(y_bgn, y_end, x_bgn,color);
return;
}
if(y_bgn > y_end)
{
temp = y_bgn;
y_bgn = y_end;
y_end = temp;
temp = x_bgn;
x_bgn = x_end;
x_end = temp;
}
x_delta = x_end - x_bgn;
y_delta = y_end - y_bgn;
if(x_delta > 0)
{
if(x_delta > y_delta)
{
Octant0(x_bgn, y_bgn, x_delta, y_delta, 1,color);
}
else
{
Octant1(x_bgn, y_bgn, x_delta, y_delta, 1,color);
}
}
else
{
x_delta = -x_delta;
if(x_delta > y_delta)
{
Octant0(x_bgn, y_bgn, x_delta, y_delta, -1,color);
}
else
{
Octant1(x_bgn, y_bgn, x_delta, y_delta, -1,color);
}
}
return;
}
void rectangle(int left,int top,int right,int bottom,unsigned int color)
{
line(left,top,right,top,color);
line(left,bottom,right,bottom,color);
line(left,top,left,bottom,color);
line(right,top,right,bottom,color);
return ;
}
//圆角矩形
void roundrect(int left, int top, int right, int bottom, int a, int b,unsigned int color)
{
BOOL flag = FALSE;
int x;
int y;
long aa;
long bb;
long d;
int t;
/* + draw rectangle */
draw_h_line( left, right, top - b,color); //绘制水平线
draw_h_line( left, right, bottom + b,color);
draw_v_line( top, bottom, left - a ,color);
draw_v_line( top, bottom, right + a, color);
/* - draw rectangle */
/* + draw ellipse */
if (a < b)
{
flag = TRUE;
t = a;
a = b;
b = t;
}
aa = (long)a * a;
bb = (long)b * b;
x = 0;
y = b;
d = 4 * bb + aa * (-b * 4 + 1);
while (4 * bb * (x + 1) < aa * (4 * y - 2))
{
if (!flag) putpixel(right + x, bottom + y, color);
else putpixel(right + y, bottom + x, color);
if (!flag) putpixel(right + x, top - y, color);
else putpixel(right + y, top - x, color);
if (!flag) putpixel(left - x, bottom + y, color);
else putpixel(left - y, bottom + x, color);
if (!flag) putpixel(left - x, top - y, color);
else putpixel(left - y, top - x, color);
if (d < 0)
{
d += 4 * bb * (2 * x + 3);
x++;
}
else
{
d += 4 * bb * (2 * x + 3) + 4 * aa * (-2 * y + 2);
x++;
y--;
}
}
y = 0;
x = a;
d = 4 * aa + bb * (-a * 4 + 1);
while (4 * bb * (x + 1) > aa * (4 * y - 2))
{
if (!flag) putpixel( right + x, bottom + y, color);
else putpixel( right + y, bottom + x, color);
if (!flag) putpixel(right + x, top - y, color);
else putpixel( right + y, top - x, color);
if (!flag) putpixel(left - x, bottom + y,color);
else putpixel( left - y, bottom + x, color);
if (!flag) putpixel( left - x, top - y,color);
else putpixel(left - y, top - x, color);
if (d < 0)
{
d += 4 * aa * (2 * y + 3);
y++;
}
else
{
d += 4 * aa * (2 * y + 3) + 4 * bb * (-2 * x + 2);
y++;
x--;
}
}
/* - draw ellipse */
}
/* 绘制填充圆角矩形的函数 */
void fillroundrect( int left, int top, int right, int bottom, int a, int b,unsigned int color)
{
BOOL flag = FALSE;
int x;
int y;
long aa;
long bb;
long d;
int t;
/* + draw filled rectangle */
for (y=top; y<bottom; y++) draw_h_line( left - a, right + a, y,color);
/* - draw filled rectangle */
/* + draw filled ellipse */
if (a < b)
{
flag = TRUE;
t = a;
a = b;
b = t;
}
aa = (long)a * a;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -