I was just playing around today and thought I'd write an Arduino Uno program that increments a timer displayed on a MAX7219 7-Segment Display.
It was a pretty straight forward process. I had to use the SPI library because none of the MAX7219 libraries that I found would work with my device.
Here is the source code. I used the online code editor from Arduino to develop my code in. It was super easy.
/*
*/
#include <SPI.h>
const int slaveSelectPin = 6;
// define 0-9 for the LED segments
const int digits[] = { 0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f, 0x70, 0x7f, 0x7b };
const int digitsdecimal[] = { 0x1fe, 0xb0, 0xed, 0xf9, 0xb3, 0xdb, 0xdf, 0xf0, 0xff, 0xfb};
int seconds = 0;
int minutes = 0;
void setup() {
// set the slaveSelectPin as an output:
pinMode(slaveSelectPin, OUTPUT);
digitalWrite(slaveSelectPin, HIGH);
SPI.begin();
initializeMax7219();
}
void loop() {
seconds++;
if(seconds == 599)
{
seconds = 0;
minutes++;
if(minutes == 10)
{
minutes = 0;
}
}
updateDisplay(minutes, seconds);
delay(100);
}
void updateDisplay(int minutes, int seconds)
{
// get our individual digit values
int tenths = seconds % 10;
int secsOnes = seconds / 10 % 10;
int secsTens = seconds / 100 % 10;
int mins = minutes % 10;
// update the first digit with our ones place value
sendMessage(0x01, digits[tenths]);
// update our second digit with our tens place value
sendMessage(0x02, digitsdecimal[secsOnes]);
// update the third digit with our hundreds place value
sendMessage(0x03, digits[secsTens]);
// update our fourth digit with our thousands place value
sendMessage(0x04, digitsdecimal[mins]);
}
void initializeMax7219()
{
// turn off test mode
sendMessage(0x0F, 0);
// set operation mode to normal
sendMessage(0x0C, 1);
// set scan limit (number of digits)
sendMessage(0x0B, 0x03); // use 4 digits
// set brightness to 100%
sendMessage(0x0A, 0x0F);
// set no decode mode
sendMessage(0x09, 0);
}
void sendMessage(int address, int data)
{
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE3));
digitalWrite(slaveSelectPin, LOW);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(data);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
SPI.endTransaction();
}
It only counts up to 9 minutes and 59.9 seconds until it resets back to zero. With a few modifications, you can add more minutes, and even hours. I'll leave that as an exercise for the reader.
Here is the datasheet for the MAX7219.
There's probably a better way to do clock math involving a mod on 60 and dividing by 60, but this one worked with my tenth of a second code.
Pin 6 is connected to the CS pin on the MAX7219 module, pin 11 to the DIN, and pin 13 to the CLK.