找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stc89c51单片机和lcd12864显示sin函数图像 源程序

[复制链接]
跳转到指定楼层
楼主
这是我花了几天时间做的让stc89c51和lcd12864(带字库)的显示波形,
附件里面包含了原理图和单片机源代码。
做出来的图像坐标都很好。
源代码有注释,这是部分代码,具体的大家去下载吧;

制作出来的实物图如下:


单片机源程序如下:
  1. #include <reg51.h>
  2. #include <intrins.h>
  3. #include <math.h>

  4. #define uint unsigned int
  5. #define uchar unsigned char

  6. #define pi 3.1419526
  7. uchar f=16;
  8. uchar u=8;

  9. sbit RS=P2^6;
  10. sbit RW=P2^5;
  11. sbit PSB=P3^2;     //lcd串行还是并行选择端
  12. sbit RST=P3^4;     //lcd的复位端口
  13. sbit EN=P2^7;


  14. /*延时*/
  15. void delay(uint x)
  16. {
  17.     uint y;
  18.     for(;x>0;x--)
  19.       for(y=110;y>0;y--);
  20. }


  21. /*写入指令*/
  22. void write_com(uchar com)
  23. {

  24.   RS=0;
  25.   RW=0;
  26.   EN=0;
  27.   P0=com;
  28.   delay(1);
  29.   EN=1;
  30.   delay(3);
  31.   EN=0;
  32. }

  33. /*写入数据*/
  34. void write_data(uchar num)
  35. {

  36.   RS=1;
  37.   RW=0;
  38.   EN=0;
  39.   P0=num;
  40.   delay(1);
  41.   EN=1;
  42.   delay(3);
  43.   EN=0;
  44. }

  45. /*读取数据*/
  46. uchar Read_data()
  47. {
  48.    uchar read;
  49.    
  50.    RS=1;
  51.    RW=1;
  52.    EN=0;
  53.    delay(1);
  54.       EN=1;
  55.     delay(2);
  56.     read=P0;
  57.     EN=0;
  58.     delay(5);
  59.     return read;
  60. }

  61. /*画图清屏*/
  62. void clear_lcd()
  63. {
  64.   uchar i,j;
  65. write_com(0x34);      //扩充指令集动作
  66. for(i=0;i<32;i++)
  67. {
  68.     write_com(0x80+i);
  69.     write_com(0x80);
  70.     for(j=0;j<32;j++)
  71.     {
  72.         write_data(0x00);
  73.     }
  74. }
  75.      write_com(0x36);    //扩充指令集动作
  76.     write_com(0x30);    //基本指令集动作

  77. }

  78. /***********画点函数**************/
  79. void DrawPoint( unsigned char X, unsigned char Y, unsigned char Color )
  80. {
  81.     unsigned char Row , Tier , Tier_bit    ;
  82.     unsigned char  ReadOldH , ReadOldL  ;
  83.     write_com( 0x34 ) ; //写入扩充指令命令
  84.     write_com( 0x36 ) ;//显示图象
  85.     Tier = X >> 4 ;   
  86.     Tier_bit = X & 0x0f ;
  87.     if( Y < 32 )
  88.     {
  89.         Row = Y ;
  90.     }
  91.     else
  92.     {
  93.         Row = Y - 32 ;
  94.         Tier += 8 ;
  95.     }
  96.     write_com( Row + 0x80 ) ;
  97.    write_com( Tier + 0x80 ) ;
  98.     Read_data() ;
  99.     ReadOldH = Read_data() ;
  100.     ReadOldL = Read_data() ;
  101.     write_com( Row + 0x80 )  ;
  102.     write_com( Tier + 0x80 ) ;
  103.     if( Tier_bit < 8 )
  104.     {
  105.         switch( Color)
  106.         {
  107.             case 0 : ReadOldH &=( ~( 0x01 << ( 7 - Tier_bit ))) ; break ;
  108.             case 1 : ReadOldH |= ( 0x01 << ( 7 - Tier_bit ))  ;  break ;
  109.             case 2 : ReadOldH ^= ( 0x01 << ( 7 - Tier_bit ))    ; break ;
  110.             default : break ;   
  111.         }
  112.        write_data( ReadOldH ) ;
  113.        write_data( ReadOldL ) ;
  114.     }
  115.     else
  116.     {
  117.         switch(Color)
  118.         {
  119.             case 0 : ReadOldL &= (~( 0x01 << ( 15 - Tier_bit ))) ;  break ;
  120.             case 1 : ReadOldL |= ( 0x01 << ( 15 - Tier_bit ))    ;  break ;
  121.             case 2 : ReadOldL ^= ( 0x01 << ( 15 - Tier_bit ))  ;  break ;
  122.             default : break ;
  123.         }
  124.         write_data( ReadOldH ) ;
  125.         write_data( ReadOldL ) ;
  126.     }
  127.    write_com( 0x30 )    ;
  128. }

  129. /*液晶初始化*/
  130. void lcd_init()
  131. {
  132.        PSB=1;
  133.        RST=1;
  134.        write_com(0x30);
  135.        delay(1);
  136.        write_com(0x3e);
  137.        delay(1);
  138.        write_com(0x0c);
  139.        delay(1);
  140.        write_com(0x01);
  141.        delay(1);
  142. }

  143. //画水平直线
  144. void Draw_xlabel_line(uchar x0,uchar x1,uchar y,uchar color)
  145. {
  146.       uchar temp;
  147.       if(x0>x1)
  148.       {
  149.        temp=x1;
  150.        x1=x0;
  151.        x0=temp;
  152.       }
  153.       for(;x0<=x1;x0++)
  154.       DrawPoint(x0,y,color);
  155. }

  156.   //画垂直直线
  157. void Draw_row_line(uchar x,uchar y0,uchar y1,uchar color)
  158. {
  159.       uchar temp;
  160.       if(y0>y1)
  161.       {
  162.        temp=y1;
  163.        y1=y0;
  164.        y0=temp;
  165.       }
  166.       for(;y0<=y1;y0++)
  167.       DrawPoint(x,y0,color);
  168. }

  169. void sin_display()
  170. {
  171.        uchar i,j;
  172.     lcd_init();     
  173.      clear_lcd();
  174.     //画y轴箭头
  175.     DrawPoint(1,1,1);
  176.     DrawPoint(3,1,1);
  177.     DrawPoint(0,2,1);
  178.     DrawPoint(4,2,1);
  179.     Draw_row_line(2,0,60,1);  //画Y轴直线

  180.     DrawPoint(126,60,1);
  181.     DrawPoint(126,62,1);
  182.     DrawPoint(125,59,1);
  183.     DrawPoint(125,63,1);
  184.     Draw_xlabel_line(2,127,61,1);  //画X轴直线

  185.     for(i=3;i<127;i++)//画sin函数,
  186.     {
  187.         j=u*sin(pi*i/f)+30;
  188.        DrawPoint(i,j,1);
  189.     }
  190. }

  191. void main()
  192. {

  193.    sin_display();
  194.    
  195. }
复制代码

都是自己原创的,请大家多多指教。

以上程序51hei下载地址:
完整sin函数.zip (45.44 KB, 下载次数: 26)

评分

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

查看全部评分

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

使用道具 举报

沙发
ID:61207 发表于 2020-11-28 15:35 | 只看该作者
51有你黑精彩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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