[51单片机] TFT2.4彩屏3 [自制动画效果-滑块+吊钩]

 

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

 1 #ifndef __ILI9325_H__
 2 #define __ILI9325_H__
 3 
 4 void ILI9325_Initial(void);
 5 void Write_Cmd_Data(unsigned char x, unsigned int y);
 6 void Write_Cmd(unsigned char DH,unsigned char DL);
 7 void Write_Data(unsigned char DH,unsigned char DL);
 8 void delayms(unsigned int tt);
 9 void  Write_Data_U16(unsigned int y);
10 static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1);
11 void CLR_Screen(unsigned int bColor);
12 void Put_pixel(unsigned char x,unsigned char y,unsigned int color);
13 void Line(unsigned int X0,
14         unsigned int Y0,
15         unsigned int X1,
16         unsigned int Y1,
17             unsigned int color);
18 void Rectangle(unsigned int left,
19                 unsigned int top,
20                 unsigned int right,
21                 unsigned int bottom,
22                 unsigned int color);                
23 #endif
9325tp.h
  1 /*----------------------------------------------------------------
  2 320x240彩屏液晶驱动程序
  3 ----------------------------------------------------------------*/
  4 #include"9325tp.h"
  5 #include"reg52.h"
  6 /*----------------------------------------------------------------
  7 全局变量
  8 ----------------------------------------------------------------*/
  9 #define WINDOW_XADDR_START    0x0050 // Horizontal Start Address Set
 10 #define WINDOW_XADDR_END    0x0051 // Horizontal End Address Set
 11 #define WINDOW_YADDR_START    0x0052 // Vertical Start Address Set
 12 #define WINDOW_YADDR_END    0x0053 // Vertical End Address Set
 13 #define GRAM_XADDR            0x0020 // GRAM Horizontal Address Set
 14 #define GRAM_YADDR            0x0021 // GRAM Vertical Address Set
 15 #define GRAMWR                 0x0022 // memory write
 16 
 17 #define DataPort P0                //数据口使用DataPort
 18 
 19 /*----------------------------------------------------------------
 20 定义硬件端口
 21 ----------------------------------------------------------------*/
 22 sbit CS=P2^2;        //片选
 23 sbit RES=P2^1;        //复位
 24 sbit RS=P2^4;        //数据/命令选择
 25 sbit RW=P2^5;
 26 /*----------------------------------------------------------------
 27 清屏函数
 28 输入参数:bColor 清屏所使用的背景色
 29 ----------------------------------------------------------------*/
 30 void CLR_Screen(unsigned int bColor)
 31 {
 32     unsigned int i,j;
 33     LCD_SetPos(0,240,0,320);//320x240
 34     for (i=0;i<320;i++)
 35     {
 36         
 37         for (j=0;j<240;j++)
 38             Write_Data_U16(bColor);
 39         
 40     }
 41 }
 42 /*----------------------------------------------------------------
 43 写命令、写数据
 44 输入参数:x 需要输入的命令 16位
 45 y 需要输入的数据 16位
 46 ----------------------------------------------------------------*/
 47 void  Write_Cmd_Data (unsigned char x,unsigned int y)
 48 {
 49     unsigned char m,n;
 50     m=y>>8;
 51     n=y;
 52     Write_Cmd(0x00,x);
 53     Write_Data(m,n);
 54     
 55 }
 56 
 57 /*----------------------------------------------------------------
 58 写16位数据
 59 ----------------------------------------------------------------*/
 60 void  Write_Data_U16(unsigned int y)
 61 {
 62     unsigned char m,n;
 63     m=y>>8;
 64     n=y;
 65     Write_Data(m,n);
 66     
 67 }
 68 /*----------------------------------------------------------------
 69 写命令
 70 ----------------------------------------------------------------*/
 71 
 72 void Write_Cmd(unsigned char DH,unsigned char DL)
 73 {
 74     CS=0;
 75     RS=0;
 76     
 77     DataPort=DH;
 78     RW=0;
 79     RW=1;
 80     
 81     DataPort=DL;
 82     
 83     RW=0;
 84     RW=1;
 85     CS=1;
 86 }
 87 
 88 /*----------------------------------------------------------------
 89 写数据 双8位
 90 ----------------------------------------------------------------*/
 91 void Write_Data(unsigned char DH,unsigned char DL)
 92 {
 93     
 94     CS=0;
 95     
 96     RS=1;
 97     DataPort=DH;
 98     RW=0;
 99     RW=1;
100     
101     DataPort=DL;    
102     RW=0;
103     RW=1;
104     CS=1;
105 }
106 /*----------------------------------------------------------------
107 延时函数
108 ----------------------------------------------------------------*/
109 void delayms(unsigned int count)
110 {
111     int i,j;                                                                                
112     for(i=0;i<count;i++)                                                                    
113     {
114         for(j=0;j<260;j++);
115     }                                                                                     
116 }
117 /*----------------------------------------------------------------
118 液晶初始化
119 ----------------------------------------------------------------*/
120 void ILI9325_Initial(void)
121 { 
122     CS=1;
123     delayms(5);
124     RES=0;
125     delayms(5);
126     RES=1;
127     delayms(50);
128     Write_Cmd_Data(0x0001,0x0100); 
129     Write_Cmd_Data(0x0002,0x0700); 
130     Write_Cmd_Data(0x0003,0x1030); 
131     Write_Cmd_Data(0x0004,0x0000); 
132     Write_Cmd_Data(0x0008,0x0207);  
133     Write_Cmd_Data(0x0009,0x0000);
134     Write_Cmd_Data(0x000A,0x0000); 
135     Write_Cmd_Data(0x000C,0x0000); 
136     Write_Cmd_Data(0x000D,0x0000);
137     Write_Cmd_Data(0x000F,0x0000);
138     //power on sequence VGHVGL
139     Write_Cmd_Data(0x0010,0x0000);   
140     Write_Cmd_Data(0x0011,0x0007);  
141     Write_Cmd_Data(0x0012,0x0000);  
142     Write_Cmd_Data(0x0013,0x0000); 
143     //vgh 
144     Write_Cmd_Data(0x0010,0x1290);   
145     Write_Cmd_Data(0x0011,0x0227);
146     //delayms(100);
147     //vregiout 
148     Write_Cmd_Data(0x0012,0x001d); //0x001b
149     //delayms(100); 
150     //vom amplitude
151     Write_Cmd_Data(0x0013,0x1500);
152     //delayms(100); 
153     //vom H
154     Write_Cmd_Data(0x0029,0x0018); 
155     Write_Cmd_Data(0x002B,0x000D); 
156     
157     //gamma
158     Write_Cmd_Data(0x0030,0x0004);
159     Write_Cmd_Data(0x0031,0x0307);
160     Write_Cmd_Data(0x0032,0x0002);// 0006
161     Write_Cmd_Data(0x0035,0x0206);
162     Write_Cmd_Data(0x0036,0x0408);
163     Write_Cmd_Data(0x0037,0x0507); 
164     Write_Cmd_Data(0x0038,0x0204);//0200
165     Write_Cmd_Data(0x0039,0x0707); 
166     Write_Cmd_Data(0x003C,0x0405);// 0504
167     Write_Cmd_Data(0x003D,0x0F02); 
168     //ram
169     Write_Cmd_Data(0x0050,0x0000); 
170     Write_Cmd_Data(0x0051,0x00EF);
171     Write_Cmd_Data(0x0052,0x0000); 
172     Write_Cmd_Data(0x0053,0x013F);  
173     Write_Cmd_Data(0x0060,0xA700); 
174     Write_Cmd_Data(0x0061,0x0001); 
175     Write_Cmd_Data(0x006A,0x0000); 
176     //
177     Write_Cmd_Data(0x0080,0x0000); 
178     Write_Cmd_Data(0x0081,0x0000); 
179     Write_Cmd_Data(0x0082,0x0000); 
180     Write_Cmd_Data(0x0083,0x0000); 
181     Write_Cmd_Data(0x0084,0x0000); 
182     Write_Cmd_Data(0x0085,0x0000); 
183     //
184     Write_Cmd_Data(0x0090,0x0010); 
185     Write_Cmd_Data(0x0092,0x0600); 
186     Write_Cmd_Data(0x0093,0x0003); 
187     Write_Cmd_Data(0x0095,0x0110); 
188     Write_Cmd_Data(0x0097,0x0000); 
189     Write_Cmd_Data(0x0098,0x0000);
190     Write_Cmd_Data(0x0007,0x0133);
191     
192     
193     
194     //    Write_Cmd_Data(0x0022);//        
195 }
196 /*----------------------------------------------------------------
197 画点
198 输入参数:x,y 需要画点坐标
199 color 点的颜色
200 ----------------------------------------------------------------*/
201 void Put_pixel(unsigned char x,unsigned char y,unsigned int color)
202 {
203     LCD_SetPos(x,x,y,y);
204     Write_Data_U16(color);
205 }
206 /*----------------------------------------------------------------
207 设置坐标
208 ----------------------------------------------------------------*/
209 static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
210 {
211     Write_Cmd_Data(WINDOW_XADDR_START,x0);
212     Write_Cmd_Data(WINDOW_XADDR_END,x1);
213     Write_Cmd_Data(WINDOW_YADDR_START,y0);
214     Write_Cmd_Data(WINDOW_YADDR_END,y1);
215     Write_Cmd_Data(GRAM_XADDR,x0);
216     Write_Cmd_Data(GRAM_YADDR,y0);
217     Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
218 }
219 /*----------------------------------------------------------------
220 在屏幕上画线
221 输入参数:  起始坐标X0,Y0,终止坐标X1,Y1
222 color 线颜色
223 ----------------------------------------------------------------*/ 
224 void Line(unsigned int X0,
225           unsigned int Y0,
226           unsigned int X1,
227           unsigned int Y1,
228           unsigned int color)
229 {
230     int dx = X1 - X0;
231     int dy = Y1 - Y0;
232     int P  = 2 * dy - dx;
233     int dobDy = 2 * dy;
234     int dobD = 2 * (dy - dx);
235     int PointX = 0,PointY = 0;
236     int incx = 0,incy = 0;
237     int distance = 0,xerr = 0,yerr = 0;
238     unsigned int i = 0;
239     
240     if(dx == 0)        //k=1斜率为1
241     {
242         PointX = X0;
243         if(Y0 < Y1)
244         {
245             PointY = Y0;
246         }
247         else
248         {
249             PointY = Y1;
250         }
251         for(i = 0;i <= ((Y0<Y1) ? (Y1-Y0) : (Y0-Y1));i++)
252         {
253             
254             Put_pixel(PointX,PointY,color);
255             PointY++;
256         }
257         return;
258     }
259     if(dy == 0)        //k=0斜率为0
260     {
261         PointY = Y0;
262         if(X0 < X1)
263         {
264             PointX = X0;
265         }
266         else
267         {
268             PointX = X1;
269         }
270         for(i = 0;i <= ((X0<X1) ? (X1-X0) : (X0-X1));i++)
271         {
272             
273             Put_pixel(PointX,PointY,color);
274             PointX++;
275         }
276         return;
277     }
278     
279     if(dx > 0)
280         incx = 1;
281     else if(dx == 0)
282         incx = 0;
283     else
284         incx = -1;
285     
286     if(dy > 0)
287         incy = 1;
288     else if(dy == 0)
289         incy = 0;
290     else
291         incy = -1;
292     
293     dx = ((X0>X1) ? (X0-X1) : (X1-X0));
294     dy = ((Y0>Y1) ? (Y0-Y1) : (Y1-Y0));
295     
296     if(dx>dy) distance=dx;
297     else distance=dy;
298     
299     PointX = X0;
300     PointY = Y0;
301     for(i=0;i<=distance+1;i++)
302     {
303         
304         Put_pixel(PointX,PointY,color);
305         xerr+=dx;
306         yerr+=dy;
307         if(xerr>distance)
308         {
309             xerr-=distance;
310             PointX+=incx;
311         }
312         if(yerr>distance)
313         {
314             yerr-=distance;
315             PointY+=incy;
316         }
317     }
318 }
319 /*---------------------------------------------------------------------------
320 绘制矩形框
321 输入参数:矩形的起始位置left,top 
322 矩形的结束位置right,bottom 
323 矩形框的颜色color
324 -----------------------------------------------------------------------------*/
325 void Rectangle(unsigned int left,
326   unsigned int top,
327   unsigned int right,
328   unsigned int bottom,
329   unsigned int color)
330 {
331   Line(left,top,right,top,color);
332   Line(left,top,left,bottom,color);
333   Line(right,top,right,bottom,color);
334   Line(left,bottom,right,bottom,color);
335 }
9325tp.c
  1 #include<reg52.h>
  2 #include"9325tp.h"
  3 
  4 /////////////////////////////////////////////////////////////////////////////////
  5 /* LCD color */
  6 #define White          0xFFFF
  7 #define Black          0x0000
  8 #define Blue           0x001F
  9 #define Red            0xF800
 10 //#define Blue2          0x051F
 11 //#define Magenta        0xF81F
 12 //#define Green          0x07E0
 13 //#define Cyan           0x7FFF
 14 //#define Yellow         0xFFE0
 15 
 16 unsigned char b_x,b_y,b_l,b_w;
 17 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;
 18 unsigned char s_y,s_old_y,s_extre_part,s_v,s_a,s_u_limit_p,s_d_limit_p;
 19 
 20 unsigned char h_x,h_y,h_w;
 21 unsigned char move_x_v,move_y_v,move_a;
 22 unsigned char line_x,line_old_x,line_y,line_l,line_old_l;
 23 //--------------------------------------------------------------
 24 void init1()
 25 {
 26     b_x=10,b_y=20,b_l=80,b_w=220;
 27     m_x=m_old_x=40,m_y=10,m_l=100,m_w=70,m_v=5,m_a=0,m_l_limit_p=40,m_r_limit_p=b_x+b_w-m_l_limit_p;
 28     s_y=s_old_y=80,s_extre_part=10,s_v=1,s_a=0,s_u_limit_p=b_y+10,s_d_limit_p=b_y+b_l-10;
 29 }
 30 //---------------------------Draw-------------------------------
 31 void drawBig()
 32 {
 33     Line(b_x,b_y,b_x+b_w,b_y,Black);
 34     Line(b_x,b_y+b_l,b_x+b_w,b_y+b_l,Black);
 35 }//主梁画
 36 void drawMiddle()
 37 {
 38     if(m_x!=m_old_x)Line(m_old_x,m_y,m_old_x,m_y+m_l,White);
 39     Line(m_x,m_y,m_x,m_y+m_l,Blue);
 40     if(m_x!=m_old_x)Line(m_old_x+m_w,m_y,m_old_x+m_w,m_y+m_l,White);
 41     Line(m_x+m_w,m_y,m_x+m_w,m_y+m_l,Blue);
 42 }//画横梁
 43 void drawSmall()
 44 {
 45     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);
 46     Line(m_x-s_extre_part,s_y,m_x+m_w+s_extre_part,s_y,Red);
 47 }//画小车
 48 void draw1()
 49 {
 50     drawBig();//画主梁
 51     drawMiddle();//画横梁
 52     drawSmall();//画小车
 53 }//总画函数
 54 //----------------------------run-----------------------------
 55 void runMiddle()
 56 {
 57     m_old_x=m_x;
 58     m_x+=m_v;
 59     if(m_x+m_w>=m_r_limit_p || m_x<=m_l_limit_p)m_v*=-1;//达到警戒线
 60 }//横梁移动
 61 void runSmall()
 62 {
 63     s_old_y=s_y;
 64     line_old_x=line_x;
 65     s_y+=s_v;
 66     line_x+=move_x_v;
 67     if(s_y>=s_d_limit_p || s_y<=s_u_limit_p){
 68         s_v*=-1;
 69         move_x_v*=-1;
 70     }//达到警戒线
 71 }//小车移动
 72 void run1()
 73 {
 74     runMiddle();
 75     runSmall();
 76 }//运动函数
 77 /////////////////////////////////////////////////////////////////////////////////
 78 
 79 
 80 void init2()
 81 {
 82     h_x=10,h_y=180,h_w=220;
 83     move_x_v=s_v*11/4,move_y_v=2,move_a=0;
 84     line_x=line_old_x=160,line_y=h_y+3,line_l=line_old_l=50;
 85 }
 86 //-------------------------draw--------------------------------
 87 void drawGou()
 88 {
 89     if(line_old_x!=line_x || line_old_l!=line_old_l){
 90         Rectangle(line_old_x-3,line_y+line_old_l,line_old_x+3,line_y+line_old_l+6,White);
 91         Line(line_old_x,line_y+line_old_l+6,line_old_x,line_y+line_old_l+9,White);
 92         Line(line_old_x,line_y+line_old_l+9,line_old_x+6,line_y+line_old_l+9,White);
 93         Line(line_old_x+6,line_y+line_old_l+9,line_old_x+6,line_y+line_old_l+15,White);
 94         Line(line_old_x+6,line_y+line_old_l+15,line_old_x-6,line_y+line_old_l+15,White);
 95         Line(line_old_x-6,line_y+line_old_l+15,line_old_x-6,line_y+line_old_l+12,White);
 96     }
 97     Rectangle(line_x-3,line_y+line_l,line_x+3,line_y+line_l+6,Blue);
 98     Line(line_x,line_y+line_l+6,line_x,line_y+line_l+9,Black);
 99     Line(line_x,line_y+line_l+9,line_x+6,line_y+line_l+9,Black);
100     Line(line_x+6,line_y+line_l+9,line_x+6,line_y+line_l+15,Black);
101     Line(line_x+6,line_y+line_l+15,line_x-6,line_y+line_l+15,Black);
102     Line(line_x-6,line_y+line_l+15,line_x-6,line_y+line_l+12,Black);    
103 }//画勾,x,y为绳的下端,r为大小
104 void drawH()
105 {
106     Line(h_x,h_y,h_x+h_w,h_y,Black);
107 }//画横梁
108 void drawHuaKuai()
109 {
110     if(line_old_x!=line_x)Rectangle(line_old_x-6*3/4,line_y-6,line_old_x+6*3/4,line_y,White);
111     Rectangle(line_x-6*3/4,line_y-6,line_x+6*3/4,line_y,Blue);
112 }//画滑块,x,y为绳的上端,r为大小
113 void drawLine()
114 {
115     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);
116     Line(line_x,line_y,line_x,line_y+line_l,Black);
117 }//画线
118 void draw2()
119 {
120     drawH();
121     drawHuaKuai();
122     drawLine();
123     drawGou();
124 }
125 //------------------------run-------------------------------
126 main()
127 {
128     ILI9325_Initial(); //初始化LCD
129     CLR_Screen(White); //用背景色清屏
130     init1();//初始化1画面
131     init2();//初始化2画面
132     while(1)
133     {
134         run1();
135         draw1();
136         draw2();
137     }
138 }

 

posted @ 2014-04-06 21:40  beautifulzzzz  阅读(1647)  评论(0编辑  收藏  举报