Ok, now we have everything connected. Is time for the code. It is quite simple but a bit long. We have to inport the libraries for the DS3231 real time clock and define the SDA and SCL pins. We also define the pins for the CLK, DATA and CS for the MAX7219 driver. In this case those will be D2 D3 and D4. In the setup loop we start the RTC module and define the MAX7219 pins as outputs.
rtc.begin(); //We start the real time module pinMode(MAX7219_Data_IN, OUTPUT); //Define the data pin for the MAX7219 as output pinMode(MAX7219_Chip_Select, OUTPUT); //Define the cs pin for the MAX7219 as output pinMode(MAX7219_Clock, OUTPUT); //Define the clock pin for the MAX7219 as output digitalWrite(MAX7219_Chip_Select, HIGH); //We set the CS to high, CS is negative enabeled
Download the full code from above. Read all the comments in the code line by line to understand more. But the code dose this: We read the real time from the module using i2c communication. We pass the hour and minute from String to integer. Then we use then shift function to pass the numbers to the 7 segment displays and by that show the hour and minute. If the buttons are presed we increase the hour and minute by 1. Before, make sure you set the time to the RTC using the library example.
Below you ahve the config of thr 7 segment displays. Change those if you want to reduce or increase the brightness or any other settings. That's it. Compile, select Arduino NANO board, select the COM and uplaod the code. Now the clock should work and display time.
shift(0x0f, 0x00); //display test register - test mode off shift(0x0c, 0x01); //shutdown register - normal operation shift(0x0b, 0x04); //scan limit register - display digits 0 thru 7 shift(0x0a, 0x01); //intensity register - Min brightness: 0x01, Max brightness: 0x0f shift(0x09, 0xff); //decode mode register - CodeB decode all digits shift(0x01, 0xff); //Turn off all segments shift(0x02, 0xff); shift(0x03, 0xff); shift(0x04, 0xff);