福州布衣 发表于 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={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 Functionsvoid 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 Functionsvoid CreateArray();void FindNums();
//Private Variablesboolean mode,DigitOn,DigitOff,SegOn,SegOff;byte DigitPins;byte SegmentPins;boolean lights;byte nums;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"#endifclass SevSeg{public:SevSeg();//Public Functionsvoid 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 Variablesprivate://Private Functionsvoid CreateArray();void FindNums();//Private Variablesboolean mode,DigitOn,DigitOff,SegOn,SegOff;byte DigitPins;byte SegmentPins;boolean lights;byte nums;int number;byte DecPlace;boolean negative;};#endif
库文件-2(SevSeg.cpp)#include "SevSeg.h"
SevSeg::SevSeg(){//Initial valuesnumber=8888;DecPlace=0;
}
//Begin/*******************************************************************************************///Set pin modes and turns all displays offvoid 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 anodemode=mode_in;if (mode==1){    DigitOn=HIGH;    DigitOff=LOW;    SegOn=LOW;    SegOff=HIGH;}else {    DigitOn=LOW;    DigitOff=HIGH;    SegOn=HIGH;    SegOff=LOW;}
DigitPins=D1;DigitPins=D2;DigitPins=D3;DigitPins=D4;SegmentPins=S1;SegmentPins=S2;SegmentPins=S3;SegmentPins=S4;SegmentPins=S5;SegmentPins=S6;SegmentPins=S7;SegmentPins=S8;

//Set Pin Modes as outputsfor (byte digit=0;digit<4;digit++) {    pinMode(DigitPins, OUTPUT);}for (byte seg=0;seg<8;seg++) {    pinMode(SegmentPins, OUTPUT);}
//Turn Everything Off//Set all digit pins off.Low for common anode, high for common cathodefor (byte digit=0;digit<4;digit++) {    digitalWrite(DigitPins, DigitOff);}//Set all segment pins off.High for common anode, low for common cathodefor (byte seg=0;seg<8;seg++) {    digitalWrite(SegmentPins, SegOff);}}
//Refresh Display/*******************************************************************************************///Cycles through each segment and turns on the correct digits for each.//Leaves everything offvoid SevSeg::PrintOutput(){for (byte seg=0;seg<8;seg++) {    //Turn the relevant segment on    digitalWrite(SegmentPins,SegOn);
    //For each digit, turn relevant digits on    for (byte digit=0;digit<4;digit++){      if (lights==1) {      digitalWrite(DigitPins,DigitOn);      }      //delay(200); //Uncomment this to see it in slow motion    }    //Turn all digits off    for (byte digit=0;digit<4;digit++){      digitalWrite(DigitPins,DigitOff);    }
    //Turn the relevant segment off    digitalWrite(SegmentPins,SegOff);}}
//New Number/*******************************************************************************************///Function to update the number displayedvoid SevSeg::NewNum(int number_in, byte DecPlace_in){//Feed the inputs into the library's variablesnumber=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 positiveif (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=21;    nums=21;    nums=21;    nums=21;}
else{    //Find the four digits    int total=number;    if (negative==0) {      nums=number/1000;      total=total-nums*1000;    }    else {      nums=21;    }    nums=total/100;    total=total-nums*100;    nums=total/10;    nums=total-nums*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 && DecPlace<3){      nums=20;      if (nums==0 && DecPlace<2) {          nums=20;          if (nums==0 && DecPlace==0) {            nums=20;          }      }      }    }    else {      if (nums==0 && DecPlace<2) {      nums=20;      if (nums==0 && DecPlace==0) {          nums=20;      }      }    }}}
//Create Array/*******************************************************************************************///From the numbers found, says which LEDs need to be turned onvoid SevSeg::CreateArray() {for (byte digit=0;digit<4;digit++) {    switch (nums){    case 0:      lights=1;      lights=1;      lights=1;      lights=1;      lights=1;      lights=1;      lights=0;      break;    case 1:      lights=0;      lights=1;      lights=1;      lights=0;      lights=0;      lights=0;      lights=0;      break;    case 2:      lights=1;      lights=1;      lights=0;      lights=1;      lights=1;      lights=0;      lights=1;      break;    case 3:      lights=1;      lights=1;      lights=1;      lights=1;      lights=0;      lights=0;      lights=1;      break;    case 4:      lights=0;      lights=1;      lights=1;      lights=0;      lights=0;      lights=1;      lights=1;      break;    case 5:      lights=1;      lights=0;      lights=1;      lights=1;      lights=0;      lights=1;      lights=1;      break;    case 6:      lights=1;      lights=0;      lights=1;      lights=1;      lights=1;      lights=1;      lights=1;      break;    case 7:      lights=1;      lights=1;      lights=1;      lights=0;      lights=0;      lights=0;      lights=0;      break;    case 8:      lights=1;      lights=1;      lights=1;      lights=1;      lights=1;      lights=1;      lights=1;      break;    case 9:      lights=1;      lights=1;      lights=1;      lights=1;      lights=0;      lights=1;      lights=1;      break;    case 20:      lights=0;      lights=0;      lights=0;      lights=0;      lights=0;      lights=0;      lights=0;      break;    case 21:      lights=0;      lights=0;      lights=0;      lights=0;      lights=0;      lights=0;      lights=1;      break;    default:      lights=0;      lights=0;      lights=1;      lights=1;      lights=1;      lights=0;      lights=1;      break;    }
    //Set the decimal place lights    if (3-digit==DecPlace){      lights=1;    }    else {      lights=0;    }}}


页: [1]
查看完整版本: 数码管驱动库使用