极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4765|回复: 0

数码管驱动库使用

[复制链接]
发表于 2019-3-26 17:42:24 | 显示全部楼层 |阅读模式
数码管驱动库使用
接线图(共阴4管)

代码-1:
//电位器
#include "SevSeg.h"
SevSeg sevseg;
void setup() {
//定义需要使用的引脚
  sevseg.Begin(0,10,11,12,13,2,3,4,5,6,7,8,9);
//sevseg.Begin(共阴0共阳1,共极引脚,a-g,dp引脚);
}

void loop() {
sevseg.PrintOutput();
int val = analogRead(A0);
val = map(val,0,1023,0,9);    //使用map函数来做数值映射   
sevseg.NewNum(val, 4);     
}

代码-2:
//串口
#include "SevSeg.h"
SevSeg sevseg;
String inString = "";   
int val=0;
void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }
  Serial.println("串口显示四位数码管");
  Serial.println();
  sevseg.Begin(0,10,11,12,13,2,3,4,5,6,7,8,9);
}

void loop() {
while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
       inString += (char)inChar;      
    }

     if (inChar == '\n') {
      if(inString.length()>4){
      Serial.print("输入的字符串数不能大于四!");
      inString = "";
}else{
      //Serial.print("Value:");
      //Serial.println(inString.toInt());
      Serial.print("String: ");
      Serial.println(inString);
      val=inString.toInt();
      inString = "";
     }
}
  }   

    sevseg.PrintOutput();
    sevseg.NewNum(val, 4);   
}

代码-3:
//数组
#include "SevSeg.h"
SevSeg sevseg;
int val[10]={0000,1111,2222,3333,4444,5555,6666,7777,8888,9999};
void setup() {
  Serial.begin(9600);
  sevseg.Begin(0,10,11,12,13,2,3,4,5,6,7,8,9);
}

void loop() {
    for(int i=0;i<10;i++){
      for(int j=0;j<1000;j++){
           sevseg.PrintOutput();
           sevseg.NewNum(val, 4);
           }
           delay(500);
    }   
}

库文件-1(SevSeg.h)
#define SevSeg_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif


class SevSeg
{

public:
  SevSeg();

  //Public Functions
  void PrintOutput();
  void NewNum(int number_in, byte DecPlace_in);
  void Begin(boolean mode_in,byte C1, byte C2, byte C3, byte C4, byte UC1, byte UC2, byte UC3, byte UC4, byte UC5, byte UC6, byte UC7, byte UC8);

  //Public Variables


private:
  //Private Functions
  void CreateArray();
  void FindNums();

  //Private Variables
  boolean mode,DigitOn,DigitOff,SegOn,SegOff;
  byte DigitPins[4];
  byte SegmentPins[8];
  boolean lights[4][8];
  byte nums[4];
  int number;
  byte DecPlace;
  boolean negative;

};

#end
#ifndef SevSeg_h
#define SevSeg_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class SevSeg{
public:
  SevSeg();
  //Public Functions
  void PrintOutput();
  void NewNum(int number_in, byte DecPlace_in);
  void Begin(boolean mode_in,byte C1, byte C2, byte C3, byte C4, byte UC1, byte UC2, byte UC3, byte UC4, byte UC5, byte UC6, byte UC7, byte UC8);
  //Public Variables
private:
  //Private Functions
  void CreateArray();
  void FindNums();
  //Private Variables
  boolean mode,DigitOn,DigitOff,SegOn,SegOff;
  byte DigitPins[4];
  byte SegmentPins[8];
  boolean lights[4][8];
  byte nums[4];
  int number;
  byte DecPlace;
  boolean negative;
};
#endif

库文件-2(SevSeg.cpp)
#include "SevSeg.h"

SevSeg::SevSeg()
{
  //Initial values
  number=8888;
  DecPlace=0;

}

