极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9044|回复: 1

Arduino 实现字符串在单数码管的显示【C++程序员的告白】

[复制链接]
发表于 2014-6-3 21:28:18 | 显示全部楼层 |阅读模式
我采用了Arduino UNO Rev3 编程在单数码管上显示了一行字符串。使字符串可以在一个数码管上滚动显示,每个单词之间用'-'字符隔开。视频中字符显示的是"I-LOUE-YOU-YUANBAO",是一个C++程序员的告白。在实现过程中,我编制了字符对应的数码管编码表,可以显示数字:0123456789 和字母:ABCDEFGHIJLNOPQUY 。单个字符停留0.5s时间,间隔字符'-'停留1.5s时间。
具体实现细节请看我上传的视频下列代码,因为Arduino IDE 无法写中文注释的原因,我采用了很poor 的英文写下了注释。
//视频要顺时针旋转90°播放哦。



  1.     //////////////////////////////////////////////////////
  2.     // Display symbol in Numerical Tube
  3.     /////////////////////////////////////////////////////
  4.    
  5.     /*
  6.     Establish a simple digital and charactor map
  7.     digital: 0,1,2,3,4,5,6,7,8,9
  8.     character: A,b,C,d,E,F,G,H,I,J,L,n,o,P,q,U,y
  9.     */
  10.    
  11.     char num_tube_char[36] = "-0123456789ABCDEFGHIJLNOPQUY";
  12.     byte num_tube_code[36] = {
  13.                             0b00000010,  /* - */
  14.                             0b11111100,0b01100000,0b11011010,0b11110010,
  15.                             0b01100110,0b10110110,0b10111110,0b11100000,
  16.                             0b11111110,0b11110110, /*from 0 to 9*/
  17.                             0b11101110,0b00111110,0b10011100,0b01111010,
  18.                             0b10011110,0b10001110,0b10111101,0b01101110,
  19.                             0b00001100,0b01110000,0b00011100,0b11101100,
  20.                             0b00111010,0b11001110,0b11100110,0b01111100,
  21.                             0b01110110  /*from A to y*/
  22.                           };
  23.     const int A_port = 9;
  24.     const int B_port = 8;
  25.     const int C_port = 2;
  26.     const int D_port = 3;
  27.     const int E_port = 4;
  28.     const int F_port = 5;
  29.     const int G_port = 6;
  30.     const int dot_port = 7;
  31.    
  32.     int port[8] = {A_port, B_port, C_port, D_port, E_port, F_port, G_port, dot_port};
  33.    
  34.     void setup()
  35.     {
  36.       pinMode(A_port, OUTPUT);
  37.       pinMode(B_port, OUTPUT);
  38.       pinMode(C_port, OUTPUT);
  39.       pinMode(D_port, OUTPUT);
  40.       pinMode(E_port, OUTPUT);
  41.       pinMode(F_port, OUTPUT);
  42.       pinMode(G_port, OUTPUT);
  43.       pinMode(dot_port, OUTPUT);
  44.     }
  45.     void loop()
  46.     {
  47.       int i =0;
  48.       char word[] = {"I-LOUE-YOU-YUANBAO&"};
  49.       while(word[i])
  50.       {
  51.         Display(word[i++]);
  52.       }
  53.     }  
  54.     /*  Write in code of charactor and display in numerical tube   */
  55.     void WriteIn(byte b)
  56.     {
  57.       byte tmp = 0b00000000;
  58.       int i = 0;
  59.       while(i < 8)
  60.       {
  61.         tmp = b;
  62.         b <<= 1;
  63.         if(b >= tmp)
  64.         {
  65.           digitalWrite(port[i++], HIGH);
  66.         }
  67.         else
  68.         {
  69.           digitalWrite(port[i++], LOW);
  70.         }
  71.       }
  72.     }
  73.      
  74.     void Display(const char ch)
  75.    {
  76.      /*Search Index from Table and find the code of charactor ch*/
  77.      int i = 0;
  78.      for( i = 0; i < 36; i++)
  79.      {
  80.        if(ch == num_tube_char[i])
  81.        {
  82.          WriteIn(num_tube_code[i]);
  83.          if(num_tube_char[i] == '-')
  84.          {
  85.            delay(1500);
  86.          }
  87.          else
  88.          {
  89.             delay(500);
  90.          }
  91.          break;
  92.        }
  93.      }
  94.      if(i == 36)
  95.      {
  96.        for(int i = 0; i < 3; i++)
  97.        {
  98.          WriteIn(0b00000000);
  99.          delay(250);
  100.          WriteIn(0b11111111);
  101.          delay(250);
  102.        }
  103.      }
  104.    }
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-6-3 21:29:46 | 显示全部楼层
哈哈,感兴趣的可以把函数封装成为类哈。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-13 20:03 , Processed in 0.035653 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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