找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机LCD12864显示波形源程序

[复制链接]
跳转到指定楼层
楼主
ID:811102 发表于 2020-9-20 10:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
12864显示波形可显示 正弦波  余弦波  方波   三角波  锯齿波

单片机源程序如下:
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include <math.h>
  4. //------------定义接口-------------//
  5. sbit RS=P2^6 ;
  6. sbit RW=P2^5 ;
  7. sbit E=P2^7;
  8. sbit PSB= P1^0;   //H=并口; L="串口";

  9. sbit  k1=P3^1;
  10. sbit  k2=P3^0;
  11. sbit  k3=P3^2;
  12. sbit  k4=P3^3;

  13. #define Lcd_Bus P0
  14. // P0 接 LCM
  15. #define uchar unsigned char
  16. #define FIRST_ADDR 0
  17. //定义字符/汉字显示起始位置
  18. /*------------------检查忙位-----------------------------*/
  19. void chk_busy()
  20. {
  21.     RS=0 ;
  22.     RW=1 ;
  23.     E=1 ;
  24.     Lcd_Bus=0xff ;
  25.     while((Lcd_Bus&0x80)==0x80);
  26.     E=0 ;
  27. }
  28. /*------------------延时子程序-----------------------------*/
  29. void delay(unsigned int t)
  30. {
  31.     unsigned int i,j ;
  32.     for(i=0;i<t;i++)
  33.     for(j=0;j<10;j++);
  34. }

  35. /*------------------写命令到LCD------------------------------*/
  36. void write_com(unsigned char cmdcode)
  37. {
  38.     chk_busy();
  39.     RS=0 ;
  40.     RW=0 ;
  41.     E=1 ;
  42.     Lcd_Bus=cmdcode ;
  43.     delay(5);
  44.     //------------------在数据写入的时候加入适当的延时
  45.     E=0 ;
  46.     delay(5);
  47. }

  48. /*-------------------写数据到LCD----------------------------*/
  49. void write_data(unsigned char Dispdata)
  50. {
  51.     chk_busy();
  52.     RS=1 ;
  53.     RW=0 ;
  54.     E=1 ;
  55.     Lcd_Bus=Dispdata ;
  56.     delay(5);
  57.     //------------------在数据写入的时候加入适当的延时
  58.     E=0 ;
  59.     delay(5);
  60. }
  61. /*------------------初始化LCD屏--------------------------*/
  62. void lcdreset()
  63. {
  64.     PSB = 1;
  65.     delay(2000);
  66.     write_com(0x30);
  67.     delay(10);
  68.     //选择基本指令集
  69.     write_com(0x30);
  70.     //选择8bit数据流
  71.     delay(5);
  72.     write_com(0x0c);
  73.     //开显示(无游标、不反白)
  74.     delay(10);
  75.     write_com(0x01);
  76.     //清除显示,并且设定地址指针为00H
  77.     delay(500);
  78.     write_com(0x06);
  79.     //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位
  80.     delay(0);
  81. }
  82. /*------------------显示字符串--------------------------*/
  83. void hzkdis(unsigned char code*s)
  84. {
  85.     while(*s>0)
  86.     {
  87.         write_data(*s);
  88.         s++;
  89.         delay(50);
  90.     }
  91. }
  92. /*------------------首屏显示--------------------------*/
  93. void ceshi()
  94. {
  95.     write_com(0x01);
  96.     //清除显示,并且设定地址指针为00H
  97.     delay(5);
  98.    
  99.     write_com(0x80);
  100.     //第一行(如果是地址是:80H,即LCD的第一行的第一个位置显示)
  101.     hzkdis("芜湖职业技术学院");
  102.    
  103.     write_com(0x90);
  104.     //第二行(如果是地址是:90H,即LCD的第二行的第一个位置显示)
  105.     hzkdis("信息工程学院");
  106.    
  107.     write_com(0x88);
  108.     //第三行(如果是地址是:88H,即LCD的第二行的第一个位置显示)
  109.     hzkdis("电子信息工程");
  110.    
  111.     write_com(0x98);
  112.     //第四行(如果是地址是:98H,即LCD的第二行的第一个位置显示)
  113.     hzkdis("李胡兵测试程序");
  114. }
  115. //------------------清整个GDRAM空间----------------------------
  116. void clrgdram()
  117. {
  118.     unsigned char x,y ;
  119.     for(y=0;y<64;y++)
  120.     for(x=0;x<16;x++)
  121.     {
  122.         write_com(0x34);
  123.         write_com(y+0x80);
  124.         //行地址
  125.         write_com(x+0x80);
  126.         //列地址
  127.         write_com(0x30);
  128.         write_data(0x00);
  129.         write_data(0x00);
  130.     }
  131. }
  132. //------------------------------------------------------------
  133. void clrscreen()
  134. {
  135.     write_com(0x01);
  136.     delay(10);
  137. }
  138. unsigned char ReadByte(void)
  139. {
  140.     unsigned char byReturnValue ;
  141.     chk_busy();
  142.     Lcd_Bus=0xff ;
  143.     RS=1 ;
  144.     RW=1 ;
  145.     E=0 ;
  146.     E=1 ;
  147.     byReturnValue=Lcd_Bus ;
  148.     E=0 ;
  149.    
  150.     return byReturnValue ;
  151. }

  152. /*增加画点子程序
  153. 函数功能:在坐标为(x,y)点画一个点
  154. 参数意义
  155. X:12864屏幕的横坐标,范围是0到128(从左到右)
  156. Y:12864的纵坐标,范围是0到64(从上到下)
  157. Color:为1的时候表示为黑点
  158. */
  159. void DrawPoint(unsigned char X,unsigned char Y,unsigned char Color)
  160. {
  161.     unsigned char Row,Tier,Tier_bit ;
  162.     unsigned char ReadOldH,ReadOldL ;
  163.     write_com(0x34);
  164.     write_com(0x36);
  165.     Tier=X>>4 ;
  166.     Tier_bit=X&0x0f ;
  167.     if(Y<32)
  168.     {
  169.         Row=Y ;
  170.     }
  171.     else
  172.     {
  173.         Row=Y-32 ;
  174.         Tier+=8 ;
  175.     }
  176.     write_com(Row+0x80);
  177.     write_com(Tier+0x80);
  178.     ReadByte();
  179.     ReadOldH=ReadByte();
  180.     ReadOldL=ReadByte();
  181.     write_com(Row+0x80);
  182.     write_com(Tier+0x80);
  183.     if(Tier_bit<8)
  184.     {
  185.         switch(Color)
  186.         {
  187.             case 0 :
  188.             ReadOldH&=(~(0x01<<(7-Tier_bit)));
  189.             break ;
  190.             case 1 :
  191.             ReadOldH|=(0x01<<(7-Tier_bit));
  192.             break ;
  193.             case 2 :
  194.             ReadOldH^=(0x01<<(7-Tier_bit));
  195.             break ;
  196.             default :
  197.             break ;
  198.         }
  199.         write_data(ReadOldH);
  200.         write_data(ReadOldL);
  201.     }
  202.     else
  203.     {
  204.         switch(Color)
  205.         {
  206.             case 0 :
  207.             ReadOldL&=(~(0x01<<(15-Tier_bit)));
  208.             break ;
  209.             case 1 :
  210.             ReadOldL|=(0x01<<(15-Tier_bit));
  211.             break ;
  212.             case 2 :
  213.             ReadOldL^=(0x01<<(15-Tier_bit));
  214.             break ;
  215.             default :
  216.             break ;
  217.         }
  218.         write_data(ReadOldH);
  219.         write_data(ReadOldL);
  220.     }
  221.     write_com(0x30);
  222. }
  223. void main(void)
  224. {
  225.     uchar i,j,colour=1,T=0;
  226.     RW=0 ;
  227.     lcdreset();
  228.     ceshi();
  229.     clrgdram();
  230.     delay(20);
  231.     clrscreen();
  232. while(1)
  233. {
  234.         if(!k1)
  235.         {
  236.             for(i=0;i<128;i++)
  237.             {
  238.                  j=32-32*sin(2*i*3.14/(64+T));
  239.                  DrawPoint(i,j,colour);
  240.             }
  241.             delay(5);
  242.         }
  243.         if(!k1)
  244.         {
  245.                 for(j=0;j<64;j++)
  246.                 {
  247.                            i=64;
  248.                  DrawPoint(i,j,colour);
  249.                 }
  250.                 for(i=0;i<128;i++)
  251.                 {
  252.                          j=32;
  253.                  DrawPoint(i,j,colour);       
  254.                 }
  255.                 delay(5000);
  256.         }
  257.         if(!k2)
  258.         {
  259.                 for(i=0;i<128;i++)
  260.             {
  261.                  j=32-32*sin(2*i*3.14/(64+T/2));
  262.                  DrawPoint(i,j,colour);
  263.             }
  264.         }


  265. }
  266.     while(1);
  267. }
复制代码

hex文件51hei提供下载:
12864显示波形.zip (41.42 KB, 下载次数: 30)


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

使用道具 举报

沙发
ID:328014 发表于 2020-9-20 22:13 | 只看该作者
能分享下原理图吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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