jg53fn55
Published © GPL3+

Display VFD48-1202FN analog style

How to control the VFD48 display with Arduino Uno

IntermediateProtip1,552
Display VFD48-1202FN analog style

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Shift Register- Parallel to Serial
Texas Instruments Shift Register- Parallel to Serial
shiftregister SN74595 and four transistor drivers UDN2981 or ULN2803
×4
DS3231MPMB1 Peripheral Module
Maxim Integrated DS3231MPMB1 Peripheral Module
×1
Temperature Sensor
Temperature Sensor
LM35
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1
Toggle Switch, (On)-Off-(On)
Toggle Switch, (On)-Off-(On)
×3
Evaluation Board, ISL8117A Synchronous Buck Converter
Evaluation Board, ISL8117A Synchronous Buck Converter
Buck converter
×2
VFD48-1202FN
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

VFD48

Display VFD48

VFD48 datasheet

datasheet

Code

VFD48

Arduino
Control the VFD48 Display
#include <RTC.h>
#include <Wire.h>
//#define DS3231_ADDRESS 0x68
static DS3231 RTC;
#define CLKRD A0
#define DATARD A1
const byte buttonPin = A2; // this is the Arduino pin we are connecting the push button to
#define Pin_LM35 A3
// zie excel bestand VFD48code.xlsx
const byte interruptPin = 2;
////Pin connected to Serial in of 74595
int dataPinR = 3;

//Pin connected to Latch of 74595
int latchPinR = 4;

//Pin connected to ClockPulse of 74595
int clockPinR = 5;
int Filament = 6;
int SW1 = 9;
int SW2 = 10;
int SW3 = 11;
unsigned long currentMillis;
long interval = 3000;
long previousMillis = 0;
int tempC2;
byte tmpeh;
byte tmptt;
float tempC;
int tempC1;
/*byte Grid1[61] = {
  0, 0, 0, 1, 1, 1, 1, 1, 2, 2,
  2, 2, 2, 4, 4, 4, 4, 4, 8, 8,
  8,  8,  8, 16, 16, 16, 16, 16, 32, 32,
  32, 32, 32, 64, 64, 64, 64, 64, 128, 128,
  128, 128, 128, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };

  byte Grid2[61] = {
  8, 8, 8, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 1, 1, 1, 1, 1, 2, 2,
  2, 2, 2, 4, 4, 4, 4, 4, 8, 8
  };
*/
byte grid1[13] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 0, 0, 0, 0};
byte grid2[13] = {8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8};

byte Anode1[30] = {
  0, 0, 0, 0, 128, 0, 0, 0, 0, 128,// seconden
  2, 4, 32, 64, 129, 2, 4, 8, 16, 129,// minuten
  2, 4, 32, 64, 1, 2, 4, 8, 16, 1// uren
};

byte Anode2[30] = {
  1, 2, 16, 32, 0, 1, 2, 4, 8, 0,// seconden
  1, 2, 16, 32, 0, 1, 2, 4, 8, 0,// minuten
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // uren
};

byte cijferuur1[12] = {
  1, 2, 4, 8, 16, 32, 64, 128, 0, 0, 0, 0
};
byte cijferuur2[12] = {
  0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8
};
byte second;
byte minute;
byte hour;
byte uur;
byte ampm;
byte Test;
byte test1;
byte test2;
byte test3;

byte a = 0;
byte b = 0;
byte c = 0;
byte d = 0;
byte e = 0;
byte f = 0;
byte tijdg[15];
volatile int encoderPos = 0;
static uint8_t prevNextCode = 0;
static uint16_t store = 0;

