>_<:引脚和前面几个连接一样,这里做了一个实用的动画效果,模拟起重机的2维视图。

 #ifndef __ILI9325_H__
#define __ILI9325_H__ void ILI9325_Initial(void);
void Write_Cmd_Data(unsigned char x, unsigned int y);
void Write_Cmd(unsigned char DH,unsigned char DL);
void Write_Data(unsigned char DH,unsigned char DL);
void delayms(unsigned int tt);
void Write_Data_U16(unsigned int y);
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1);
void CLR_Screen(unsigned int bColor);
void Put_pixel(unsigned char x,unsigned char y,unsigned int color);
void Line(unsigned int X0,
unsigned int Y0,
unsigned int X1,
unsigned int Y1,
unsigned int color);
void Rectangle(unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom,
unsigned int color);
#endif

9325tp.h

 /*----------------------------------------------------------------
320x240彩屏液晶驱动程序
----------------------------------------------------------------*/
#include"9325tp.h"
#include"reg52.h"
/*----------------------------------------------------------------
全局变量
----------------------------------------------------------------*/
#define WINDOW_XADDR_START 0x0050 // Horizontal Start Address Set
#define WINDOW_XADDR_END 0x0051 // Horizontal End Address Set
#define WINDOW_YADDR_START 0x0052 // Vertical Start Address Set
#define WINDOW_YADDR_END 0x0053 // Vertical End Address Set
#define GRAM_XADDR 0x0020 // GRAM Horizontal Address Set
#define GRAM_YADDR 0x0021 // GRAM Vertical Address Set
#define GRAMWR 0x0022 // memory write #define DataPort P0 //数据口使用DataPort /*----------------------------------------------------------------
定义硬件端口
----------------------------------------------------------------*/
sbit CS=P2^; //片选
sbit RES=P2^; //复位
sbit RS=P2^; //数据/命令选择
sbit RW=P2^;
/*----------------------------------------------------------------
清屏函数
输入参数:bColor 清屏所使用的背景色
----------------------------------------------------------------*/
void CLR_Screen(unsigned int bColor)
{
unsigned int i,j;
LCD_SetPos(,,,);//320x240
for (i=;i<;i++)
{ for (j=;j<;j++)
Write_Data_U16(bColor); }
}
/*----------------------------------------------------------------
写命令、写数据
输入参数:x 需要输入的命令 16位
y 需要输入的数据 16位
----------------------------------------------------------------*/
void Write_Cmd_Data (unsigned char x,unsigned int y)
{
unsigned char m,n;
m=y>>;
n=y;
Write_Cmd(0x00,x);
Write_Data(m,n); } /*----------------------------------------------------------------
写16位数据
----------------------------------------------------------------*/
void Write_Data_U16(unsigned int y)
{
unsigned char m,n;
m=y>>;
n=y;
Write_Data(m,n); }
/*----------------------------------------------------------------
写命令
----------------------------------------------------------------*/ void Write_Cmd(unsigned char DH,unsigned char DL)
{
CS=;
RS=; DataPort=DH;
RW=;
RW=; DataPort=DL; RW=;
RW=;
CS=;
} /*----------------------------------------------------------------
写数据 双8位
----------------------------------------------------------------*/
void Write_Data(unsigned char DH,unsigned char DL)
{ CS=; RS=;
DataPort=DH;
RW=;
RW=; DataPort=DL;
RW=;
RW=;
CS=;
}
/*----------------------------------------------------------------
延时函数
----------------------------------------------------------------*/
void delayms(unsigned int count)
{
int i,j;
for(i=;i<count;i++)
{
for(j=;j<;j++);
}
}
/*----------------------------------------------------------------
液晶初始化
----------------------------------------------------------------*/
void ILI9325_Initial(void)
{
CS=;
delayms();
RES=;
delayms();
RES=;
delayms();
Write_Cmd_Data(0x0001,0x0100);
Write_Cmd_Data(0x0002,0x0700);
Write_Cmd_Data(0x0003,0x1030);
Write_Cmd_Data(0x0004,0x0000);
Write_Cmd_Data(0x0008,0x0207);
Write_Cmd_Data(0x0009,0x0000);
Write_Cmd_Data(0x000A,0x0000);
Write_Cmd_Data(0x000C,0x0000);
Write_Cmd_Data(0x000D,0x0000);
Write_Cmd_Data(0x000F,0x0000);
//power on sequence VGHVGL
Write_Cmd_Data(0x0010,0x0000);
Write_Cmd_Data(0x0011,0x0007);
Write_Cmd_Data(0x0012,0x0000);
Write_Cmd_Data(0x0013,0x0000);
//vgh
Write_Cmd_Data(0x0010,0x1290);
Write_Cmd_Data(0x0011,0x0227);
//delayms(100);
//vregiout
Write_Cmd_Data(0x0012,0x001d); //0x001b
//delayms(100);
//vom amplitude
Write_Cmd_Data(0x0013,0x1500);
//delayms(100);
//vom H
Write_Cmd_Data(0x0029,0x0018);
Write_Cmd_Data(0x002B,0x000D); //gamma
Write_Cmd_Data(0x0030,0x0004);
Write_Cmd_Data(0x0031,0x0307);
Write_Cmd_Data(0x0032,0x0002);//
Write_Cmd_Data(0x0035,0x0206);
Write_Cmd_Data(0x0036,0x0408);
Write_Cmd_Data(0x0037,0x0507);
Write_Cmd_Data(0x0038,0x0204);//
Write_Cmd_Data(0x0039,0x0707);
Write_Cmd_Data(0x003C,0x0405);//
Write_Cmd_Data(0x003D,0x0F02);
//ram
Write_Cmd_Data(0x0050,0x0000);
Write_Cmd_Data(0x0051,0x00EF);
Write_Cmd_Data(0x0052,0x0000);
Write_Cmd_Data(0x0053,0x013F);
Write_Cmd_Data(0x0060,0xA700);
Write_Cmd_Data(0x0061,0x0001);
Write_Cmd_Data(0x006A,0x0000);
//
Write_Cmd_Data(0x0080,0x0000);
Write_Cmd_Data(0x0081,0x0000);
Write_Cmd_Data(0x0082,0x0000);
Write_Cmd_Data(0x0083,0x0000);
Write_Cmd_Data(0x0084,0x0000);
Write_Cmd_Data(0x0085,0x0000);
//
Write_Cmd_Data(0x0090,0x0010);
Write_Cmd_Data(0x0092,0x0600);
Write_Cmd_Data(0x0093,0x0003);
Write_Cmd_Data(0x0095,0x0110);
Write_Cmd_Data(0x0097,0x0000);
Write_Cmd_Data(0x0098,0x0000);
Write_Cmd_Data(0x0007,0x0133); // Write_Cmd_Data(0x0022);//
}
/*----------------------------------------------------------------
画点
输入参数:x,y 需要画点坐标
color 点的颜色
----------------------------------------------------------------*/
void Put_pixel(unsigned char x,unsigned char y,unsigned int color)
{
LCD_SetPos(x,x,y,y);
Write_Data_U16(color);
}
/*----------------------------------------------------------------
设置坐标
----------------------------------------------------------------*/
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
{
Write_Cmd_Data(WINDOW_XADDR_START,x0);
Write_Cmd_Data(WINDOW_XADDR_END,x1);
Write_Cmd_Data(WINDOW_YADDR_START,y0);
Write_Cmd_Data(WINDOW_YADDR_END,y1);
Write_Cmd_Data(GRAM_XADDR,x0);
Write_Cmd_Data(GRAM_YADDR,y0);
Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
}
/*----------------------------------------------------------------
在屏幕上画线
输入参数: 起始坐标X0,Y0,终止坐标X1,Y1
color 线颜色
----------------------------------------------------------------*/
void Line(unsigned int X0,
unsigned int Y0,
unsigned int X1,
unsigned int Y1,
unsigned int color)
{
int dx = X1 - X0;
int dy = Y1 - Y0;
int P = * dy - dx;
int dobDy = * dy;
int dobD = * (dy - dx);
int PointX = ,PointY = ;
int incx = ,incy = ;
int distance = ,xerr = ,yerr = ;
unsigned int i = ; if(dx == ) //k=1斜率为1
{
PointX = X0;
if(Y0 < Y1)
{
PointY = Y0;
}
else
{
PointY = Y1;
}
for(i = ;i <= ((Y0<Y1) ? (Y1-Y0) : (Y0-Y1));i++)
{ Put_pixel(PointX,PointY,color);
PointY++;
}
return;
}
if(dy == ) //k=0斜率为0
{
PointY = Y0;
if(X0 < X1)
{
PointX = X0;
}
else
{
PointX = X1;
}
for(i = ;i <= ((X0<X1) ? (X1-X0) : (X0-X1));i++)
{ Put_pixel(PointX,PointY,color);
PointX++;
}
return;
} if(dx > )
incx = ;
else if(dx == )
incx = ;
else
incx = -; if(dy > )
incy = ;
else if(dy == )
incy = ;
else
incy = -; dx = ((X0>X1) ? (X0-X1) : (X1-X0));
dy = ((Y0>Y1) ? (Y0-Y1) : (Y1-Y0)); if(dx>dy) distance=dx;
else distance=dy; PointX = X0;
PointY = Y0;
for(i=;i<=distance+;i++)
{ Put_pixel(PointX,PointY,color);
xerr+=dx;
yerr+=dy;
if(xerr>distance)
{
xerr-=distance;
PointX+=incx;
}
if(yerr>distance)
{
yerr-=distance;
PointY+=incy;
}
}
}
/*---------------------------------------------------------------------------
绘制矩形框
输入参数:矩形的起始位置left,top
矩形的结束位置right,bottom
矩形框的颜色color
-----------------------------------------------------------------------------*/
void Rectangle(unsigned int left,
unsigned int top,
unsigned int right,
unsigned int bottom,
unsigned int color)
{
Line(left,top,right,top,color);
Line(left,top,left,bottom,color);
Line(right,top,right,bottom,color);
Line(left,bottom,right,bottom,color);
}

