Ja, da hast du recht. Jetzt habe ich allerdings ein anderes Problem. und
zwar wird empfängt und sendet der Slave nur 0x00, auch wenn ich etwas
anderes schicke.
Slave
1 | void spi_init()
|
2 | // Initialize pins for spi communication
|
3 | {
|
4 | /* Set MOSI SCK and SS output, all others input */
|
5 | DDR_SPI = (1 << DD_MISO);
|
6 | DDR_SPI &= ~((1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS)); // DDRB
|
7 | //spi_interrupt_init();
|
8 |
|
9 | /* Enable SP */
|
10 | SPCR = ((1<<SPE)| // SPI Enable
|
11 | (1<<SPIE)| // SPI Interupt Enable
|
12 | (0<<DORD)| // Data Order (0:MSB first / 1:LSB first)
|
13 | (0<<MSTR)| // Master/Slave select -> Slave
|
14 | (0<<SPR1)|(0<<SPR0)| // SPI Clock Rate FOSC/64
|
15 | (0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle)
|
16 | (0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling)
|
17 |
|
18 | SPSR = (1<<SPIF); // initialize
|
19 | }
|
20 |
|
21 | //void spi_interrupt_init(){
|
22 | // DDRD &= ~(1 << DD_SS); // Input INT0
|
23 | // EICRA = (1 << ISC01); // INT0 falling edge
|
24 | // EIMSK = (1 << INT0); // enable INT0 interrupt
|
25 | //}
|
26 |
|
27 |
|
28 | // SPI Transmission/reception complete ISR
|
29 | ISR(SPI_STC_vect)
|
30 | {
|
31 | PORTD |= (1<<PORTD3); // LED ON
|
32 | spi_data = SPDR;
|
33 | new_spi_data = 1;
|
34 | PORTD &= ~(1<<PORTD3); // LED OFF
|
35 | }
|
Slave main
1 | while(1){
|
2 | ...
|
3 | if(new_spi_data==1){
|
4 | usart_transmit_string("\r\n received data: ");
|
5 | usart_transmit_char(spi_data);
|
6 | new_spi_data = 0;
|
7 | }
|
8 | ...
|
9 | }
|
Master: (ATTINY)
1 | void spi_init()
|
2 | // Initialize pins for spi communication
|
3 | {
|
4 | /* Set MOSI, SCK, SS as output, all others input */
|
5 | DDR_SPI = ((1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS));
|
6 | DDR_SPI &= ~(1 << DD_MISO);
|
7 | PORT_SPI |= (1 << DD_SS); // Set SlaveSelect High
|
8 | }
|
9 |
|
10 | uint8_t spi_fast_shift(uint8_t data)
|
11 | // Clocks only one byte to target device and returns the received one
|
12 | {
|
13 | PORT_SPI &= ~(1 << DD_SS); // Salve Select
|
14 | _delay_ms(10); // Slave jumps to ISR
|
15 | // Load data into the data-register
|
16 | USIDR = data;
|
17 | USISR = (1 << USIOIF);
|
18 | while(!(USISR & (1<<USIOIF))){
|
19 | USICR |= ((1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC));
|
20 | }
|
21 | PORT_SPI |= (1 << DD_SS);
|
22 | return USIDR;
|
23 | }
|
Master main:
1 | while (1){
|
2 |
|
3 | //#ifdef SPI_MASTER
|
4 | if(new_usart_data==1){
|
5 | PORTD |= (1<<PORTD5); // LED ON
|
6 | spi_data = spi_fast_shift(usart_data);
|
7 | usart_transmit_string("\r\nslave data: ");
|
8 | usart_transmit_char(spi_data);
|
9 | new_usart_data = 0;
|
10 | PORTD &= ~(1<<PORTD5); // LED OFF
|
11 | }
|
Ich gebe im prinzip über uart vor, welche Daten gesendet werden sollen,
damit ich das noch nachvollziehen kann.
Beim Master soll er mir also die zurückgegebenen Werte darstellen. Dabei
erhalte ich immer 0x00, egal was ich sende.
Der Slave soll mir ebenfalls die erhaltenen Werte ausgeben. auch hier
bekomme ich nur 0x00.
spi_data ist dabei als volatile definiert. Die Änderung muss also
eigentlich übernommen werden.
Auch wenn ich die Ausgabe direkt nach dem Senden in der funktion
spi_fast_shift bzw. in der ISR erstelle, erhalte ich nur 0x00 zurück.