byte oldButtonState = HIGH;  // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds
unsigned long buttonPressTime;  // when the switch last changed state
boolean buttonPressed = 0; // a flag variabl
volatile byte secondsInterrupt = 0;
//----------------------------------------------------------------------
void setup() {
  analogReference(INTERNAL);
  //set pins to output because they are addressed in the main loop
  pinMode(latchPinR, OUTPUT);
  pinMode(clockPinR, OUTPUT);
  pinMode(dataPinR, OUTPUT);
  pinMode(Filament, OUTPUT);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(Pin_LM35, INPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  pinMode(buttonPin, INPUT_PULLUP);
  //pinMode(CLKRD, INPUT);
  pinMode(CLKRD, INPUT_PULLUP);
  //pinMode(DATARD, INPUT);
  pinMode(DATARD, INPUT_PULLUP);
  Serial.begin(9600);
  Wire.begin();                 // begin I2C routine
  Wire.beginTransmission(0x68); // adress RTC DS1307 or DS3231
  Wire.write(byte(0x0E));       // address control register [0x0E for DS3231]
  Wire.write(byte(0x00));       // output SQ =1 Hz [0x00 for DS3231]
  Wire.endTransmission();       // einde I2C routine
  attachInterrupt(digitalPinToInterrupt(interruptPin), secondsRTC, FALLING);
  RTC.begin();
  RTC.setHourMode(CLOCK_H24);
  digitalWrite(Filament, LOW);
  delay(2000);
  digitalWrite(Filament, HIGH);
  TijdUitlezen();
  Cijferstest();

  delay(2000);
}
//------------------------------------------------------------------
void secondsRTC(void) {                 // *** ISR iedere seconde ***
  secondsInterrupt = 1;                 // ISR vlag = 1 : er was een 'seconds' interrupt.
}
//------------------------------------------------------------------------
void TijdUitlezen() {
  second = RTC.getSeconds();

  a = Eenh(second);       // eenheden
  b = Tiental(second);    // tientallen
  //RTC.setHourMode(CLOCK_H24);
  minute = RTC.getMinutes();
  c = Eenh(minute);       // eenheden
  d = Tiental(minute);;   // tientallen
  // uur uitlezen///
  hour = RTC.getHours();
  e = Eenh(hour);              // eenheden
  f = Tiental(hour);         // tientallen

}

// ------------------------------------------------------------------
void RotaryData() {
  static int8_t  val;

  if ( val = read_rotary() ) {
    //c += val;
    encoderPos += val;

    //Serial.print(encoderPos); Serial.print(" ");
    //RData = c;
    //if ( prevNextCode == 0x0b) {
    // Serial.print("eleven ");
    // Serial.println(store, HEX);
    //}

    // if ( prevNextCode == 0x07) {
    // Serial.print("seven ");
    //Serial.println(store, HEX);
    //}
  }
}
// --------------------------------------------------------------------
// A vald CW or  CCW move returns 1, invalid returns 0.
int8_t read_rotary() {
  static int8_t rot_enc_table[] = {0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0};

  prevNextCode <<= 2;
  if (digitalRead(DATARD)) prevNextCode |= 0x02;
  if (digitalRead(CLKRD)) prevNextCode |= 0x01;
  prevNextCode &= 0x0f;

  // If valid then store as 16 bit data.
  if  (rot_enc_table[prevNextCode] ) {
    store <<= 4;
    store |= prevNextCode;
    //if (store==0xd42b) return 1;
    //if (store==0xe817) return -1;
    if ((store & 0xff) == 0x2b) return -1;
    if ((store & 0xff) == 0x17) return 1;
  }
  return 0;
}
//-----------------------------------------
int Eenh(int waarde) {
  int test = waarde;
  test = waarde % 10;// % = remainder
  return test;
}
//---------------------------------------------
int Tiental(int waarde) {
  int test2 = waarde / 10;
  return test2;
}
//---------------------------------------------
void readtime() {

  second = RTC.getSeconds();
  // seconde uitlezen///
  a = Eenh(second);       // eenheden
  b = Tiental(second);    // tientallen

  if (second == 0) {
    minute = RTC.getMinutes();
    // minuut uitlezen///
    c = Eenh(minute);       // eenheden
    d = Tiental(minute);;   // tientallen
  }

  if (minute == 0 & second == 0) {
    hour = RTC.getHours();

    // uur uitlezen///
  }
  //Serial.print("tijd : ");
  //Serial.print(hour);
  //Serial.print(":");
  //Serial.println(minute);
}
//---------------------------------------------------------

void loop() {
  test1 = digitalRead(SW1);
  test2 = digitalRead(SW2);
  test3 = digitalRead(SW3);

  Test = test3 * 4;
  Test += test2 * 2;
  Test += test1;
  //Test = 0;

  if (secondsInterrupt == 1) {         // **** doe als ISR vlag = 1  ****
    readtime();                   // de subroutine die iedere seconde de tijd registers leest.
    secondsInterrupt = 0;            // zet ISR vlag terug naar '0'
  }

  // tijd();
  // delay(500);

  switch (Test) {
    case 0:
    digitalWrite(Filament, HIGH);
      tijd();

      break;
    case 1:
     digitalWrite(Filament, HIGH);
      TijdMetSec();

      break;
    case 2:
     digitalWrite(Filament, HIGH);
      TijdZonderSec();

      break;
    case 3:
     digitalWrite(Filament, HIGH);
      //============een keer per 3 seconden sensor uitlezen ===========================
      currentMillis = millis();
      if (currentMillis - previousMillis > interval)
      {
        previousMillis = currentMillis;

        temperatuur();
        //Serial.println(tempC);

      }
      tempdisp();
      break;
    case 4:
     digitalWrite(Filament, HIGH);
      AdjustU();

      break;
    case 5:
     digitalWrite(Filament, HIGH); 
      AdjustM();

      break;
    case 6:
      StandBy();
      break;
    case 7:
      Nachtmode();
      break;

  }

}
//-------------------------------------------------------------
void StandBy() {
 digitalWrite(Filament, LOW);
}
//-------------------------------------------------------------
void smu( byte eh, byte tt,  byte uur) {
  // smu = seconden, minuten, uren
  tt = tt * 2;
  if (eh >= 0 & eh < 3) {
    tijdg[1] = grid1[tt];//ic1
    tijdg[2] = grid2[tt];//ic2
  }
  else if (eh >= 3 & eh < 8) {
    tijdg[1] = grid1[tt + 1]; //ic1
    tijdg[2] = grid2[tt + 1]; //ic2
  }
  else if (eh >= 8) {
    tijdg[1] = grid1[tt + 2]; //ic1
    tijdg[2] = grid2[tt + 2]; //ic2
  }
  tijdg[3] = Anode1[eh + uur]; //ic3
  tijdg[4] = Anode2[eh + uur]; //ic4
  DataB(1, 2, 3, 4);
  DataB(0, 0, 0, 0);
}

//-------------------------------------------------------------
void uren(int minuutx) {
  // ------- uren ----------------------------
  int uur1;
  int mintot;
  int minuut;
  uur1 = hour;
  minuut = minuutx;
  ampm = 0;
  if (uur1 > 12) {
    uur1 = uur1 - 12;
    ampm = 1;
    DataB(5, 6, 7, 8);// dot aan
    DataB(0, 0, 0, 0);
  }
  mintot = (uur1 * 60) + minuut;
  mintot = mintot / 12;

  e = Eenh(mintot);              // eenheden
  f = Tiental(mintot);         // tientallen

  smu(e, f, 20);
}
//-------------------------------------------------------------
void tijd() {
  smu(a, b, 0);// seconden (0 = alleen buitenste wijzer)
  smu(c, d, 10);// minuten (10 = hele wijzer)
  uren(minute);
  Cijfers();

}


//-------------------------------------------------------------
void temperatuur() {
  int adcVal = analogRead(Pin_LM35);
  float milliVolt = adcVal * (1100 / 1024);
  tempC = milliVolt / 10;

}
//-------------------------------------------------------------
void tempdisp() {
  tmpeh = Eenh(tempC);       // eenheden
  tmptt = Tiental(tempC);;   // tientallen
  smu(tmpeh, tmptt, 10);

  tempC1 = tempC;
  tempC2 = ((tempC - tempC1) * 10);
  smu(tempC2, 0, 0);

  for (int i = 0; i <= 6; i++) {
    smu(0, i, 0);
  }
}
//-------------------------------------------------------------
void Cijfers() {

  for (byte i = 0; i <= 11; i++) {
    tijdg[9] = cijferuur1[i];
    tijdg[10] = cijferuur2[i];
    DataB(9, 10, 11, 12);
  }

  DataB(0, 0, 0, 0);
}
//-------------------------------------------------------------
void Cijferstest() {
  tijdg[0] = 0;
  tijdg[5] = 1;
  tijdg[6] = 0;
  tijdg[7] = 0;
  tijdg[8] = 128;
  tijdg[9] = 1;
  tijdg[10] = 0;
  tijdg[11] = 0;
  tijdg[12] = 64;
  DataB(5, 6, 7, 8);
  delay(2000);
  DataB(0, 0, 0, 0);
  for (byte i = 0; i <= 11; i++) {
    tijdg[9] = cijferuur1[i];
    tijdg[10] = cijferuur2[i];
    DataB(9, 10, 11, 12);
    delay(250);
  }

  DataB(0, 0, 0, 0);
  delay(1000);

  for (byte i = 0; i <= 59; i++) {
    a = Eenh(i);       // eenheden
    b = Tiental(i);    // tientallen
    smu(9 - a, 5 - b, 0);
    smu(a, b, 20);
    delay(30);
  }
  for (byte i = 0; i <= 59; i++) {
    a = Eenh(i);       // eenheden
    b = Tiental(i);    // tientallen
    smu(9 - a, 5 - b, 20);
    smu(a, b, 0);
    delay(30);
  }

}
//-------------------------------------------------------------
void TijdMetSec() {
  smu(a, b, 10);// seconden
  smu(c, d, 10);// minuten
  uren(minute);
  Cijfers();
}
//-------------------------------------------------------------
void TijdZonderSec() {
  smu(c, d, 10);// minuten
  uren(minute);
  Cijfers();
}
//-------------------------------------------------------------
void Nachtmode() {
  if (hour >= 0 & hour < 8) {
    digitalWrite(Filament, LOW);
  }
  else {
    digitalWrite(Filament, HIGH);
    tijd();
  }

}
//-------------------------------------------------------------
void DataB(byte a, byte b, byte c, byte d) {

  digitalWrite(latchPinR, LOW);
  shiftOut(dataPinR, clockPinR, MSBFIRST, tijdg[a]);
  shiftOut(dataPinR, clockPinR, MSBFIRST, tijdg[b]);
  shiftOut(dataPinR, clockPinR, MSBFIRST, tijdg[c]);
  shiftOut(dataPinR, clockPinR, MSBFIRST, tijdg[d]);
  digitalWrite(latchPinR, HIGH);
  delayMicroseconds(40);

}

//------------------------------------------------------------------------------------
void button() {
  byte buttonState = digitalRead (buttonPin);
  if (buttonState != oldButtonState) {
    if (millis () - buttonPressTime >= debounceTime) { // debounce
      buttonPressTime = millis ();  // when we closed the switch
      oldButtonState =  buttonState;  // remember for next time
      if (buttonState == LOW) {
        //Serial.println ("Button closed"); // DEBUGGING: print that button has been closed
        buttonPressed = 1;
      }
      else {
        ///Serial.println ("Button opened"); // DEBUGGING: print that button has been opened
        buttonPressed = 0;
      }
    }  // end if debounce time up
  } // end of state change
}

//-------------------------------------------------------------
void AdjustU() {
  TijdUitlezen();
  uur = hour;
  if (uur == 0) {
    uur = 12;
  }
  if (uur > 12) {
    uur = uur - 12;
  }
  if (a % 2 == 0) {
    uren(0);// wijzer op hele uur
  }
  tijdg[9] = cijferuur1[uur - 1];
  tijdg[10] = cijferuur2[uur - 1];
  tijdg[11] = 0;
  tijdg[12] = 64;
  DataB(9, 10, 11, 12);// uurcijfer aan
  DataB(0, 0, 0, 0);
  buttonPressed = 0;
  button();
  if (buttonPressed == 1) {
    AdjustUUR();
  }

}
//-------------------------------------------------------------
void AdjustUUR() {
  int exx;
  exx = 0;
  uur = hour;
  ampm = 0;

  if (uur >= 12) {
    uur = uur - 12;
    ampm = 1;
  }
  Displaykeuze1(uur);
  buttonPressed = 0;
  while (exx == 0) {
    RotaryData();

    //Serial.println (memC);

    Displaykeuze1(uur);// uur
    if ( encoderPos == 0) {
      smu(0, 0, 0);
    }

    //-----------------
    button();

    if ( encoderPos > 1) {
      uur = uur + 1;
      if (uur >= 12) {
        uur = uur - 12;
        ampm = 1;
      }
      Displaykeuze1(uur);// uur
      //Serial.println (uur);
      //delay(100);
      encoderPos = 1;

    }
    if (buttonPressed == 1 & encoderPos > 0) {
      if (ampm == 1) {
        hour = uur + 12;
      }
      else {
        hour = uur;
      }

      RTC.setHours(hour);
      Displaykeuze1(uur);// uur
      encoderPos = 0;
      buttonPressed = 0;
      exx = 1;
    }

    // --------------------------
    if ( encoderPos < -1) {
      uur = uur - 1;
      //Serial.println(uur);
      if (uur == 255) {
        uur = 12;
        ampm = 0;
      }
      Displaykeuze1(uur);// uur
      // Serial.println (uur);
      //delay(100);
      encoderPos = -1;

    }

    if (buttonPressed == 1 & encoderPos < 0) {
      if (ampm == 1) {
        hour = uur + 12;
      }
      else {
        hour = uur;
      }
      RTC.setHours(hour);
      Displaykeuze1(uur);// uur
      encoderPos = 0;
      buttonPressed = 0;
      exx = 1;
    }

    if (buttonPressed == 1 && encoderPos == 0) {
      //Serial.println("zonder save");
      exx = 1;
    }
  }
}
// ------------------------------------------------------
void Displaykeuze1(byte uur) {
  byte uu;

  uu = uur * 5;
  e = Eenh(uu);              // eenheden
  f = Tiental(uu);         // tientallen
  smu(e, f, 20);

  if (uur == 0) {
    uur = 12;
  }
  tijdg[9] = cijferuur1[uur - 1];
  tijdg[10] = cijferuur2[uur - 1];
  tijdg[11] = 0;
  tijdg[12] = 64;
  DataB(9, 10, 11, 12);// uurcijfer aan
  DataB(0, 0, 0, 0);

  if (ampm == 1) {
    DataB(5, 6, 7, 8);// dot aan
    DataB(0, 0, 0, 0);
  }

}

//-----------------------------------------------------------
void AdjustM() {
  TijdUitlezen();
  if (a % 2 == 0) {
    smu(c, d, 0);
    smu(c, d, 10);
  }
  buttonPressed = 0;
  button();
  if (buttonPressed == 1) {
    AdjustMIN();

  }
}
//-----------------------------------------------------------
void AdjustMIN() {
  int exx;
  Displaykeuze2();
  //delay(1000);
  buttonPressed = 0;
  exx = 0;
  while (exx == 0) {

    RotaryData();

    //Serial.println (memC);

    Displaykeuze2();// min
    if ( encoderPos == 0) {
      smu(c, d, 10);
    }
    //-----------------
    button();

    if ( encoderPos > 1) {
      minute = minute + 1;
      if (minute > 59) {
        minute = 0;
      }
      c = Eenh(minute);              // eenheden
      d = Tiental(minute);         // tientallen
      Displaykeuze2();// min
      //delay(100);
      encoderPos = 1;
    }
    if (buttonPressed == 1 & encoderPos > 0) {
      RTC.setMinutes(minute);
      second = 0;
      RTC.setSeconds(second);
      Displaykeuze2();// min
      encoderPos = 0;
      buttonPressed = 0;
      exx = 1;
    }

    // --------------------------
    if ( encoderPos < -1) {
      minute = minute - 1;
      //Serial.println(minute);
      if (minute == 255) {// een byte kan niet negatief worden, maar wordt dan 255
        minute = 59;
      }
      c = Eenh(minute);              // eenheden
      d = Tiental(minute);         // tientallen
      Displaykeuze2();// min
      //delay(100);
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos < 0) {
      RTC.setMinutes(minute);
      second = 0;
      RTC.setSeconds(second);
      Displaykeuze2();// min
      encoderPos = 0;
      buttonPressed = 0;
      exx = 1;
    }
    if (buttonPressed == 1 && encoderPos == 0) {
      //Serial.println("zonder save");
      exx = 1;
    }
  }
}

// ------------------------------------------------------
void Displaykeuze2() {

  smu(c, d, 0);
  //smu(c, d, 10);

}

Credits

jg53fn55

jg53fn55

0 projects • 4 followers
I am retired.

Comments