1
\$\begingroup\$

I have a MAX7221 SPI 7-seg. LED driver linked to an AVR168P microcontroller.

Sometimes when powering on, everything works as expected. Other times the display blanks and remains blank. Other times the display illuminates all digits and remains "stuck" like that.

Can anyone give me any pointers as to what I could try to fix this?

Schematic

Layout (The 10K pull-up on CS is off-board - closer to the uC)

SCK clock pulse

My code:

.MACRO spiDeselect
    SBI PORTB, pinCS3
    SBI PORTB, pinCS2
    SBI PORTB, pinCS1
.ENDMACRO

.MACRO spiSelect2
    SBI PORTB, pinCS3
    SBI PORTB, pinCS1
    CBI PORTB, pinCS2
.ENDMACRO

.MACRO spiOut ; value in portReg
    OUT SPDR0, portReg
spiOutWait:
    IN portReg, SPSR0 ; Can't use SBIS for port > 31
    ANDI portReg, (1 << SPIF0)
    BREQ spiOutWait
.ENDMACRO

.MACRO setupSpi
    ; Prevent SlaveSelect acting as an input
    ; or it'll force SPI into slave mode
    LDI portReg, (1 << pinSS) | (1 << pinCS1) | (1 << pinCS2) | (1 << pinCS3) | (1 << pinBlink)
    OUT DDRB, portReg
    SBI PORTB, pinSS
    SBI PORTB, pinBlink
    LDI portReg, (1 << SPE0) | ( 1 << MSTR0 ) ;| spiDivide128
    OUT SPCR0, portReg
    ; Setup SCK and MOSI pins AFTER enabling SPI, to avoid
    ; accidentally clocking in a single bit
    SBI DDRB, pinMOSI
    SBI DDRB, pinSCK
    spiDeselect
.ENDMACRO

.MACRO max7221SetRegister
    spiSelect2
    MOV portReg, regReg ; MAX7221 register to set
    spiOut
    MOV portReg, valReg ; value to set
    spiOut
    spiDeselect
.ENDMACRO

.EQU Max7221RegisterDecodeMode=0x09
.EQU Max7221RegisterIntensity=0x0A
.EQU Max7221RegisterScanLimit=0x0B

.MACRO setupMax7221
    LDI valReg, 0
    LDI regReg, Max7221RegisterDecodeMode
    max7221SetRegister
    LDI valReg, 0x2
    LDI regReg, Max7221RegisterIntensity
    max7221SetRegister
    LDI regReg, Max7221RegisterScanLimit
    LDI valReg, 7 ; display 8 digits
    max7221SetRegister
    LDI regReg, Max7221RegisterShutdown
    LDI valReg, 1 ; shutdown mode = 0
    max7221SetRegister
.ENDMACRO

progStart:
    CLI
    setupStackAndReg
    setupSpi
    setupMax7221
................

Thanks.

\$\endgroup\$
  • \$\begingroup\$ Without knowing much about the 7221, it sounds like a timing (or reset) issue. Can you slow your setup code down some - see if adding some delay in stabilizes the startup? \$\endgroup\$ – mike65535 Dec 19 '18 at 14:12
  • 2
    \$\begingroup\$ Have you checked your setup, hold and clock timing. Scope the SPI. The ATmega can run at 20MHz, with two cycles per instruciton. The MAX7221 is max 10MHz. \$\endgroup\$ – Jason Morgan Dec 19 '18 at 17:36
  • 1
    \$\begingroup\$ Following Damien's request for a board-layout made me think "it looks like there's A LOT of stray capacitance round here and the routing of the clock isn't very good either" could make @JasonMorgan 's point about clock division even more important. Anyway, I divided the SPI clock by 128 (the maximum division) and that seems to have fixed my issues. I'm going to try some smaller divisions and see how that works out. \$\endgroup\$ – Andy Preston Dec 21 '18 at 12:29
  • 1
    \$\begingroup\$ Seems running SPI with the default clock divider of 4 was just too quick for either the MAX7221 or my shoddy board layout... It seems if I go one divider up and use clock/16 everything seems fine. \$\endgroup\$ – Andy Preston Dec 21 '18 at 12:45
  • 1
    \$\begingroup\$ Add a 10R resistor at the output of the clock pin, this will remove some ringing if there is any and make little difference is there is none. \$\endgroup\$ – Jason Morgan Dec 21 '18 at 13:42
3
\$\begingroup\$

The MAX7221 / MAX7219 datasheet specifies that

To minimize power-supply ripple due to the peak digit driver currents, connect a 10µF electrolytic and a 0.1µF ceramic capacitor between V+ and GND as close to the device as possible.

But the demo breadboard circuit that Kevin Darrah assembles in This YouTube video uses a higher value 100µF electrolytic.

I've always thought this is a power problem (a hunch rather than any real engineering know-how).

I tried Kevin's capacitor and it worked.

\$\endgroup\$
  • \$\begingroup\$ This still doesn't quite work. All but the "worst" power supplies now work. And the "worst" is improved somewhat but putting yet another 100µF across the power rails themselves. But it still doesn't always come on. I'll try a bigger cap on the power rails later and see if this story still has a "........ to be continued" \$\endgroup\$ – Andy Preston Jan 16 at 10:00
  • 1
    \$\begingroup\$ I'll just add the comment that some (many? most?) of the cheap Ebay / AliExpress etc. MAX7221 and similar ICs, seem to be counterfeit. I remember finding a web page where one was decapped (might have been this one) and a completely different die was found inside. They seem to be a good emulation, but are not the genuine IC. So if the source of your IC was anything other than definitely legitimate, then you might be unable to use the real datasheet parameters / comments etc. to help. This might even be relevant for the original problem :-( \$\endgroup\$ – SamGibson Jan 16 at 15:54

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.