Max7219, DS3231 RTC Clock

Hi All,

I am trying to make a 6 digit clock for a retirement present for a work colleague / mentor who is retiring. The plan is to eventually incorporate the clock into an old piece of test equipment.

After advice from the forum I am using a DS3231 RTC Module combined with a MAX7219 IC chip with two common cathode 4x7segment displays. The plan being that once I have got the code working I will then change the displays for 2x7segments to mount into the test equipment.

I have limited knowledge of coding after teaching myself enough for a project at Uni 10 years ago and am therefore rusty to say the least.

So far I have got the code working to set the RTC and can see this working over the serial link as it updated every second;

#include <config.h>
#include <ds3231.h>
#include <Wire.h>

struct ts t; 

void setup() {
  Serial.begin(9600);
  Wire.begin();

  t.hour=12; 
  t.min=30;
  t.sec=0;
  t.mday=25;
  t.mon=12;
  t.year=2019;
   
  DS3231_set(t);
}

void loop() {
 DS3231_get(&t);
  Serial.print("Date : ");
  Serial.print(t.mday);
  Serial.print("/");
  Serial.print(t.mon);
  Serial.print("/");
  Serial.print(t.year);
  Serial.print("\t Hour : ");
  Serial.print(t.hour);
  Serial.print(":");
  Serial.print(t.min);
  Serial.print(".");
  Serial.println(t.sec);
 
  delay(1000);
}

When I try to combine the above code with some code I have found for from the LedControl Library it wants to count in Hex-Decimal and will only display the smallest bit of the number i,e instead of 12 it shows “c”

#include <LedControl.h>
#include <config.h>
#include <ds3231.h>
#include <Wire.h>

struct ts t; 
// identifying pins for the Max7219 & 7seg display
LedControl lc=LedControl(12,11,10,1);

unsigned long delaytime=250;

void setup() {
  // Setup for the serial monitor and setting clock time:
  Serial.begin(9600);
  Wire.begin();

  t.hour=12; 
  t.min=59;
  t.sec=0;
  t.mday=25;
  t.mon=12;
  t.year=2019;
   
  DS3231_set(t);

  // Wakeup and settings for the 7 segment display:
  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
}

void printtime(){
  DS3231_get(&t);
  Serial.print("Date : ");
  Serial.print(t.mday);
  Serial.print("/");
  Serial.print(t.mon);
  Serial.print("/");
  Serial.print(t.year);
  Serial.print("\t Time : ");
  Serial.print(t.hour);
  Serial.print(":");
  Serial.print(t.min);
  Serial.print(".");
  Serial.println(t.sec);
 
  delay(1000);  
}

void displaytime(){
    lc.setDigit(0,5,t.hour,false);
    lc.setDigit(0,3,t.min,false);
    lc.setDigit(0,1,t.sec,false);
  
}
void loop() {
 printtime();
 displaytime();
}

I know I am doing something wrong but cant put my finger on what I need to do to rectify this - Although its probably something simple and I could well be barking up the wrong tree with my code.

Can anyone please shed some light on my issue or point me in the right direction?

Many thanks in advance
Tris

Have you read all the LEDControl documentation?

void displaytime(){
    lc.setDigit(0,5,t.hour / 10,false);
    lc.setDigit(0,4,t.hour % 10,false);
    lc.setDigit(0,3,t.min / 10,false);
    lc.setDigit(0,2,t.min % 10,false);
    lc.setDigit(0,1,t.sec / 10,false);
    lc.setDigit(0,0,t.sec % 10,false);
 
}

Please modify your post above and use code tags (not quote tags) for code, as I just did.

PaulRB:

void displaytime(){

lc.setDigit(0,5,t.hour / 10,false);
   lc.setDigit(0,4,t.hour % 10,false);
   lc.setDigit(0,3,t.min / 10,false);
   lc.setDigit(0,2,t.min % 10,false);
   lc.setDigit(0,1,t.sec / 10,false);
   lc.setDigit(0,0,t.sec % 10,false);

}



Please modify your post above and use code tags (not quote tags) for code, as I just did.

PaulRB - Thanks for the help, Its much appreciated. Apologies re the Code & Quote tags, All edited now.

From playing around with the power and coding I am not 100% sure that I have got the coding correct for the RTC as when I put I power cycle the board it reverts back to 12:30:00. My first thought was that as I had the code specifying the Time in the Setup loop this was the issue, but after removing this from the setup I have found that this is not the case.

Edit - By also removing the “DS3231_set(t);” from the setup loop it hold the correct time after removing the power.

now to try and incorporate a couple of switched to change the time manually for when there is a clock change or incase the batteries go flat.

+1 karma for fixing the code tags.

So… does the time display correctly now? If so, do you understand why, and what was going on in your previous attempt?

but after removing this from the setup I have found that this is not the case.

Are you sure you uploaded the code successfully after removing those lines? RTC which are faulty or have dead/missing battery default to midnight, not 12:30.

PaulRB:
So.... does the time display correctly now? If so, do you understand why, and what was going on in your previous attempt?

The time is displaying correctly now. I believe that the original coding was trying to display the Hour, Min & Seconds, each on one one seven segment display. As this is not possible it counted 0-9 and then from A-F in Hex.

By using your suggestions it divides the figure by 10 and displays the values of 0-9 on each display.

Are you sure you uploaded the code successfully after removing those lines? RTC which are faulty or have dead/missing battery default to midnight, not 12:30.

I made the mistake of forgetting to remove "DS3231_set(t)" the part of the setup coding which was reverting the clock back to a preset value. After removing this clock holds the time and continues counting when the power is removed.

All correct, excellent.