Problems with MAX7219

Hello.
I have tried, unsuccessfully, to control some seven segment displays with MAX7219.
My problem is that I cannot light up more than three segments simultaneously.

The current code I am working with is this:

#include <SPI.h>

int SS2 = 10;  // need for SPI
// the digits the MAX7221 writes out
int tenths=0;  
int ones=0;  
int tens=0;  

// addresses for the MAX7221, and the values/ranges to write in

#define DECODE_MODE 0x09 // write data 0xFF, Code B Decode for all digits
#define INTENSITY_ADDRESS 0x0A // 0x07 to start, half intensity. valid from 0x00 (min) to 0x0F (max)
#define SCANLIMIT_ADDRESS 0x0B // 0xFF, all 8 digits on
#define SHUTDOWN_ADDRESS 0x0C  // 0x01, normal operation (0x01 = shutdown) - powers up in shutdown mode

#define DISPLAYTEST_ADDRESS 0x0F // 0x01 = all lights on full, 0x00 = normal ops
#define tenths 0x01 // digit 0, fill right hand byte with data to display
// data = 0-9, A='-', B='E', C='H', D='L', E='P', F=blank
#define ones 0x02 // digit 1
#define tens 0x03 // digit 2




void setup() // stuff that runs once before looping forever
{
  Serial.begin(9600);
  // start up SPI to talk to the MAX7221
  SPI.begin(); // nothing in () because we are the master
  pinMode(SS2, OUTPUT);  // Slave Select for SPI



  // put known values into MAX7221 so doesn't have weird display when actually turned on
  // 0x0F = blank digit
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(tenths);  // select the Address,
  SPI.transfer(0x0F);      // select the data
  digitalWrite(SS2,HIGH);  // take the SS pin high to de-select the chip



  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(ones);  // select the Address,
  SPI.transfer(0x0F);      // select the data
  digitalWrite(SS2,HIGH);   // take the SS pin high to de-select the chip



  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(tens);  // select the Address,
  SPI.transfer(0x0F);      // select the data
  digitalWrite(SS2,HIGH);   // take the SS pin high to de-select the chip  




  //  MAX7221: 
  //  write intensity register
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(INTENSITY_ADDRESS);  // select the Address,
  SPI.transfer(0x09);      // select the data
  digitalWrite(SS2,HIGH);   // take the SS pin high to de-select the chip:
  //Serial.println("intensity register ");

  // write scanlimit register
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(SCANLIMIT_ADDRESS);  // select the Address,
  SPI.transfer(0x07);      // select the data - FF = all 8 digits, 02 only 3 digits
  digitalWrite(SS2,HIGH);   // take the SS pin high to de-select the chip:
  //Serial.println("scanlimit register ");

  // write decode register
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(DECODE_MODE);  // select the Address,
  SPI.transfer(0x00);      // select the data - FF = all 8 digits               <<< change this if you only use 3 digits
  digitalWrite(SS2,HIGH);   // take the SS pin high to de-select the chip:
  //Serial.println("decode register ");



  // write shutdown register for normal display operations
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(SHUTDOWN_ADDRESS);  // select the Address,
  SPI.transfer(0x01);      // select the data, 0x01 = Normal Ops
  digitalWrite(SS2,HIGH);   // take the SS pin high to de-select the chip:
  //Serial.println("shutdown register, displays on ");

}


void loop () {

  // 0x0F = blank digit
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(tenths);  // select the Address,
  SPI.transfer(0b00000001);      // select the data
  digitalWrite(SS2,HIGH);  // take the SS pin high to de-select the chip
  delay(500);


  // 0x0F = blank digit
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(tenths);  // select the Address,
  SPI.transfer(0b01100000);      // select the data
  digitalWrite(SS2,HIGH);  // take the SS pin high to de-select the chip
  delay(500);




   // 0x0F = blank digit
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  SPI.transfer(tenths);  // select the Address,
  SPI.transfer(0b00110001);      // select the data
  digitalWrite(SS2,HIGH);  // take the SS pin high to de-select the chip
  delay(500);

}

I have found this code somewhere on a really old thread, on this same forum.

I have also tried the LedControl library, which didn't work at all

I tested the displays, individually and with the display test functionality of the MAX7219.

The moment I send to MAX a number with more than three bits set, it flashes the correct display and then immediately stops responding to inputs and a reset is needed

as a start with the max7219 I would begin with the well known and working library "LedControl". You can install it via the library manager in the IDE.

If that library is working and your main goal is to display numbers on the display, I recommend my adopted Noiasca LedControl

post the link to the code you found.
You can try this loop() (untested) :

void loop () {

  // 0x0F = blank digit
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  // SPI.transfer(tenths);  // select the Address,
  SPI.transfer( (byte) 1 );  // select the Address,
  SPI.transfer(0b00000001);      // select the data
  digitalWrite(SS2,HIGH);  // take the SS pin high to de-select the chip
  delay(500);


  // 0x0F = blank digit
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  // SPI.transfer(tenths);  // select the Address,
  SPI.transfer( (byte) 2 );  // select the Address,
  SPI.transfer(0b01100000);      // select the data
  digitalWrite(SS2,HIGH);  // take the SS pin high to de-select the chip
  delay(500);


   // 0x0F = blank digit
  digitalWrite(SS2,LOW);  // take the SS pin low to select the chip:
  // SPI.transfer(tenths);  // select the Address,
  SPI.transfer( (byte) 3 );  // select the Address,
  SPI.transfer(0b00110001);      // select the data
  digitalWrite(SS2,HIGH);  // take the SS pin high to de-select the chip
  delay(500);

}

However, you'd be better off attempting to get a standard library to work

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.