Arduino Uno SPI Master sending incorrect data to MAX7219

For general Flowcode discussion that does not belong in the other sections.
Post Reply
Bijumon
Posts: 28
http://meble-kuchenne.info.pl
Joined: Fri Dec 30, 2022 11:44 am
Has thanked: 3 times
Been thanked: 10 times

Flowcode v11 Arduino Uno SPI Master sending incorrect data to MAX7219

Post by Bijumon »

Hello,

I am testing the SPI Master component with an Arduino Uno to drive a MAX7219 LED driver, But SPI bus sending incorrect data.
i have tested both hardware and software channel.
For example, when I send 0x09, the SPI debugger in Proteus shows 0x84.
Proteus debugger screenshot is attached.
Attachments
SPI.jpg
SPI.jpg (46.51 KiB) Viewed 90 times
MAX7219 .fcfx
(11.75 KiB) Downloaded 6 times

BenR
Matrix Staff
Posts: 1989
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 523 times
Been thanked: 710 times

Re: Arduino Uno SPI Master sending incorrect data to MAX7219

Post by BenR »

Hello,

SPI has a number of modes which dictate things like which phase of the clock the data is sampled and the state of the clock when idle.

It looks like Flowcode and Proteus are simply using different settings and hence getting different values.

0x09 = 0b00001001

0x84 = 0b10000100 0x80 = 0b10000000

So it's the same data just shifted by one bit position due to the clock edge configuration mismatch.

Bijumon
Posts: 28
Joined: Fri Dec 30, 2022 11:44 am
Has thanked: 3 times
Been thanked: 10 times

Flowcode v11 Re: Arduino Uno SPI Master sending incorrect data to MAX7219

Post by Bijumon »

Hi BenR,

Thank you for your prompt reply.

I first tested the program on real hardware (Arduino Uno), but it did not work.
To investigate, I ran the same program in Proteus for simulation and debugging.

Then I tried the exact same Flowcode program using a PIC16F18877 target, and the simulation worked correctly in Proteus (I don’t have real PIC hardware for testing).

Please find the PIC16F18877 Proteus debugger screenshot attached for your reference.

Thanks....
Attachments
Screenshot 18877.jpg
Screenshot 18877.jpg (34.6 KiB) Viewed 73 times

Bijumon
Posts: 28
Joined: Fri Dec 30, 2022 11:44 am
Has thanked: 3 times
Been thanked: 10 times

Re: Arduino Uno SPI Master sending incorrect data to MAX7219 (Solved)

Post by Bijumon »

Hi BenR,

I finally managed to get it working by using direct C code to initialize the SPI, instead of the SPI Initialize component macro.
Now everything works fine!

Here’s the code I used:

// --- SPI pins setup ---
DDRB |= (1 << PB3) | (1 << PB5) | (1 << PB2); // MOSI, SCK, SS as output
DDRB &= ~(1 << PB4); // MISO as input
PORTB |= (1 << PB2); // SS high (inactive)
// --- SPI Control Register setup ---
// SPE = 1 → Enable SPI
// MSTR = 1 → Master mode
// CPOL = 0, CPHA = 0 → SPI mode 0 (clock idle low, sample on rising edge)
// SPR1:0 = 01 → fosc/16 (safe speed for MAX7219)
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);


Thanks!

Post Reply