1
\$\begingroup\$

I'm trying to use an ATmega32 to communicate to a MAX7219 chip for multiplexing with an led matrix. However, I do have multiple different devices that I want to communicate with.

I am trying to communicate with the device without actually using the SPI pins provided on the microcontroller. I have made a test project, but nothing is working. At first it seemed there was something wrong with my code. I've been on other forums and people have shown me better code for doing the job, but still I can't seem to get this working. I've checked all my wiring and even connected it to an Arduino and it ran perfectly. So now I'm thinking it has something to do the the ATmega. Do any pins need pull-up or pull-down resistors on them? Or is there maybe something else that is the issue?

enter image description here

here is just a lazy test. Not even this works, test nor shutdown. simple test code:

//test

#include <avr/io.h>

int main(void)
{
DDRB = 0b00000111; // pin 1, 2 and 3 are outputs
/*
//display test
PORTB = 1 << PINB0; // data pin 1 is high
PORTB = 0 << PINB1; // clock pin 2 is low
PORTB = 0 << PINB2; // latch pin 3 is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

// next 8 bits

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB2; // latch pin 3 is high

*/

// shutdown
PORTB = 1 << PINB0; // data pin 1 is high
PORTB = 0 << PINB1; // clock pin 2 is low
PORTB = 0 << PINB2; // latch pin 3 is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB0; // data pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

// next 8 bits

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB1; // clock pin is high
PORTB = 0 << PINB1; // clock pin is low

PORTB = 1 << PINB2; // latch pin 3 is high

}

\$\endgroup\$
12
  • \$\begingroup\$ Why are all the _delay_ms()s commented out? \$\endgroup\$
    – Gerben
    Commented Jun 15, 2014 at 19:22
  • \$\begingroup\$ Oh yes, I was seeing if a delay would help at all. I commented them out when I wasn't using them. \$\endgroup\$ Commented Jun 15, 2014 at 19:30
  • \$\begingroup\$ I suggest you download the Arduino IDE, and take a look at the shiftOut() function to see how they do it. Your way seems to be a little bit arse about face if you ask me. \$\endgroup\$
    – Majenko
    Commented Jun 15, 2014 at 19:33
  • \$\begingroup\$ I see. I will look more into that. I had also made a very simple test example until I got the disired bits output. but it still didn't work. like: PORTB = 1 << PINB0; // data pin 1 is high PORTB = 0 << PINB1; // clock pin 2 is low PORTB = 0 << PINB2; // latch pin 3 is low // ect.. \$\endgroup\$ Commented Jun 15, 2014 at 19:38
  • 3
    \$\begingroup\$ I think your main problem is your understanding of how the PORTB variable works. Every time you're setting one pin to a state you're turning off all the others. \$\endgroup\$
    – Majenko
    Commented Jun 15, 2014 at 19:51

1 Answer 1

1
\$\begingroup\$

I believe the port changes should be as follows: PORTB |= 1 << PORTPIN, the or-equals allows the previously set bits to remain unchanged as you toggle new pins. The way you currently have your code written, each time you turn a new pin on or off, you are clearing all the other bits in that register.

\$\endgroup\$
1
  • \$\begingroup\$ Closer, but that could only ever turn the pin on, and never off. You need anding with the inverted bitmask for that. \$\endgroup\$ Commented Aug 15, 2014 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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