//Begin
/*******************************************************************************************/
//Set pin modes and turns all displays off
void SevSeg::Begin(boolean mode_in,byte D1, byte D2, byte D3, byte D4, byte S1, byte S2, byte S3, byte S4, byte S5, byte S6, byte S7, byte S8){

  //Assign input values to variables
  //Mode sets what the digit pins must be set at for it to be turned on.  0 for common cathode, 1 for common anode
  mode=mode_in;
  if (mode==1){
    DigitOn=HIGH;
    DigitOff=LOW;
    SegOn=LOW;
    SegOff=HIGH;
  }
  else {
    DigitOn=LOW;
    DigitOff=HIGH;
    SegOn=HIGH;
    SegOff=LOW;
  }

  DigitPins[0]=D1;
  DigitPins[1]=D2;
  DigitPins[2]=D3;
  DigitPins[3]=D4;
  SegmentPins[0]=S1;
  SegmentPins[1]=S2;
  SegmentPins[2]=S3;
  SegmentPins[3]=S4;
  SegmentPins[4]=S5;
  SegmentPins[5]=S6;
  SegmentPins[6]=S7;
  SegmentPins[7]=S8;


  //Set Pin Modes as outputs
  for (byte digit=0;digit<4;digit++) {
    pinMode(DigitPins[digit], OUTPUT);
  }
  for (byte seg=0;seg<8;seg++) {
    pinMode(SegmentPins[seg], OUTPUT);
  }

  //Turn Everything Off
  //Set all digit pins off.  Low for common anode, high for common cathode
  for (byte digit=0;digit<4;digit++) {
    digitalWrite(DigitPins[digit], DigitOff);
  }
  //Set all segment pins off.  High for common anode, low for common cathode
  for (byte seg=0;seg<8;seg++) {
    digitalWrite(SegmentPins[seg], SegOff);
  }
}

//Refresh Display
/*******************************************************************************************/
//Cycles through each segment and turns on the correct digits for each.
//Leaves everything off
void SevSeg:rintOutput(){
  for (byte seg=0;seg<8;seg++) {
    //Turn the relevant segment on
    digitalWrite(SegmentPins[seg],SegOn);

    //For each digit, turn relevant digits on
    for (byte digit=0;digit<4;digit++){
      if (lights[digit][seg]==1) {
        digitalWrite(DigitPins[digit],DigitOn);
      }
      //delay(200); //Uncomment this to see it in slow motion
    }
    //Turn all digits off
    for (byte digit=0;digit<4;digit++){
      digitalWrite(DigitPins[digit],DigitOff);
    }

    //Turn the relevant segment off
    digitalWrite(SegmentPins[seg],SegOff);
  }
}

//New Number
/*******************************************************************************************/
//Function to update the number displayed
void SevSeg::NewNum(int number_in, byte DecPlace_in)
{
  //Feed the inputs into the library's variables
  number=number_in;
  DecPlace=DecPlace_in;
  FindNums();
  CreateArray();
}


//Find Digits (Nums)
/*******************************************************************************************/
//Function to find the four individual digits to be displayed from the variable 'number'
void SevSeg::FindNums() {
  //If the number received is negative, set the flag and make the number positive
  if (number<0) {
    negative=1;
    number=number*-1;
  }
  else {
    negative=0;
  }

  //If the number is out of range, just display '----'
  if ((negative==0 && number>9999) || (negative==1 && number>999)) {
    nums[0]=21;
    nums[1]=21;
    nums[2]=21;
    nums[3]=21;
  }

  else{
    //Find the four digits
    int total=number;
    if (negative==0) {
      nums[0]=number/1000;
      total=total-nums[0]*1000;
    }
    else {
      nums[0]=21;
    }
    nums[1]=total/100;
    total=total-nums[1]*100;
    nums[2]=total/10;
    nums[3]=total-nums[2]*10;


    //If there are zeros, set them to 20 which means a blank
    //However, don't cut out significant zeros
    if (negative==0) {
      if (nums[0]==0 && DecPlace<3){
        nums[0]=20;
        if (nums[1]==0 && DecPlace<2) {
          nums[1]=20;
          if (nums[2]==0 && DecPlace==0) {
            nums[2]=20;
          }
        }
      }
    }
    else {
      if (nums[1]==0 && DecPlace<2) {
        nums[1]=20;
        if (nums[2]==0 && DecPlace==0) {
          nums[2]=20;
        }
      }
    }
  }
}

