作者 主题: 分享一个原创7段数码管类库  (阅读 2989 次)

ArdyPro

  • Newbie
  • *
  • 帖子: 17
分享一个原创7段数码管类库
« 于: 四月 22, 2012, 03:34:18 下午 »
自己折腾LED时候顺手写的数码管类库,可以支持共阴/共阳两种类型,可以显示整数/浮点数/字符串,位数可以任意,当然我只测试了4位的,太多的话,估计要做小的修改。

在Arduino0022、0023下测试正常,1.0好像在显示浮点数的时候有个黑屏时间,如果你在使用过程中遇到这个问题,可以自己先将浮点数转成字符串,然后调用display(string);

默认的数码管类型是共阴,如果是共阳类型,请在声明对象时,选择COMMONANODE类型:

SevenSegmentDisplay led(COMMONANODE);

可以在声明对象时传递引脚,也可以后面在传递。

显示的时候,直接用led.display()方法,有几种重载的方式,因此无论显示整数还是浮点数,或者字符串,都是同样方法display();

代码写的比较粗糙,希望大家在使用过程中提出好的建议。

本类库可以免费用于一切应用,但是商用的话,需要提前吱一声,好让我知道。

类库可以下载附件,最新版本在google code那里维护,经常去瞅瞅,可能有新发现, ;D

http://code.google.com/p/7segmentled/source/checkout
« 最后编辑时间: 四月 22, 2012, 06:33:33 下午 作者 ArdyPro »

ArdyPro

  • Newbie
  • *
  • 帖子: 17
Re: 分享一个原创7段数码管类库
« 回复 #1 于: 四月 22, 2012, 03:38:38 下午 »
sample code:

程序代码
#include <SevenSegmentDisplay.h>
/*
 Sample codes for 7 segments display
 
 by Jack Zhong(jzopen@yeah.net)
 
 
 
 Please refer to the library header file
 for usage instructions in Chinese.
 
 If you ran into trouble to read those
 Chinese words, please open the .h files
 with Notepad or any other word processing
 application you like. Please bear in mind,
 DO NOT try to modify the codes unless there
 is a must to do so.
 
 */

//  Declare a variable led of cathode common type,
//  if it were a anode one, please imply the type
//  when declared in this way:
//  SevenSegmentDisplay led(COMMONANODE);
SevenSegmentDisplay led;
//  Define an array to host digit pins
//  In this example, the display has
//  4 digits, in another word, it could
//  display 4 digits at a time
unsigned int digitsPins[4] =
{
  2, 3, 4, 5 };
//  Define an array to host segment pins
//  pins start from segmemt a to segment dp.
unsigned int segPins[8] =
{
  6, 7, 8, 9, 10, 11, 12, 13 };

char c='1';
int i=0;

void setup()
{
  //  enable Serial object if you need trace the project
  Serial.begin(9600); 

  //  You can pass the pins in either of two ways
  //led.setSegmentPins(&segPins[0]);
  led.setSegmentPins(&segPins[0]);
  led.setDigitPins(&digitsPins[0]);

  // call diagnoze() to check if the display work properly
  led.diagnoze();
}

void loop()
{
  i++;
  c= i/1000;
  if (c>6)
    i=0;

  //because my serial in mega board was out of work,
  //so I disable the following code.

  //#define GOODSERIAL
#ifdef GOODSERIAL
  if (Serial.available()>0)
    c= Serial.read();
#else
  c=c+'0';
#endif

  Serial.println(c);

  switch (c)
  {
  case '0':
  case 'l':
  case 'L':

    //  Display string, it supports left alignment,
    //  center alignment and right alignment
    //  the default value for display string is left one.
    led.display("Err");
    break;
  case '1':
  case 'c':
  case 'C':
    led.display("Err", taCENTER);
    break;
  case '2':
  case 'r':
  case 'R':
    led.display("Err", taRIGHT);
    break;
  case '3':
  case 'i':
  case 'I':
    //  Display integer number
    led.display(i/5);
    break;
  case '4':
    //  you can decide whether add the leading zero or not
    led.display(i/5,true);
    break;
  case '5':
  case 'f':
  case 'F':
    //  display float number
    led.display( i/10.0);
    break;
  default:
    //led.diagnoze();
    break;
  }

}
[/size][/size][/font][/size]

注:我的板子串口有点抽风,所以用了循环。
« 最后编辑时间: 四月 22, 2012, 03:41:09 下午 作者 ArdyPro »

Tags: