段显示

时间:2018-11-22 17:33:59

标签: arduino segment

我希望有人可以对此进行指导,我全神贯注,不知道我需要改变什么。我将这些代码从我看到的其他示例中一点一点地放到一起。倒计时结束后,我试图使显示屏上显示“ End”字样,但我只是不理解使显示部分有任何进一步更改的代码。

这基本上就是代码的作用。我使用旋转旋钮并设置了一个时间,按下旋转旋钮,计时器开始倒计时,同时在4位7段数码管显示屏中显示该值。倒数计时结束时,试管显示变为空白,而不是空白,我想显示“ End”一词,如何使之成为可能?

我知道捕获在函数setDigit()中,但是我对使用7段显示器一无所知,这是我第一次。

const int PinCLK = 3;  // Rotary CLK signal
const int PinDT = 2;   // Rotary DT signal
const int PinSW = 0;   // Rotary switch
int lastCount = 0;
volatile int virtualPosition = 0;
int timeCount = 0;  // count in tenths of seconds
int sequence_Num = 0;

char disp[4];
const int PinSCLK = 10;
const int PinRCLK = 11;
const int PinDIO = 12;
int Display[4];
unsigned long prev = 0;
unsigned long waitMS = 0;

void isr()
{
  static unsigned long lastInterruptTime = 0;
  unsigned long interruptTime = millis();

  // If interrupts come faster than 5
  if(interruptTime - lastInterruptTime > 5)
  {
    if(digitalRead(PinDT) == LOW)
    {
      virtualPosition = virtualPosition+10;
    }
    else
    {
      virtualPosition = virtualPosition-10;
    }

    virtualPosition = min(1000, max(0, virtualPosition));
  }

  lastInterruptTime = interruptTime;  // Keep track of when we were here last (no more than every 5ms)
}

void displayCount()
{
  showDisplay();
  for(int i = 0; i < 4; i++)
  {
    setDigit(i, Display[i]);
  }
}

void setDigit(int dig, int character)
{
  //  0   1  2  3 4 5 6 7    for 8 digit array
  int digits[] = {128, 64, 32, 16, 8, 4, 2, 1};

  // 21 items character set (0-9)0-9, (10-19)0.-9., (72)- (73) space
  int characters[] = {3, 159, 37, 13, 153, 73, 65, 31, 1, 9, 2, 158, 36, 12, 152, 72, 64, 30, 0, 8, 255};

  digitalWrite(PinRCLK, LOW);
  shiftOut(PinDIO, PinSCLK, LSBFIRST, characters[character]);
  shiftOut(PinDIO, PinSCLK, LSBFIRST, digits[dig]);
  digitalWrite(PinRCLK, HIGH);
}

void showDisplay()
{
  for(int i = 0; i < 4; i++)
  {
    int val = disp[i];
    if ((val >= 32) && (val <= 47))
    {
      switch(val)
      {
        case 45 : val = 20;
          break;
          default : val = 20;
          break;
      }
    }
    else if ((val >= 48) && (val <= 57))
    {
      val -= 48;
    }
    Display[i] = val;
  }
}

void wait(unsigned long milsec)
{
  prev = millis();
  waitMS = milsec;
}

void showText(char a, char b , char c, char d)
{
  disp[0] = d;
  disp[1] = c;
  disp[2] = b;
  disp[3] = a;
}

void setTimer(bool inc)
{
    displayCount();
    if(millis() > (prev + waitMS))
    {
      if(inc)
      {
        if(timeCount = virtualPosition)
        { // NOTE: This incrementer continues to count although the display 'int period' has occured
          showText(' ', virtualPosition / 1000 % 10, virtualPosition / 100 % 10 + 10, virtualPosition / 10 % 10);
          if(timeCount < virtualPosition)
          {
            timeCount++;
          }
          wait(9); // Delay to compensates for lag to count in increments of 9 milliseconds
        }
      }
      else
      {
        if(timeCount <= virtualPosition) 
        { 
          showText(' ', (timeCount / 1000) % 10, (timeCount / 100) % 10 + 10, (timeCount / 10) % 10);
          timeCount--;
        }
        wait(9);
        if(timeCount == 0) 
        {
          timeCount = virtualPosition;
          sequence_Num = 1;
          delay(2000);
        }
      }
    }
}

void setup() 
{
  Serial.begin(9600);             // Just whilst we debug, view output on serial monitor
  pinMode(PinCLK, INPUT);        // Rotary pulses are INPUTs
  pinMode(PinDT, INPUT);         // Rotary pulses are INPUTs
  pinMode(PinSW, INPUT_PULLUP);  // use the in-built PULLUP so we don't need a resistor

  attachInterrupt(digitalPinToInterrupt(PinCLK), isr, LOW);     // Attach the routine to service the interrupts
  pinMode(PinRCLK, OUTPUT);  // latch
  pinMode(PinDIO, OUTPUT);
  pinMode(PinSCLK, OUTPUT);

  lastCount = virtualPosition;
  sequence_Num = 1;
}

void loop() 
{
  // Is someone pressing the rotary switch?
  if((!digitalRead(PinSW)))
  {
    while(!digitalRead(PinSW))
    delay(9);
    sequence_Num = 2;
  }

  switch(sequence_Num)
  {
    case 1:  
      setTimer(true);
      break;
    case 2:
      setTimer(false);
      break;
  }
}

感谢任何帮助和解释,以便我理解以备将来使用。

谢谢

0 个答案:

没有答案
相关问题