I'm trying to connect a ATmega 168 with an MAX6979 (16x LED driver) via SPI.
So far it works but after the first transfer the ATmega stops and doesn't do anything. Here is the code I'm using:
Code: Select all
//F_CPU 8 Mhz
#ifndef F_CPU
#define F_CPU 8000000
#endif
#include <avr/io.h>
#include <util/delay.h>
#define ERR_LED_1 _BV(PC3)
#define MAX_LE _BV(PD6) //
#define SCK _BV(PB5)
#define MISO _BV(PB4)
#define MOSI _BV(PB3)
uint8_t spiSend(uint8_t data);
int main(void) {
// IO Pins
DDRC |= ERR_LED_1;
PORTC &= ~ERR_LED_1; // error led - off
DDRD |= MAX_LE;
PORTD |= MAX_LE;
// SPI init
DDRB |= MOSI | SCK ;
PORTB &= ~(SCK | MOSI | MISO);
// SPI Master enable, fosc = fclk/2
SPCR = (1<<SPE) | (1<<MSTR);
SPSR = (1<<SPI2X);
while(1) {
PORTC ^= ERR_LED_1; // toggle error led - on
_delay_ms(800);
spiSend(0xaa);
PORTC ^= ERR_LED_1; // toggle error led - off
_delay_ms(800);
spiSend(0x55);
}
return 0;
}
uint8_t spiSend(uint8_t data) {
PORTD |= MAX_LE; // latch enable
SPDR = data;
while(!(SPSR & (1<<SPIF))); // wait for end of transfer
PORTD &= ~MAX_LE; // latch disable
return SPDR;
}
I also tried this with an arduino before which shows the same behavior.
What am I missing?