Gast
#182754
Hallo,
ich versuche Daten über SPI zwischen 2 Atmega32 zu übertragen, habe
dabei aber leider das Problem das unregelmässig ein komplettes Byte
nicht ankommt. Ob der Fehler beim Sender oder Empfänger liegt weiß ich
nicht, hier mein Code:
Master:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
unsigned char status = 0;
volatile unsigned char count;
void spi_master_init (void);
void spi_putc (unsigned char data);
SIGNAL (SIG_SPI) {
return;
}
void spi_puts(const char *s )
{
while (*s)
spi_putc(*s++);
}
void spi_master_init (void) {
DDRB = (1<<PB0) | (1<<PB5) | (1<<PB7) | (1<<PB4); //als Ausgang
DDRB &= ~(1<<PB6); //als Eingang
PORTB = (1<<PB7) | (1<<PB0); // SCK und PB0 high (ist mit SS am
Slave verbunden)
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0);
status = SPSR;
}
void spi_putc (unsigned char data)
{
PORTB &= ~(1<<PB0);
SPDR = data;
while (!(SPSR & (1<<SPIF)));
PORTB |= (1<<PB0);
}
int main (void)
{
spi_master_init ();
sei ();
spi_puts("Test abc123");
for (;;);
return 0;
}
Slave:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include "uart.h"
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#define UART_BAUD_RATE 19200
volatile unsigned char data;
unsigned char status;
SIGNAL (SIG_SPI)
{
data = SPDR;
uart_putc(data);
}
void spi_slave_init (void)
{
DDRB |= (1<<PB6);
SPCR = (1<<SPE) | (1<<SPIE);
status = SPSR;
}
int main (void)
{
uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
uart_putc('T');
spi_slave_init ();
sei ();
for (;;);
return 0;
}
Für die UART-Ausgabe benutze ich die Routinen von Peter Fleury.
Wäre schön wenn mir jemand weiterhelfen könnte.