找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5328|回复: 1
打印 上一主题 下一主题
收起左侧

51单片机制作的推箱子游戏 PG160128A液晶显示 带源码与仿真(共9关)

[复制链接]
跳转到指定楼层
楼主
推箱子游戏设计资料


PG160128A液晶屏初始化源代码(51单片机):
  1. #define uchar unsigned char
  2. #define LCD_CHAR 0x14

  3. sbit cd = P3^0;                /*通道选择*/
  4. sbit rd = P3^1;                /*读操作信号*/
  5. sbit wr = P3^2;                /*写操作信号*/
  6. sbit error0 = P1^5;  /*出错提示1*/
  7. sbit error1 = P1^6;  /*出错提示2*/
  8. sbit error2 = P1^7;  /*出错提示3*/

  9. /*读状态*/
  10. uchar read_state(){
  11.         uchar temp;
  12.         P2 = 0xff;
  13.         cd = 1;
  14.         rd = 0;
  15.         temp = P2;
  16.         rd = 1;
  17.         return(temp);
  18. }

  19. /*STA0指令读写状态,STA1数据读写状态 判断函数*/
  20. void lcd_enable(){
  21.         uchar i;
  22.         for(i = 10; i > 0; i--)
  23.                 if((read_state() & 0x03) == 0x03)
  24.                         break;
  25.         if(i==0)error0=1;/*若i==0,说明错误*/
  26.         //else error0=0;
  27. }

  28. /*STA2数据自动读状态 判断函数*/
  29. void atrd_enable(){
  30.         uchar i;
  31.         for(i = 10; i > 0; i--)
  32.                 if((read_state() & 0x04) == 0x04)
  33.                         break;
  34.         if(i==0)error1=1;/*若i==0,说明错误*/
  35.         //else error1=0;
  36. }

  37. /*STA3数据自动写状态 判断函数*/
  38. void atwr_enable(){
  39.         uchar i;
  40.         for(i = 10; i > 0; i--)
  41.                 if((read_state() & 0x08) == 0x08)
  42.                         break;
  43.         if(i==0)error2=1;/*若i==0,说明错误*/
  44.         //else error2=0;
  45. }

  46. /*写无参数函数*/
  47. void write_cmd0(uchar cmd){
  48.         lcd_enable();
  49.         cd=1;
  50.         P2=cmd;
  51.         wr=0;
  52.         wr=1;
  53. }

  54. /*写单参数函数*/
  55. void write_cmd1(uchar data1, uchar cmd){
  56.         lcd_enable();
  57.         cd=0;
  58.         P2=data1;
  59.         wr=0;
  60.         wr=1;
  61.         lcd_enable();
  62.         cd=1;
  63.         P2=cmd;
  64.         wr=0;
  65.         wr=1;
  66. }

  67. /*写双参数函数*/
  68. void write_cmd2(uchar data1, uchar data2, uchar cmd){
  69.         lcd_enable();
  70.         cd=0;
  71.         P2=data1;
  72.         wr=0;
  73.         wr=1;
  74.         lcd_enable();
  75.         cd=0;
  76.         P2=data2;
  77.         wr=0;
  78.         wr=1;       
  79.         lcd_enable();
  80.         cd=1;
  81.         P2=cmd;
  82.         wr=0;
  83.         wr=1;       
  84. }

  85. /*写数据函数*/
  86. void write_data(uchar data0){
  87.         lcd_enable();
  88.         cd=1;
  89.         P2=data0;
  90.         wr=0;
  91.         wr=1;       
  92. }

  93. /*读数据函数*/
  94. uchar read_data(){
  95.         char temp;
  96.         lcd_enable();
  97.         cd = 0;
  98.         P2 = 0xff;
  99.         rd = 0;
  100.         temp = P2;
  101.         rd = 1;
  102.         return(temp);        /*若返回0,可能错误*/
  103. }

  104. /*自动写开始*/
  105. void auto_write(){
  106.         write_cmd0(AUT_WR);
  107. }

  108. /*自动读开始*/
  109. void auto_read(){
  110.         write_cmd0(AUT_RD);
  111. }

  112. /*自动写结束*/
  113. void atwr_stop(){
  114.         write_cmd0(AUT_WO);
  115. }

  116. /*自动读结束*/
  117. void atrd_stop(){
  118.         write_cmd0(AUT_RO);
  119. }

  120. /*数据一次写函数*/
  121. void write_one(uchar data1, char way){
  122.         atwr_enable();
  123.         auto_write();
  124.         write_cmd1(data1,way);
  125.         atwr_stop();
  126. }

  127. /*数据一次读函数*/
  128. uchar read_one(char way){
  129.         uchar temp;
  130.         atrd_enable();
  131.         auto_read();
  132.         write_cmd0(way);
  133.         temp = read_data();
  134.         atrd_stop();
  135.         return(temp);
  136. }

  137. /*设置当前显示位置函数x,y从0开始表示单位为字符*/
  138. void set_xy(uchar x, uchar y){
  139.         int temp;
  140.         temp = y * LCD_CHAR + x;
  141.         write_cmd2(temp&0xff,temp/0xff,ADR_POS);                       
  142. }

  143. void set_adr(uchar D1, uchar D2){
  144.         write_cmd2(D1,D2,ADR_POS);
  145. }

  146. /*设置光标指针 x,y从0开始*/
  147. void set_cur(char x, char y){
  148.         write_cmd2(x,y,CUR_POS);
  149. }

  150. /*CGRAM偏置地址设置函数*/
  151. void set_cgram(){
  152.         write_cmd2(0x01,0x00,CGR_POS);//0000,1100,0000,0000 0C00
  153. }

  154. /*液晶初始化函数(文本区首地址D1,文本区首地址D2, 文本区宽度,  图形区首地址D1, 图形区首地址D2, 图形区宽度,   光标形状,  显示方式,  显示开关)*/
  155. void lcd_init(uchar txtstpd1, uchar txtstpd2, uchar txtwid, uchar grhstpd1, uchar grhstpd2, uchar grhwid, uchar cur, uchar mod, uchar sw){
  156.         write_cmd2(txtstpd1,txtstpd2,TXT_STP);                                                                /*文本区首地址*/
  157.         write_cmd2(txtwid,0x00,TXT_WID);                        /*文本区宽度*/
  158.         write_cmd2(grhstpd1,grhstpd2,GRH_STP);                /*图形区首地址*/
  159.         write_cmd2(grhwid,0x00,GRH_WID);                        /*图形区宽度*/
  160.         write_cmd0(CUR_SHP | cur);                                        /*光标形状*/
  161.         write_cmd0(mod);                                                        /*显示方式*/
  162.         write_cmd0(DIS_SW | sw);                                        /*显示开关*/
  163. }
