Hi.
I am building a digital clock using ATmega328P with DS1307 and bunch of shift registers for the segments display.
I store the data of the LED matrix in a 32bit x 16 array, then shift out one row per loop. The whole display requires 16 loops to refresh.
My code structure looks like this:
#include <Wire.h>
#include <SPI.h>
#inlclude <EEPROM.h>
#include "RTClib.h"
#define UPDATE_INTERVAL 1000000
//Global variables
RTC_DS1307 rtc;
static uint32_t update_ref_time;
static DateTime time_utc;
static uint32_t matrix_data[16];
void setup() {
//Set pin, SPI, RTC etc.
}
void loop() {
static row = 0;
uint32_t microsec = micros();
if (microsec - update_ref_time > UPDATE_INTERVAL) {
update_ref_time = microsec;
time_utc = rtc.now(); //Update time from RTC every UPDATE_INTERVAL
}
update_display_data(); //Update display data
some_other_functions(); //Scan buttons, check_alarm etc.
refresh_row(row); //Shift out the row
row = (row < 15) ? row + 1 : 0;
//Print loop exec time
Serial.println(micros() - microsec, DEC);
}
The code works, but the display is flickering.
The loop executed without "time_utc = rtc.now();" takes around 430 us, but the loop with it takes about 1480 us. The RTC update blocks the scanning of display which causes noticeable brightness bump on some random segments.
I tried setting the UPDATE_INTERVAL to 0, so all the loops would take approximately 1480 us, the flikering did disappear but the display was flashing because the refresh rate was too low.
I also tried scanning all row in one loop like
void loop() {
time_utc = rtc.now();
update_display_data();
some_other_functions();
scan_all_16_rows();
}
But the LEDs in the last row was much brighter than the others since the loop was always blocked after last row scan.
How can I solve the problem?
Thank you!
Update: I attached the WIP code. I am a beginner in programming so my code is very nasty.
And here's a photo of the staff I've been workin on
DigitalClock.zip (11.4 KB)