Hallo,
ich versuche mit zwei µC ATMega8 über SPI zu kommunizieren. Der eine µC
liegt an STK500,der andere an einem Rowalt ARVBoard.
Folgende PIN Connection habe ich:
SS - SS | PINB2 mit PINB2
MOSI - MOSI | PINB3 mit PINB3
MISO - MISO | PINB4 mit PINB4
SCK - SCK | PINB5 mit PINB5
Hier das Programm für den Master:
1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <stdlib.h>
|
4 | #include <stdio.h>
|
5 | #include <util/delay.h>
|
6 |
|
7 |
|
8 | void SPI_MasterInit(void)
|
9 | {
|
10 | DDRB = (1<<PINB3)|(1<<PINB5)||(1<<PINB2);
|
11 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
|
12 | }
|
13 |
|
14 | void SPI_MasterTransmit(unsigned char Data)
|
15 | {
|
16 | PORTB &= ~_BV(PINB2); //sending 0 to SS and starting transmission
|
17 | SPDR = Data;
|
18 | while(!(SPSR & (1<<SPIF))){//während dem Sendevorgang LED's blinken
|
19 | DDRC=0xff;
|
20 | _delay_ms(500);
|
21 | DDRC=0x00;
|
22 | _delay_ms(500);
|
23 | }
|
24 | PORTB |= _BV(PINB2); //end transimssion - send 1
|
25 | }
|
26 |
|
27 | int main(){
|
28 | SPI_MasterInit();
|
29 | SPI_MasterTransmit(0x01);
|
30 |
|
31 | }
|
und hier für den Slave:
1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <stdlib.h>
|
4 | #include <util/delay.h>
|
5 | #include <stdio.h>
|
6 |
|
7 |
|
8 |
|
9 | void SPI_SlaveInit(void)
|
10 | {
|
11 | DDRB = (1<<PINB4);
|
12 | SPCR = (1<<SPE);
|
13 | }
|
14 |
|
15 | char SPI_SlaveReceive(void)
|
16 | {
|
17 | while(!(SPSR & (1<<SPIF))){//während der Transaktion schnell blinken
|
18 | PORTC=0xff;
|
19 | _delay_ms(500);
|
20 | PORTC=0x00;
|
21 | _delay_ms(500);
|
22 | }
|
23 | return SPDR;
|
24 | }
|
25 |
|
26 | int main(){
|
27 | unsigned char dane;
|
28 | SPI_SlaveInit();
|
29 | while(1){
|
30 | dane=SPI_SlaveReceive();
|
31 | if(dane==0x01){
|
32 | while(1) { PORTC=0xff;} //falls erfolgreich,LED's an
|
33 | }
|
34 | else
|
35 | {
|
36 | while(1){ //falls Transaktion gescheitert,langsam blinken
|
37 | PORTC=0xff;
|
38 | _delay_ms(2000);
|
39 | PORTC=0x00;
|
40 | _delay_ms(2000);
|
41 |
|
42 | }
|
43 | }
|
44 | }
|
45 | }
|
Ich versuche also eine 1 vom Master zu Slave zu schicken. Bei dem Slave
blinken allerdings die LED's die ganze Zeit schnell,der scheint immer in
der Recieve-Schleife zu sein.Bei dem Master stelle ich den Sendevorgang
mit dem einmaligen Blinken der LED's fest.
Könnte mir das jemand helfen,was ich falsch mache?