public
Last active

Basic code for using Maxim MAX7219/MAX7221 with Arduino.

  • Download Gist
max7219_max7221.ino
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*
Basic code for using Maxim MAX7219/MAX7221 with Arduino.
 
Wire the Arduino and the MAX7219/MAX7221 together as follows:
 
| Arduino | MAX7219/MAX7221 |
| --------- | --------------- |
| MOSI (11) | DIN (1) |
| SCK (13) | CLK (13) |
| I/O (7)* | LOAD/CS (12) |
 
* - This should match the LOAD_PIN constant defined below.
For the rest of the wiring follow the wiring diagram found in the datasheet.
Datasheet: http://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf
 
Author: Nicholas Dobie <nick@nickdobie.com>
Date: 30 December 2013
License: WTFPL (http://www.wtfpl.net/)
*/
#include <SPI.h>
 
// What pin on the Arduino connects to the LOAD/CS pin on the MAX7219/MAX7221
#define LOAD_PIN 7
 
/**
* Transfers data to a MAX7219/MAX7221 register.
*
* @param address The register to load data into
* @param value Value to store in the register
*/
void maxTransfer(uint8_t address, uint8_t value) {
 
// Ensure LOAD/CS is LOW
digitalWrite(LOAD_PIN, LOW);
 
// Send the register address
SPI.transfer(address);
 
// Send the value
SPI.transfer(value);
 
// Tell chip to load in data
digitalWrite(LOAD_PIN, HIGH);
}
 
void setup() {
// Set load pin to output
pinMode(LOAD_PIN, OUTPUT);
 
// Reverse the SPI transfer to send the MSB first
SPI.setBitOrder(MSBFIRST);
// Start SPI
SPI.begin();
 
// Run test
// All LED segments should light up
maxTransfer(0x0F, 0x01);
delay(1000);
maxTransfer(0x0F, 0x00);
// Enable mode B
maxTransfer(0x09, 0xFF);
// Use lowest intensity
maxTransfer(0x0A, 0x00);
// Only scan one digit
maxTransfer(0x0B, 0x00);
// Turn on chip
maxTransfer(0x0C, 0x01);
}
 
void loop() {
// Loop through each code
for (uint8_t i = 0; i < 0x10; ++i)
{
maxTransfer(0x01, i);
delay(500);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.