digitalWrite() is not working

I am using this seemingly standard way to shift bits serially into the MAX7219 register:

void Write_Max7219_byte(unsigned char DATA) 
{   
  unsigned char i;
  digitalWrite(Max7219_pinCS,LOW);  
  for(i=8;i>=1;i--)
  {    
    digitalWrite(Max7219_pinCLK,LOW);
    digitalWrite(Max7219_pinDIN,DATA&0x80);
    DATA = DATA<<1;
    digitalWrite(Max7219_pinCLK,HIGH);
  }                                 
}
 
void Write_Max7219(unsigned char address,unsigned char dat)
{
  digitalWrite(Max7219_pinCS,LOW);
  Write_Max7219_byte(address);          
  Write_Max7219_byte(dat);               
  digitalWrite(Max7219_pinCS,HIGH);
}

I have also tried this:

    shiftOut(Max7219_pinDIN, Max7219_pinCLK, MSBFIRST, address);
    shiftOut(Max7219_pinDIN, Max7219_pinCLK, MSBFIRST, dat);             

as seen in my wokwi simulator:
https://wokwi.com/projects/371484292346739713

Wokwi works as expected

Actual Development Board does not!

on the development board...

mx.setPoint(0, 1, true);

works fine!

I have debugged it into the Write_Max7219_byte function.
as well as using shiftOut.

so I believe it is my setup of the MAX7219 chip.

What does this tell us?
Reality is incorrect.

Or something else?

Did you configure those pins as outputs?

I checked the versions of the MD_MAX72XX.h library, and enforced 3.4.1 in wokwi and it still worked.

I have found the Documentation for MD_MAX72XX.h
https://majicdesigns.github.io/MD_MAX72XX/class_m_d___m_a_x72_x_x.html#a9c3c6ea52bfe61fc0279d9deb98a9e6e

So I will switch to the available library offering

However I am still wondering why I am unable to digitalWrite to the chip.

Yes I did.

initially I didn't and Wokwi did not care.

but I added

  pinMode(Max7219_pinCLK, OUTPUT);
  pinMode(Max7219_pinCS, OUTPUT);
  pinMode(Max7219_pinDIN, OUTPUT);

Anyway

please note the setPoint or setColumn works.

so I have removed this code altogether:

void Write_Max7219_byte(unsigned char DATA) 
{   
  unsigned char i;
  digitalWrite(Max7219_pinCS,LOW);  
  for(i=8;i>=1;i--)
  {    
    digitalWrite(Max7219_pinCLK,LOW);
    digitalWrite(Max7219_pinDIN,DATA&0x80);
    DATA = DATA<<1;
    digitalWrite(Max7219_pinCLK,HIGH);
  }                                 
}
 
void Write_Max7219(unsigned char address,unsigned char dat)
{
  digitalWrite(Max7219_pinCS,LOW);
  Write_Max7219_byte(address);          
  Write_Max7219_byte(dat);               
  digitalWrite(Max7219_pinCS,HIGH);
}

I have added these functions to my display.h code

void turnLedsOn(){
      mx.setColumn(rc1,C1);
      mx.setColumn(rc2,C2);
      mx.setColumn(rc3,C3);
      mx.setColumn(rc4,C4);
      mx.setColumn(rc5,C5);
      mx.setColumn(rc6,C6);
      mx.setColumn(rc7,C7);
      mx.setColumn(rc8,C8);
}

void turnSomeOff(){
      mx.setColumn(rc1,c1);
      mx.setColumn(rc2,c2);
      mx.setColumn(rc3,c3);
      mx.setColumn(rc4,c4);
      mx.setColumn(rc5,c5);
      mx.setColumn(rc6,c6);
      mx.setColumn(rc7,c7);
      mx.setColumn(rc8,c8);
}

And Changed my Looping Light flashing code to to use these 2 new methods:

  if(flashOn < flashTimer){
    if(flashOn == 0) {
//mx.displayText("A", PA_RIGHT, 2, 1, PA_RANDOM, PA_SLICE)
      updateGameBoard();
      turnLedsOn();
    }
    flashOn++;
    if(flashOn == flashTimer){
      flashOff=0;
    }
  }

// Turn off Flashing Leds and Wait
  if(flashOff < flashTimer){
    if(flashOff == 0) {
      updateGameBoard();
      turnSomeOff();
    }
    flashOff++;
    if(flashOff == flashTimer){
      flashOn=0;
    }
  }

effectively doing away with digitalWrite(); from my code.

It did screw up the order of the leads they are now upside down but that should be an easy fix

So my last Question/s
Why did the digitalWrite not work?
Does it have something to do with using the CLK pin on the Arduino (pin 13)?

First of all, what is your arduino board?

And next, please note, that the second parameter of digitalWrite() is not boolean.
Therefore using true and false as HIGH and LOW is a way for troubles.

Your line

is incorrect and may not works in some controllers.

Uno R3

What is this development board?

DATA&0x80
is a bitwise operator, not a boolean, but the result in my case would be a byte (I assume), and hence you are still most likely correct that it is neither HIGH nor LOW.

my byte with the &0x80
results in what appears to be a single MSD of 8 bits either 10000000 or 00000000

Are you suggesting that that 8th bit needs to become a LOW or a HIGH rather than a 0x80 or a 0x00?

Not to say you are wrong but what little I have read infers 0x80 is OK as a HIGH.

the reference guide is not conclusive though, it does not swing it either way, in fact quite ambiguous:

You are suggesting something more like this which I believe would be technically wasted effort.

  for(i=8;i>=1;i--)
  {    
    digitalWrite(Max7219_pinCLK,LOW);
    if( DATA&0x80 == 0x80){
        digitalWrite(Max7219_pinDIN,HIGH);
    }else{
        digitalWrite(Max7219_pinDIN,LOW);
    }
    DATA = DATA<<1;
    digitalWrite(Max7219_pinCLK,HIGH);
  }                                 

Thanks for the train of thought.
I will test it, and wait for other feedback.

did you not see my post prior to yours or are you asking what is an "Uno R3"

By development board I assumed you meant some other board that has the MAX7219 and other parts

@cassieninja

In setup(), set the clock low.
Then for each bit, do the following:

Output the data bit
Then bring the clock high
Wait 10us
Bring the clock low
Wait 10us

void Write_Max7219_byte(unsigned char DATA) 
{   
  unsigned char i;
  digitalWrite(Max7219_pinCLK,LOW);
  for(i=8;i>=1;i--)
  {    
    digitalWrite(Max7219_pinDIN,DATA&0x80);
    digitalWrite(Max7219_pinCLK,HIGH);
    delayMicroseconds(10);
    digitalWrite(Max7219_pinCLK,LOW);
    delayMicroseconds(10);
    DATA = DATA<<1;
  }                                 
}
 
void Write_Max7219(unsigned char address,unsigned char dat)
{
  digitalWrite(Max7219_pinCLK,LOW);
  digitalWrite(Max7219_pinCS,LOW);
  Write_Max7219_byte(address);          
  Write_Max7219_byte(dat);               
  digitalWrite(Max7219_pinCS,HIGH);
}

Yes, that can be a problem. But the UNO R3 is simple in this respect: a 0 sets the output low, any other value sets it high. But e.g. the Arduino Nano Every ( ATmega4809 ) is different:
0 -> output is set low
1-> output is set high
2 -> output toggles !!!
any other value sets the output high. I haven't checked it on other boards, but it should be safe to always set the parameter value to 1 ( HIGH) to set the output high.

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