9325tp.c

 #include<reg52.h>
#include"9325tp.h" /////////////////////////////////////////////////////////////////////////////////
/* LCD color */
#define White 0xFFFF
#define Black 0x0000
#define Blue 0x001F
#define Red 0xF800
//#define Blue2 0x051F
//#define Magenta 0xF81F
//#define Green 0x07E0
//#define Cyan 0x7FFF
//#define Yellow 0xFFE0 unsigned char b_x,b_y,b_l,b_w;
unsigned char m_x,m_old_x,m_y,m_l,m_w,m_v,m_a,m_l_limit_p,m_r_limit_p;
unsigned char s_y,s_old_y,s_extre_part,s_v,s_a,s_u_limit_p,s_d_limit_p; unsigned char h_x,h_y,h_w;
unsigned char move_x_v,move_y_v,move_a;
unsigned char line_x,line_old_x,line_y,line_l,line_old_l;
//--------------------------------------------------------------
void init1()
{
b_x=,b_y=,b_l=,b_w=;
m_x=m_old_x=,m_y=,m_l=,m_w=,m_v=,m_a=,m_l_limit_p=,m_r_limit_p=b_x+b_w-m_l_limit_p;
s_y=s_old_y=,s_extre_part=,s_v=,s_a=,s_u_limit_p=b_y+,s_d_limit_p=b_y+b_l-;
}
//---------------------------Draw-------------------------------
void drawBig()
{
Line(b_x,b_y,b_x+b_w,b_y,Black);
Line(b_x,b_y+b_l,b_x+b_w,b_y+b_l,Black);
}//主梁画
void drawMiddle()
{
if(m_x!=m_old_x)Line(m_old_x,m_y,m_old_x,m_y+m_l,White);
Line(m_x,m_y,m_x,m_y+m_l,Blue);
if(m_x!=m_old_x)Line(m_old_x+m_w,m_y,m_old_x+m_w,m_y+m_l,White);
Line(m_x+m_w,m_y,m_x+m_w,m_y+m_l,Blue);
}//画横梁
void drawSmall()
{
if(s_old_y!=s_y || m_x!=m_old_x)Line(m_old_x-s_extre_part,s_old_y,m_old_x+m_w+s_extre_part,s_old_y,White);
Line(m_x-s_extre_part,s_y,m_x+m_w+s_extre_part,s_y,Red);
}//画小车
void draw1()
{
drawBig();//画主梁
drawMiddle();//画横梁
drawSmall();//画小车
}//总画函数
//----------------------------run-----------------------------
void runMiddle()
{
m_old_x=m_x;
m_x+=m_v;
if(m_x+m_w>=m_r_limit_p || m_x<=m_l_limit_p)m_v*=-;//达到警戒线
}//横梁移动
void runSmall()
{
s_old_y=s_y;
line_old_x=line_x;
s_y+=s_v;
line_x+=move_x_v;
if(s_y>=s_d_limit_p || s_y<=s_u_limit_p){
s_v*=-;
move_x_v*=-;
}//达到警戒线
}//小车移动
void run1()
{
runMiddle();
runSmall();
}//运动函数
///////////////////////////////////////////////////////////////////////////////// void init2()
{
h_x=,h_y=,h_w=;
move_x_v=s_v*/,move_y_v=,move_a=;
line_x=line_old_x=,line_y=h_y+,line_l=line_old_l=;
}
//-------------------------draw--------------------------------
void drawGou()
{
if(line_old_x!=line_x || line_old_l!=line_old_l){
Rectangle(line_old_x-,line_y+line_old_l,line_old_x+,line_y+line_old_l+,White);
Line(line_old_x,line_y+line_old_l+,line_old_x,line_y+line_old_l+,White);
Line(line_old_x,line_y+line_old_l+,line_old_x+,line_y+line_old_l+,White);
Line(line_old_x+,line_y+line_old_l+,line_old_x+,line_y+line_old_l+,White);
Line(line_old_x+,line_y+line_old_l+,line_old_x-,line_y+line_old_l+,White);
Line(line_old_x-,line_y+line_old_l+,line_old_x-,line_y+line_old_l+,White);
}
Rectangle(line_x-,line_y+line_l,line_x+,line_y+line_l+,Blue);
Line(line_x,line_y+line_l+,line_x,line_y+line_l+,Black);
Line(line_x,line_y+line_l+,line_x+,line_y+line_l+,Black);
Line(line_x+,line_y+line_l+,line_x+,line_y+line_l+,Black);
Line(line_x+,line_y+line_l+,line_x-,line_y+line_l+,Black);
Line(line_x-,line_y+line_l+,line_x-,line_y+line_l+,Black);
}//画勾,x,y为绳的下端,r为大小
void drawH()
{
Line(h_x,h_y,h_x+h_w,h_y,Black);
}//画横梁
void drawHuaKuai()
{
if(line_old_x!=line_x)Rectangle(line_old_x-*/,line_y-,line_old_x+*/,line_y,White);
Rectangle(line_x-*/,line_y-,line_x+*/,line_y,Blue);
}//画滑块,x,y为绳的上端,r为大小
void drawLine()
{
if(line_old_l!=line_l || line_x!=line_old_x)Line(line_old_x,line_y,line_old_x,line_y+line_old_l,White);
Line(line_x,line_y,line_x,line_y+line_l,Black);
}//画线
void draw2()
{
drawH();
drawHuaKuai();
drawLine();
drawGou();
}
//------------------------run-------------------------------
main()
{
ILI9325_Initial(); //初始化LCD
CLR_Screen(White); //用背景色清屏
init1();//初始化1画面
init2();//初始化2画面
while()
{
run1();
draw1();
draw2();
}
}
05-02 02:40