找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于单片机1602显示心形程序及实物

[复制链接]
跳转到指定楼层
楼主
ID:624820 发表于 2019-10-16 09:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 1850638586 于 2019-10-16 09:32 编辑
  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. unsigned char table1[]={0x03,0x07,0x0f,0x1f,0x1f,0x1f,0x1f,0x1f,
  5.                         0x18,0x1E,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
  6.                         0x07,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
  7.                         0x10,0x18,0x1c,0x1E,0x1E,0x1E,0x1E,0x1E,
  8.                         0x0f,0x07,0x03,0x01,0x00,0x00,0x00,0x00,
  9.                         0x1f,0x1f,0x1f,0x1f,0x1f,0x0f,0x07,0x01,
  10.                         0x1f,0x1f,0x1f,0x1f,0x1f,0x1c,0x18,0x00,
  11.                         0x1c,0x18,0x10,0x00,0x00,0x00,0x00,0x00};//心图案
  12. #define   CLEARSCREEN   LCD_write_command(0x01)

  13. /**************定义接口************************/

  14. #define   LCDIO    P2
  15. sbit LCD1602_RS=P0^7;   
  16. sbit LCD1602_RW=P0^6;   
  17. sbit LCD1602_E=P0^5;

  18. /**************定义函数************************/
  19. void LCD_write_command(unsigned char command);//写入指令函数
  20. void LCD_write_dat(unsigned char dat);//写入数据函数
  21. void LCD_set_xy( unsigned char x, unsigned char y );//设置显示位置函数
  22. void LCD_dsp_char( unsigned x,unsigned char y,unsigned char dat);//显示一个字符函数
  23. void LCD_dsp_string(unsigned char X,unsigned char Y,unsigned char *s);//显示字符串函数
  24. void LCD_init(void);//初始化函数
  25. void delay_nms(unsigned int n);//延时函数
  26. /********************************************/

  27. /************初始化函数****************/
  28. void LCD_init(void)
  29. {
  30. CLEARSCREEN;//clear screen
  31. LCD_write_command(0x38);//set 8 bit data transmission mode
  32. LCD_write_command(0x0c);//open display (enable lcd display)
  33. LCD_write_command(0x80);//set lcd first display address
  34. CLEARSCREEN;//clear screen
  35. }
  36. /****************************************************/

  37. /**************写指令函数********************************/
  38. void LCD_write_command(unsigned char command)
  39. {
  40.     LCDIO=command;
  41.     LCD1602_RS=0;   
  42.     LCD1602_RW=0;
  43.     LCD1602_E=0;
  44.     LCD1602_E=1;
  45.     delay_nms(10);
  46. }
  47. /***************************************************/
  48. /****************写数据函数************************/
  49. void LCD_write_dat(unsigned char dat)
  50. {
  51. LCDIO=dat;
  52. LCD1602_RS=1;
  53. LCD1602_RW=0;
  54. LCD1602_E=0;
  55. delay_nms(1);
  56. LCD1602_E=1;
  57. }
  58. /****************************************************/

  59. /***************设置显示位置**************************/
  60. void LCD_set_xy( unsigned char x, unsigned char y )
  61. {
  62. unsigned char address;
  63. if (y == 1)
  64.    address = 0x80 + x;
  65. else
  66.       address =0xc0+ x;
  67. LCD_write_command(address);
  68. }
  69. /***************************************************/

  70. /****************显示一个字符**********************/
  71. void LCD_dsp_char( unsigned x,unsigned char y,unsigned char dat)
  72. {
  73. LCD_set_xy( x, y );
  74. LCD_write_dat(dat);
  75. }
  76. /**********************************************/

  77. /***************显示字符串函数***************/
  78. void LCD_dsp_string(unsigned char X,unsigned char Y,unsigned char *s)
  79. {
  80.      LCD_set_xy( X, Y );
  81.      while (*s)
  82.      {
  83.        LCD_write_dat(*s);   
  84.        s ++;
  85.      }
  86. }
  87. /***********************************************/

  88. /********** 延时**********************/
  89. void delay_nms(unsigned int n)      
  90. {
  91.      unsigned int i=0,j=0;
  92.      for (i=n;i>0;i--)
  93.      for (j=0;j<10;j++);
  94. }
  95. /**************************************/

  96. /***********主函数**************/
  97. void main(void)
  98. {
  99. unsigned char i,j,k,tmp;
  100. LCD_init();
  101. delay_nms(100);
  102. tmp=0x40;//设置CGRAM地址的格式字
  103. k=0;
  104. for(j=0;j<8;j++)
  105.    {
  106.       for(i=0;i<8;i++)
  107.        {
  108.          LCD_write_command(tmp+i); // 设置自定义字符的 CGRAM 地址
  109.          delay_nms(2);
  110.          LCD_write_dat(table1[k]); // 向CGRAM写入自定义字符表的数据
  111.          k++;
  112.          delay_nms(2);
  113.        }
  114.       tmp=tmp+8;
  115.     }
  116.    LCD_dsp_string(1,1,"heart");//在第一行第一列显示“heart”
  117.    for (i=0;i<4;i++)
  118.      {
  119.        LCD_dsp_char( 10+i,1,i);//在第一行第10列位置显示心图案的上半部
  120.        delay_nms(1);
  121.      }
  122.    for (i=4;i<8;i++)
  123.      {
  124.        LCD_dsp_char( 10+i-4,2,i);//在第二行第10列位置显示心图案的下半部
  125.        delay_nms(1);
  126.      }
  127.    while (1);
  128. }
复制代码


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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