Arduino 实现字符串在单数码管的显示【C++程序员的告白】
我采用了Arduino UNO Rev3 编程在单数码管上显示了一行字符串。使字符串可以在一个数码管上滚动显示,每个单词之间用'-'字符隔开。视频中字符显示的是"I-LOUE-YOU-YUANBAO",是一个C++程序员的告白。在实现过程中,我编制了字符对应的数码管编码表,可以显示数字:0123456789 和字母:ABCDEFGHIJLNOPQUY 。单个字符停留0.5s时间,间隔字符'-'停留1.5s时间。具体实现细节请看我上传的视频下列代码,因为Arduino IDE 无法写中文注释的原因,我采用了很poor 的英文写下了注释。
//视频要顺时针旋转90°播放哦。
http://www.tudou.com/programs/view/WdciExTPQOo
//////////////////////////////////////////////////////
// Display symbol in Numerical Tube
/////////////////////////////////////////////////////
/*
Establish a simple digital and charactor map
digital: 0,1,2,3,4,5,6,7,8,9
character: A,b,C,d,E,F,G,H,I,J,L,n,o,P,q,U,y
*/
char num_tube_char = "-0123456789ABCDEFGHIJLNOPQUY";
byte num_tube_code = {
0b00000010,/* - */
0b11111100,0b01100000,0b11011010,0b11110010,
0b01100110,0b10110110,0b10111110,0b11100000,
0b11111110,0b11110110, /*from 0 to 9*/
0b11101110,0b00111110,0b10011100,0b01111010,
0b10011110,0b10001110,0b10111101,0b01101110,
0b00001100,0b01110000,0b00011100,0b11101100,
0b00111010,0b11001110,0b11100110,0b01111100,
0b01110110/*from A to y*/
};
const int A_port = 9;
const int B_port = 8;
const int C_port = 2;
const int D_port = 3;
const int E_port = 4;
const int F_port = 5;
const int G_port = 6;
const int dot_port = 7;
int port = {A_port, B_port, C_port, D_port, E_port, F_port, G_port, dot_port};
void setup()
{
pinMode(A_port, OUTPUT);
pinMode(B_port, OUTPUT);
pinMode(C_port, OUTPUT);
pinMode(D_port, OUTPUT);
pinMode(E_port, OUTPUT);
pinMode(F_port, OUTPUT);
pinMode(G_port, OUTPUT);
pinMode(dot_port, OUTPUT);
}
void loop()
{
int i =0;
char word[] = {"I-LOUE-YOU-YUANBAO&"};
while(word)
{
Display(word);
}
}
/*Write in code of charactor and display in numerical tube */
void WriteIn(byte b)
{
byte tmp = 0b00000000;
int i = 0;
while(i < 8)
{
tmp = b;
b <<= 1;
if(b >= tmp)
{
digitalWrite(port, HIGH);
}
else
{
digitalWrite(port, LOW);
}
}
}
void Display(const char ch)
{
/*Search Index from Table and find the code of charactor ch*/
int i = 0;
for( i = 0; i < 36; i++)
{
if(ch == num_tube_char)
{
WriteIn(num_tube_code);
if(num_tube_char == '-')
{
delay(1500);
}
else
{
delay(500);
}
break;
}
}
if(i == 36)
{
for(int i = 0; i < 3; i++)
{
WriteIn(0b00000000);
delay(250);
WriteIn(0b11111111);
delay(250);
}
}
} 哈哈,感兴趣的可以把函数封装成为类哈。
页:
[1]