复制代码


部分源码预览:
  1. /*获取当前行,列*/
  2. /*设置当前行,列*/
  3. uchar g=0;
  4. void delay(int c){
  5.         int i, j;
  6.         for(i = 0; i < c; i++)
  7.                 for(j = 0; j < 1000; j++);
  8. }

  9. /*清屏 320 = (160/8) * (128/8) = 20 * 16 = 320*/
  10. void cls(void){
  11.         int i;
  12.         set_xy(0,0);
  13.         for(i = 0; i < 320; i++)
  14.                 write_one(0x94,INC_WR);
  15. }


  16. uchar curx,cury;        /*纪录当前人物所在位置*/

  17. uchar level_temp[8][8]={
  18.         0,0,0,0,0,0,0,0,
  19.         0,0,0,0,0,0,0,0,
  20.         0,0,0,0,0,0,0,0,
  21.         0,0,0,0,0,0,0,0,
  22.         0,0,0,0,0,0,0,0,
  23.         0,0,0,0,0,0,0,0,
  24.         0,0,0,0,0,0,0,0,
  25.         0,0,0,0,0,0,0,0,
  26. };


  27. void wirte_bg(void){
  28.         int i;
  29.         set_adr(0x50,0x01);
  30.         for(i = 0; i < 2560; i++)
  31.                 write_one(bg[i],INC_WR);
  32. }

  33. void wirte_cgrom(void){
  34.         int i;
  35. //        set_adr(0x50,0x01);
  36. //        for(i = 0; i < 2560; i++)
  37. //                write_one(0xff,INC_WR);
  38.         set_adr(0x00,0x0c);
  39.         /*自定义字符写入CGROM*/
  40.         for(i = 0; i < 848; i++)
  41.                 write_one(Lattice[i],INC_WR);
  42. }

  43. void start(void){
  44.         uchar i;
  45.         set_xy(0,0);
  46.         for(i=0;i<20;i++)
  47.                 write_one(0x95,INC_WR);
  48.         set_xy(0,15);
  49.         for(i=0;i<20;i++)
  50.                 write_one(0x95,INC_WR);
  51.         for(i=0;i<15;i++){
  52.                 set_xy(0,i);
  53.                 write_one(0x95,INC_WR);
  54.                 set_xy(19,i);
  55.                 write_one(0x95,INC_WR);
  56.         }
  57.         set_xy(18,1);
  58.         write_one(0x96,INC_WR);
  59.         set_xy(18,14);
  60.         write_one(0x97,INC_WR);
  61.         set_xy(1,1);
  62.         write_one(0x98,INC_WR);
  63.         set_xy(1,14);
  64.         write_one(0x99,INC_WR);

  65.         set_xy(7,6);
  66.         write_one(0xaa,INC_WR);
  67.         write_one(0xab,INC_WR);
  68.         write_one(0xae,INC_WR);
  69.         write_one(0xaf,INC_WR);
  70.         write_one(0xb2,INC_WR);
  71.         write_one(0xb3,INC_WR);
  72.         set_xy(7,7);
  73.         write_one(0xac,INC_WR);
  74.         write_one(0xad,INC_WR);
  75.         write_one(0xb0,INC_WR);
  76.         write_one(0xb1,INC_WR);
  77.         write_one(0xb4,INC_WR);
  78.         write_one(0xb5,INC_WR);

  79.         set_xy(6,8);
  80.         write_one(0x9a,INC_WR);
  81.         write_one(0x9b,INC_WR);
  82.         write_one(0x9e,INC_WR);
  83.         write_one(0x9f,INC_WR);
  84.         write_one(0xa2,INC_WR);
  85.         write_one(0xa3,INC_WR);
  86.         write_one(0xa6,INC_WR);
  87.         write_one(0xa7,INC_WR);
  88.         set_xy(6,9);
  89.         write_one(0x9c,INC_WR);
  90.         write_one(0x9d,INC_WR);
  91.         write_one(0xa0,INC_WR);
  92.         write_one(0xa1,INC_WR);
  93.         write_one(0xa4,INC_WR);
  94.         write_one(0xa5,INC_WR);
  95.         write_one(0xa8,INC_WR);
  96.         write_one(0xa9,INC_WR);
  97.         while(i){                        /*此while语句判断确定键超级技巧*/
  98.                 switch(P1&0x1f){
  99.                         case 0x0f:
  100.                                 i=0;
  101.                                 break;                               
  102.                 }
  103.         }
  104. }


  105. void guan(void){
  106.         /*推*/       
  107.         set_xy(16,0);
  108.         write_one(0xaa,INC_WR);
  109.         write_one(0xab,INC_WR);
  110.         set_xy(16,1);
  111.         write_one(0xac,INC_WR);
  112.         write_one(0xad,INC_WR);
  113.         /*箱*/
  114.         set_xy(16,2);
  115.         write_one(0xae,INC_WR);
  116.         write_one(0xaf,INC_WR);
  117.         set_xy(16,3);
  118.         write_one(0xb0,INC_WR);
  119.         write_one(0xb1,INC_WR);
  120.         /*子*/
  121.         set_xy(16,4);
  122.         write_one(0xb2,INC_WR);
  123.         write_one(0xb3,INC_WR);
  124.         set_xy(16,5);
  125.         write_one(0xb4,INC_WR);
  126.         write_one(0xb5,INC_WR);       
  127.         /*第*/
  128.         set_xy(16,8);
  129.         write_one(0xd2,INC_WR);
  130.         write_one(0xd3,INC_WR);       
  131.         set_xy(16,9);
  132.         write_one(0xd4,INC_WR);
  133.         write_one(0xd5,INC_WR);
  134.         /*几*/       
  135.         set_xy(16,10);
  136.         write_one(0xd6,INC_WR);
  137.         write_one(0xd6+2*(g+1),INC_WR);       
  138.         set_xy(16,11);
  139.         write_one(0xd7,INC_WR);
  140.         write_one(0xd7+2*(g+1),INC_WR);
  141.         /*关*/       
  142.         set_xy(16,12);
  143.         write_one(0xce,INC_WR);
  144.         write_one(0xcf,INC_WR);       
  145.         set_xy(16,13);
  146.         write_one(0xd0,INC_WR);
  147.         write_one(0xd1,INC_WR);
  148.         /*阿*/
  149.         set_xy(18,0);
  150.         write_one(0x9a,INC_WR);
  151.         write_one(0x9b,INC_WR);       
  152.         set_xy(18,1);
  153.         write_one(0x9c,INC_WR);
  154.         write_one(0x9d,INC_WR);
  155.         /*С*/       
  156.         set_xy(18,2);
  157.         write_one(0x9e,INC_WR);
  158.         write_one(0x9f,INC_WR);       
  159.         set_xy(18,3);
  160.         write_one(0xa0,INC_WR);
  161.         write_one(0xa1,INC_WR);
  162.         /*制*/       
  163.         set_xy(18,4);
  164.         write_one(0xa2,INC_WR);
  165.         write_one(0xa3,INC_WR);       
  166.         set_xy(18,5);
  167.         write_one(0xa4,INC_WR);
  168.         write_one(0xa5,INC_WR);
  169.         /*作*/       
  170.         set_xy(18,6);
  171.         write_one(0xa6,INC_WR);
  172.         write_one(0xa7,INC_WR);       
  173.         set_xy(18,7);
  174.         write_one(0xa8,INC_WR);
  175.         write_one(0xa9,INC_WR);
  176. }




  177. void printc(uchar i, uchar j, uchar c){
  178.         set_xy(i*2,j*2);
  179.         switch(c){       
  180.                 case 0:
  181.                            write_one(0x94,INC_WR);
  182.                            write_one(0x94,INC_WR);
  183.                            set_xy(i*2,j*2+1);
  184.                            write_one(0x94,INC_WR);
  185.                            write_one(0x94,INC_WR);
  186.                            break;                                          
  187.                 case 1:                /*人物1*/
  188.                            write_one(0x80,INC_WR);
  189.                            write_one(0x81,INC_WR);
  190.                            set_xy(i*2,j*2+1);
  191.                            write_one(0x82,INC_WR);
  192.                            write_one(0x83,INC_WR);
  193.                            break;
  194.                 case 2:                /*砖头2*/
  195.                            write_one(0x84,INC_WR);
  196.                            write_one(0x85,INC_WR);
  197.                            set_xy(i*2,j*2+1);
  198.                            write_one(0x86,INC_WR);
  199.                            write_one(0x87,INC_WR);
  200.                            break;
  201.                 case 3:                /*箱子3*/
  202.                            write_one(0x88,INC_WR);
  203.                            write_one(0x89,INC_WR);
  204.                            set_xy(i*2,j*2+1);
  205.                            write_one(0x8a,INC_WR);
  206.                            write_one(0x8b,INC_WR);
  207.                            break;
  208.                 case 4:                /*目的4*/
  209.                            write_one(0x8c,INC_WR);
  210.                            write_one(0x8d,INC_WR);
  211.                            set_xy(i*2,j*2+1);
  212.                            write_one(0x8e,INC_WR);
  213.                            write_one(0x8f,INC_WR);
  214.                            break;
  215.                 case 5:                /*成功5*/
  216.                            write_one(0x90,INC_WR);
  217.                            write_one(0x91,INC_WR);
  218.                            set_xy(i*2,j*2+1);
  219.                            write_one(0x92,INC_WR);
  220.                            write_one(0x93,INC_WR);
  221.                            break;
  222.         }
  223.        
  224. }



  225. void pushbox(){
  226.         uchar i,j;
  227.         /*根据level.h中的值进行输出单个字符点阵为16*16,显示8*8个字符*/
  228.         for(i = 0; i < 8; i++)
  229.                 for(j = 0; j < 8; j++){
  230.                         level_temp[i][j]=level[g][j][i];
  231.                         switch(level_temp[i][j]){       
  232.                                 case 0:
  233.                                            printc(i,j,0);
  234.                                            break;                                          
  235.                                 case 1:                /*人物1*/
  236.                                            curx=i;
  237.                                            cury=j;
  238.                                            printc(i,j,1);
  239.                                            break;
  240.                                 case 2:                /*砖头2*/
  241.                                            printc(i,j,2);
  242.                                            break;
  243.                                 case 3:                /*箱子3*/
  244.                                            printc(i,j,3);
  245.                                            break;
  246.                                 case 4:                /*目的4*/
  247.                                            printc(i,j,4);
  248.                                            break;
  249.                                 case 5:                /*成功5*/
  250.                                           



  251. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码


下载:
推箱子.zip (75.42 KB, 下载次数: 60)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩1
回复

使用道具 举报

沙发
ID:194355 发表于 2017-4-27 20:53 | 只看该作者
怎么修改某某制作?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表