DS3231 MAX7219 8digits

… or DS3231 part II 🙂 (see also part III and IV – the story goes on with interrupts)

The next step after testing if it works at all was to display ful time, and especially seconds. So 4 digits offered by TM1637 based module (let’s call it part I) was not enough. I used 8 digits 7 segment display based on MAX7219.

 

maxim-clock1

For the code, apart from DS3231 library mentioned in previous post, I used also Eberhard Fahle LedControl library.

The code for full 8 digits (including hundredts of seconds) and displaying via serial, how long does it take to refresh every 10 milisecond (1/100 second) the time:

#include "LedControl.h"
#include <Wire.h>
#include <DS3231.h>
/*
 Now we need a LedControl to work with.
 pin 12 is connected to the DataIn 
 pin 10 is connected to the CLK 
 pin 11 is connected to LOAD 
 */
LedControl lc=LedControl(12,10,11,1);
DS3231 Clock;

/* we always wait a bit between updates of the display */
unsigned long delaytime=10;
unsigned long m;

void ReadDS3231()
{
  static int oldsecond;
  static long oldmillis;   
  int second,minute,hour,hlast,mlast,slast,tens,hundreds;
  bool PM, h12; 
  second=Clock.getSecond();
  if ( second != oldsecond ) {
    oldmillis = millis();
    oldsecond = second; 
  }
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  slast = second % 10;
  mlast = minute % 10;
  hlast = hour % 10;
  second = (second - slast) / 10;
  minute = (minute - mlast) / 10;
  hour = (hour - hlast) / 10;
  tens = (millis()-oldmillis)/100;
  hundreds = ((millis()-oldmillis)/10)%10;
    lc.setDigit(0,7,hour,false);
    lc.setDigit(0,6,hlast,true);
    lc.setDigit(0,5,minute,false);
    lc.setDigit(0,4,mlast,true);
    lc.setDigit(0,3,second,false);
    lc.setDigit(0,2,slast,true);
    lc.setDigit(0,1,tens,false);
    lc.setDigit(0,0,hundreds,false);

}

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  Wire.begin(); 
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,6);
  /* and clear the display */
  lc.clearDisplay(0);
  Serial.begin(9600);
}

void loop() { 
  m=millis();
  ReadDS3231();
  Serial.println(millis()-m,DEC);
  delay(delaytime-m); //start with delaytime and change
    // to delaytime - m only once you assure m < delaytime
}

In my case m was almost all time equal 4, so we still had to delay a while to display next 0.01 second.
If you prefer to stick to full seconds and skip in-procedure time checking the code could be much shorter:

#include "LedControl.h"
#include <Wire.h>
#include <DS3231.h>
/*
 Now we need a LedControl to work with.
 pin 12 is connected to the DataIn 
 pin 11 is connected to LOAD 
 pin 10 is connected to the CLK
 */
LedControl lc=LedControl(12,10,11,1);
DS3231 Clock;

/* we always wait a bit between updates of the display */
unsigned long delaytime=1000;

void ReadDS3231()
{
  int second,minute,hour,hlast,mlast,slast;
  bool PM, h12; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  slast = second % 10;
  mlast = minute % 10;
  hlast = hour % 10;
  second = (second - slast) / 10;
  minute = (minute - mlast) / 10;
  hour = (hour - hlast) / 10;
    lc.setDigit(0,5,hour,false);
    lc.setDigit(0,4,hlast,true);
    lc.setDigit(0,3,minute,false);
    lc.setDigit(0,2,mlast,true);
    lc.setDigit(0,1,second,false);
    lc.setDigit(0,0,slast,false);
  delay(delaytime);
}

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  Wire.begin(); 
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,6);
  /* and clear the display */
  lc.clearDisplay(0);
}

void loop() { 
  ReadDS3231();
}

So this was a kind of intermediate step. In next one and fourth one we shall use timer interrupts. Go on.

 

3 thoughts on “DS3231 MAX7219 8digits”

Leave a Reply