找回密码
 立即注册

QQ登录

只需一步,快速开始

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

无控制器12864lcd仿真小设计(汉字,图片,画直线,画圆)

[复制链接]
跳转到指定楼层
楼主
一个无控制器12864lcd小设计。包括显示ASCII字符,汉字,图片,画直线,画圆。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. #include<reg51.h>
  2. #include<intrins.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include "ziku.h"
  6. #include "bmp.h"
  7. //***************************        
  8. #define LCD P2;
  9. sbit RS=P3^0;
  10. sbit RW=P3^1;
  11. sbit EN=P3^2;
  12. sbit CS1=P3^4;
  13. sbit CS2=P3^3;
  14. //***************************
  15. sbit button1=P1^0;
  16. sbit button2=P1^1;
  17. sbit button3=P1^2;
  18. sbit button4=P1^3;
  19. sbit button5=P1^4;
  20. sbit button6=P1^5;
  21. //*****************************
  22. unsigned char code huan[]= // 欢
  23. {0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00,
  24. 0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00};
  25. unsigned char code ying[]= //迎
  26. {0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00,
  27. 0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00};
  28. //**************************
  29. /*void busy()
  30. {
  31.         P0=0x00;
  32.         RS=0;
  33.         RW=1;
  34.         EN=1;
  35.         while(P2&0x80);
  36.         EN=0;
  37. } */
  38. //**************************
  39. void delay(unsigned int t)
  40. {
  41.         unsigned int t1;
  42.         for(t1=0;t1<t;t1++);
  43. }
  44. //**********************
  45. void write_cmd(unsigned char com)//写指令
  46. {
  47.         //busy();
  48.         RS=0;RW=0;EN=0;
  49.         P2=com;
  50.         delay(5);
  51.         EN=1;
  52.         delay(5);
  53.         EN=0;
  54. }
  55. //***************************
  56. void write_dat(unsigned char dat)//写数据
  57. {
  58.         //busy();
  59.         RS=1;RW=0;EN=0;
  60.         P2=dat;
  61.         delay(5);
  62.         EN=1;
  63.         delay(5);
  64.         EN=0;
  65. }
  66. //*****************
  67. void set_page(unsigned char page)//设置页,12864LCD共有8页,每页有8行点阵点。
  68. {
  69.         page=0xb8|page;  //首页地址为0XB8
  70.         write_cmd(page);                  //page取值范围为0~7,表示第1到8页
  71. }
  72. //***************
  73. void set_line(unsigned char line)//设置显示的起始行,共有0——63行,一般从0行开始显示
  74. {
  75.         line=0xc0|line;  //起始行地址0XC0
  76.         write_cmd(line);        //line取值范围为0~63,表示第1到64行
  77. }
  78. //***********************
  79. void set_column(unsigned char column)//设置显示的列
  80. {
  81.         column=0x40|column;         //列的首地址为0X40,
  82.         write_cmd(column);                 //column的取值范围为0~63,共64列
  83. }
  84. //***********************
  85. void set_onoff(unsigned char onoff)//设置显示开关,onoff取值为0或1
  86. {
  87.         onoff|=0x3e;//0X3E是关显示,0X3F是开显示
  88.         write_cmd(onoff);//所以若onoff为0,则表示关显示,onoff为1,则表示开显示
  89. }
  90. //********************
  91. void select_screen(unsigned char screen)
  92. {
  93.         switch(screen)
  94.         {
  95.                 case 0:CS1=0;CS2=0;break;         //全屏
  96.                 case 1:CS1=0;CS2=1;break;          //左半屏
  97.                 case 2:CS1=1;CS2=0;break;          //右半屏
  98.                 default:break;
  99.         }
  100. }        
  101. //***********************
  102. void intialize()        //12864初始化
  103. {
  104.         //busy();        
  105.         RS=0;RW=0;EN=0;P2=0xff;
  106.         write_cmd(0x30);        //基本指令集*
  107.         write_cmd(0x0c);        //开显示关光标
  108.         write_cmd(0x01);        //清屏
  109. }
  110. //**********************
  111. void clear()
  112. {
  113.         unsigned char i,j;
  114.         select_screen(0);//先选屏
  115.         for(i=0;i<8;i++)//控制页数0——7,共8页
  116.         {
  117.                 set_page(i);//设置页
  118.                 set_column(0); //设置列,每页都从第1列开始,共64列
  119.                 for(j=0;j<64;j++)//控制列数0——63,共64列
  120.                 write_dat(0x00);//写入0,列地址指针会自动加1
  121.         }
  122. }
  123. //*********************************
  124. void writechar(unsigned char screen,unsigned char page,unsigned char column,unsigned char *p)//显示一个汉字(16*16)共8列4行(1~2屏,1~4行,1~4列)
  125. {
  126.         unsigned char i;
  127.         select_screen(screen);
  128.         set_page(2*(page-1));
  129.         set_column((column-1)*16);
  130.         for(i=0;i<16;i++)
  131.                 write_dat(p[i]);
  132.         set_page(2*(page-1)+1);
  133.         set_column((column-1)*16);
  134.         for(i=0;i<16;i++)
  135.                 write_dat(p[i+16]);
  136. }
  137. //*****************************
  138. void writeasc(unsigned char screen,unsigned char page,unsigned char column,unsigned char asc)//显示一个ASC(8*16)共16列8行(1~2屏,1~4行,1~8列)
  139. {
  140.         unsigned char i;
  141.         select_screen(screen);
  142.         set_page(2*(page-1));
  143.         set_column(8*(column-1));
  144.         for(i=0;i<8;i++)
  145.                 write_dat(asctab[(asc-32)*16+i]);
  146.         set_page(2*(page-1)+1);
  147.         set_column(8*(column-1));
  148.         for(i=0;i<8;i++)
  149.                 write_dat(asctab[(asc-32)*16+8+i]);
  150. }
  151. //*****************************
  152. void writestring(unsigned char screen,unsigned char page,unsigned char column,unsigned char *string)
  153. {        
  154.         unsigned char i=0;
  155.         while(screen==1&&string[i]!='\0'&&i<(17-column))
  156.     {
  157.         if(i<8){writeasc(screen,page,column+i,string[i]);i++;}
  158.         else{writeasc(screen+1,page,column+i,string[i]);i++;}
  159.         }                                                         
  160.         while(screen==2&&string[i]!='\0'&&i<(17-column))
  161.     {writeasc(screen,page,column+i,string[i]);i++;}        
  162. }
  163. //*****************************
  164. void drawbmp(unsigned char page,unsigned char column,unsigned char *pic)
  165. {
  166.         unsigned char j=0,i=0;
  167.         CS1=0;CS2=1;
  168.         for(j=0;j<8;j++)
  169.         {
  170.                 write_cmd(0xb8+page-1+j);
  171.                 write_cmd(0x40+column-1);
  172.                 for(i=0;i<64;i++)
  173.                         write_dat(pic[128*j+i]);
  174.         }
  175.         CS1=1;CS2=0;
  176.         for(j=0;j<8;j++)
  177.         {
  178.                 write_cmd(0xb8+page-1+j);
  179.                 write_cmd(0x40+column-1);
  180.                 for(i=64;i<128;i++)
  181.                         write_dat(pic[128*j+i]);
  182.         }
  183. }  
  184. //***********************
  185. unsigned char ReadData()
  186. {
  187.         unsigned char dsp_data;
  188.         P2=0xff;
  189.         RW=1;
  190.         RS=1;
  191.         EN=1;
  192.         delay(5);
  193.         EN=0;
  194.         delay(5);
  195.         EN=1;
  196.         delay(5);
  197.         dsp_data=P2;
  198.         delay(5);
  199.         EN=0;
  200.         return(dsp_data);
  201. }
  202. void Pixel(unsigned char X,unsigned char Y)//横纵坐标
  203. {
  204. unsigned char DX = (Y >> 3);
  205. unsigned char BX = Y - (DX << 3);
  206. unsigned char TempData = 0;
  207. if (X > 63)   {    CS1=1;CS2=0;   X -= 64;  }  
  208. else   {    CS1=0;CS2=1;  }
  209. write_cmd(0xb8+DX);
  210. write_cmd(0x40+X);
  211. TempData = ReadData();
  212. TempData |= (1 << BX); //TempData |= (1 << BX);TempData &= ~(1<<BX);TempData ^= (1 << BX);
  213. write_cmd(0xb8+DX);
  214. write_cmd(0x40+X);
  215. write_dat(TempData);
  216. }

  217. //*********************
  218. void drawline(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)//起点终点横纵坐标
  219. {
  220.         char dx,dy;
  221.         char inc_x,inc_y;
  222.         int xerr = 0,yerr = 0;        //初始化变量
  223.         unsigned char i,ds;
  224.         dx = x2 - x1;        //计算坐标增量
  225.         dy = y2 - y1;
  226.         if(dx > 0)
  227.                 inc_x = 1;        //设置单步方向
  228.         else
  229.         {
  230.                 if(dx == 0)
  231.                         {inc_x = 0;}        //垂直线
  232.                 else
  233.                         {inc_x = -1; dx = -dx;}
  234.         }
  235.         if(dy > 0)
  236.                 inc_y = 1;        //设置单步方向
  237.         else
  238.         {
  239.                 if(dy == 0)
  240.                          {inc_y = 0;}        //水平线
  241.                 else
  242.                         {inc_y = -1; dy = -dy;}
  243.         }        
  244.         if(dx > dy)
  245.                 ds = dx;        //选取基本增量坐标轴
  246.         else
  247.                 ds = dy;        
  248.         for(i = 0; i <= ds+1; i++)        //画线输出
  249.         {
  250.                 Pixel(x1, y1);        //画点
  251.                 xerr += dx;
  252.                 yerr += dy;
  253.                 if(xerr > ds)
  254.                         {xerr -= ds;x1 += inc_x;}
  255.                 if(yerr > ds)
  256.                         {yerr -= ds;y1 += inc_y;}
  257.         }
  258. }
  259. //***********************
  260. void drawcircle(unsigned char x0 , unsigned char y0 , unsigned char r)//横纵坐标,半径
  261. {
  262.         char a , b;
  263.         char di;
  264.         if(r > 31 || r == 0)//画不下
  265.         return;
  266.         a = 0;
  267.         b = r;
  268.         di = 3 - 2 * r;//下一个点
  269.         while(a <= b)
  270.         {
  271.                 Pixel( x0 - b , y0 - a);
  272.                 Pixel( x0 + b , y0 - a);
  273.                 Pixel( x0 - a , y0 + b);
  274.                 Pixel( x0 - b , y0 - a);
  275.                 Pixel( x0 - a , y0 - b);
  276.                 Pixel( x0 + b , y0 + a);
  277.                 Pixel( x0 + a , y0 - b);
  278.                 Pixel( x0 + a , y0 + b);
  279.                 Pixel( x0 - b , y0 + a);        
  280.                 a ++;
  281.         //***bresenham********/
  282.                 if(di < 0)
  283.                         di += 4 * a + 6;
  284.                 else
  285.                 {
  286.                 di += 10 + 4 * (a - b);                        
  287.                 }
  288.                 Pixel( x0 + a , y0 + b);
  289.         }
  290. }


  291. //*************************
  292. void main()
  293. {
  294.         intialize();
  295.         clear();
  296.         while(1)
  297.         {
  298.                 if(button1==0)
  299.                 {
  300.                         delay(3000);
  301.                         if(button1==0)
  302.                         {
  303.                                 clear();writechar(1,4,4,huan);writechar(2,1,1,ying);
  304.                                 while(!button1);
  305.                         }        
  306.                 }
  307.                 if(button2==0)
  308.                 {
  309.                         delay(3000);
  310.                         if(button2==0)
  311.                         {
  312.                                 clear();writestring(1,1,1,"A Beast of Prey");
  313.                                 while(!button2);
  314.                         }        
  315.                 }
  316.                 if(button3==0)
  317.                 {
  318.                         delay(3000);
  319. ……………………

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

所有资料51hei提供下载:
12864.rar (75.6 KB, 下载次数: 44)




评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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