Go Down

Topic: Max7221 Sketch Question (Read 677 times) previous topic - next topic

SteveOr

The Arduino Cookbook has the following sketch for controlling 7 segment displays with the Max7221:

Code: [Select]
/*
Max7221_digits
*/
#include <SPI.h> // Arduino SPI library introduced in Arduino version 0019
const int slaveSelect = 10; //pin used to enable the active slave
const int numberOfDigits = 2; // change these to match the number of digits
wired up
const int maxCount = 99;
int number = 0;
void setup()
{
   Serial.begin(9600);
   SPI.begin(); // initialize SPI
   pinMode(slaveSelect, OUTPUT);
   digitalWrite(slaveSelect,LOW); //select slave
   // prepare the 7221 to display 7-segment data - see data sheet
   sendCommand(12,1); // normal mode (default is shutdown mode);
   sendCommand(15,0); // Display test off
   sendCommand(10,8); // set medium intensity (range is 0-15)
   sendCommand(11,numberOfDigits); // 7221 digit scan limit command
   sendCommand(9,255); // decode command, use standard 7-segment digits
   digitalWrite(slaveSelect,HIGH); //deselect slave
}
void loop()
{
   // display a number from the serial port terminated by the end of line
character
   if(Serial.available())
   {
      char ch = Serial.read();
      if( ch == '\n')
      {
         displayNumber(number);
         number = 0;
      }
      else
      number = (number * 10) + ch - '0'; // see Chapter 4 for details
   }
}
// function to display up to four digits on a 7-segment display
void displayNumber( int number)
{
   for(int i = 0; i < numberOfDigits; i++)
   {
      byte character = number % 10; // get the value of the rightmost decade
      if(number == 0 && i > 0)
         character = 0xf; // the 7221 will blank the segments when receiving value
      // send digit number as command, first digit is command 1
      sendCommand(numberOfDigits-i, character);
      number = number / 10;
   }
}
void sendCommand( int command, int value)
{
   digitalWrite(slaveSelect,LOW); //chip select is active low
   //2-byte data transfer to the 7221
   SPI.transfer(command);
   SPI.transfer(value);
   digitalWrite(slaveSelect,HIGH); //release chip, signal end transfer
}


My question is in regards to this portion of the sketch:

Code: [Select]
// prepare the 7221 to display 7-segment data - see data sheet
   sendCommand(12,1); // normal mode (default is shutdown mode);
   sendCommand(15,0); // Display test off
   sendCommand(10,8); // set medium intensity (range is 0-15)
   sendCommand(11,numberOfDigits); // 7221 digit scan limit command
   sendCommand(9,255); // decode command, use standard 7-segment digits


It says to reference the Max7221 datasheet (http://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf) regarding the registers used in these commands.  I sort of understand this but the datasheet references Hex commands and such which doesn't directly translate to the Arduino Cookbook sendcommands.  Can someone help me on this?

Jack Christensen

#1
Nov 26, 2012, 08:49 pm Last Edit: Nov 26, 2012, 09:14 pm by Jack Christensen Reason: 1
The sendCommand() function sends two bytes to the MAX7221. First is the command (or register address, really) and second is the value to be placed in that register. Both can be coded as decimal or hex values. The following are equivalent.

Code: [Select]
// prepare the 7221 to display 7-segment data - see data sheet
  sendCommand(12, 1); // normal mode (default is shutdown mode);
  sendCommand(15, 0); // Display test off
  sendCommand(10, 8); // set medium intensity (range is 0-15)
  sendCommand(11, numberOfDigits); // 7221 digit scan limit command
  sendCommand(9, 255); // decode command, use standard 7-segment digits


Code: [Select]
// prepare the 7221 to display 7-segment data - see data sheet
  sendCommand(0x0C, 1); // normal mode (default is shutdown mode);
  sendCommand(0x0F, 0); // Display test off
  sendCommand(0x0A, 8); // set medium intensity (range is 0-15)
  sendCommand(0x0B, numberOfDigits); // 7221 digit scan limit command
  sendCommand(0x09, 0xFF); // decode command, use standard 7-segment digits


Edit: This may be more readable. I've left most of the comments but they are perhaps less necessary, since cryptic numeric values that don't mean a whole lot are replaced by readable symbols that give a clue what they're about.

Code: [Select]
#define MAX72_DECODE 0x09
#define MAX72_INTENSITY 0x0A
#define MAX72_SCANLIMIT 0x0B
#define MAX72_ENABLE 0x0C
#define MAX72_DISPLAYTEST 0x0F

// prepare the 7221 to display 7-segment data - see data sheet
   sendCommand(MAX72_ENABLE, 1);                  //chip/display enabled
   sendCommand(MAX72_DISPLAYTEST, 0);             //display test off
   sendCommand(MAX72_INTENSITY, 8);               //medium intensity (range is 0-15)
   sendCommand(MAX72_SCANLIMIT, numberOfDigits);
   sendCommand(MAX72_DECODE, 0xFF);               //standard 7-segment decoding
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

SteveOr

Terrific.  That clarifies things.  Thanks.

Go Up