//Create Array
/*******************************************************************************************/
//From the numbers found, says which LEDs need to be turned on
void SevSeg::CreateArray() {
  for (byte digit=0;digit<4;digit++) {
    switch (nums[digit]){
    case 0:
      lights[digit][0]=1;
      lights[digit][1]=1;
      lights[digit][2]=1;
      lights[digit][3]=1;
      lights[digit][4]=1;
      lights[digit][5]=1;
      lights[digit][6]=0;
      break;
    case 1:
      lights[digit][0]=0;
      lights[digit][1]=1;
      lights[digit][2]=1;
      lights[digit][3]=0;
      lights[digit][4]=0;
      lights[digit][5]=0;
      lights[digit][6]=0;
      break;
    case 2:
      lights[digit][0]=1;
      lights[digit][1]=1;
      lights[digit][2]=0;
      lights[digit][3]=1;
      lights[digit][4]=1;
      lights[digit][5]=0;
      lights[digit][6]=1;
      break;
    case 3:
      lights[digit][0]=1;
      lights[digit][1]=1;
      lights[digit][2]=1;
      lights[digit][3]=1;
      lights[digit][4]=0;
      lights[digit][5]=0;
      lights[digit][6]=1;
      break;
    case 4:
      lights[digit][0]=0;
      lights[digit][1]=1;
      lights[digit][2]=1;
      lights[digit][3]=0;
      lights[digit][4]=0;
      lights[digit][5]=1;
      lights[digit][6]=1;
      break;
    case 5:
      lights[digit][0]=1;
      lights[digit][1]=0;
      lights[digit][2]=1;
      lights[digit][3]=1;
      lights[digit][4]=0;
      lights[digit][5]=1;
      lights[digit][6]=1;
      break;
    case 6:
      lights[digit][0]=1;
      lights[digit][1]=0;
      lights[digit][2]=1;
      lights[digit][3]=1;
      lights[digit][4]=1;
      lights[digit][5]=1;
      lights[digit][6]=1;
      break;
    case 7:
      lights[digit][0]=1;
      lights[digit][1]=1;
      lights[digit][2]=1;
      lights[digit][3]=0;
      lights[digit][4]=0;
      lights[digit][5]=0;
      lights[digit][6]=0;
      break;
    case 8:
      lights[digit][0]=1;
      lights[digit][1]=1;
      lights[digit][2]=1;
      lights[digit][3]=1;
      lights[digit][4]=1;
      lights[digit][5]=1;
      lights[digit][6]=1;
      break;
    case 9:
      lights[digit][0]=1;
      lights[digit][1]=1;
      lights[digit][2]=1;
      lights[digit][3]=1;
      lights[digit][4]=0;
      lights[digit][5]=1;
      lights[digit][6]=1;
      break;
    case 20:
      lights[digit][0]=0;
      lights[digit][1]=0;
      lights[digit][2]=0;
      lights[digit][3]=0;
      lights[digit][4]=0;
      lights[digit][5]=0;
      lights[digit][6]=0;
      break;
    case 21:
      lights[digit][0]=0;
      lights[digit][1]=0;
      lights[digit][2]=0;
      lights[digit][3]=0;
      lights[digit][4]=0;
      lights[digit][5]=0;
      lights[digit][6]=1;
      break;
    default:
      lights[digit][0]=0;
      lights[digit][1]=0;
      lights[digit][2]=1;
      lights[digit][3]=1;
      lights[digit][4]=1;
      lights[digit][5]=0;
      lights[digit][6]=1;
      break;
    }

    //Set the decimal place lights
    if (3-digit==DecPlace){
      lights[digit][7]=1;
    }
    else {
      lights[digit][7]=0;
    }
  }
}



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 17:18 , Processed in 0.061108 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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