Hallo Leute,
habe folgendes Problem:
Ich versuche gerade zwei Mikrocontroller miteinander über SPI
kommunizieren zu lassen. Ein Kommunikation zwischen zwei ATMega32 ist
mir bereits gelungen. Leider Möchte der ATTiny2313 aber irgendwie nicht
mit dem ATMega32 sprechen :-(
Nach langem Suchen in unzälligen Foren und stundenlanges Wälzen des
Datenblattes habe ich ein Programm geschrieben, welches eigentlich
funktionieren sollte. Vielleicht könnt ihr mir ja mal wieder auf die
Sprünge helfen :-)
Hier der Code des Master (ATMega32):
1 | /*
|
2 | * SPI_Master.c
|
3 | *
|
4 | * Created: 06.07.2013 21:05:31
|
5 | * Author: Jens
|
6 | */
|
7 |
|
8 | // This program is for the SPI-Master on an ATMega device
|
9 |
|
10 | #include <avr/io.h>
|
11 | #include <util/delay.h>
|
12 |
|
13 | // Initialize the SPI-Interface
|
14 | void init_spi()
|
15 | {
|
16 | //Set MOSI, clock pin and SS pin as output. Not using
|
17 | // the SS pin at this point.
|
18 | DDRB = (1<<DDB4) | (1<<DDB5) | (1<<DDB7);
|
19 |
|
20 | //Enable SPI mode, master and F_CPU/16. Slowed it down so
|
21 | // could see what is going on with scope.
|
22 | SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0);
|
23 | }
|
24 |
|
25 | // Send data via SPI
|
26 | void send_spi(int data)
|
27 | {
|
28 | // Load the data into the data register
|
29 | SPDR = data;
|
30 |
|
31 | // Wait until data is transmitted
|
32 | while(!(SPSR & (1<<SPIF)));
|
33 |
|
34 | // Wait 500ms before sending the next Byte
|
35 | _delay_ms(500);
|
36 | }
|
37 |
|
38 | int main(void)
|
39 | {
|
40 | int data;
|
41 |
|
42 | // Set LED-Port PB2 and PB3 as output
|
43 | DDRB = (1<<DDB3) | (1<<DDB2);
|
44 |
|
45 | init_spi();
|
46 |
|
47 | data = '5';
|
48 |
|
49 | // Send 5, 6, 5, 6, 5, ...
|
50 | while(1)
|
51 | {
|
52 | if(data == '5')
|
53 | {
|
54 | // Send '5' and turn on LED1 and off LED2
|
55 | send_spi(data);
|
56 | data = '6';
|
57 | PORTB = (1<<PB3);
|
58 | PORTB &= ~(1<<PB2);
|
59 | }
|
60 | else
|
61 | {
|
62 | // Send '6' and turn on LED2 and off LED1
|
63 | send_spi(data);
|
64 | data = '5';
|
65 | PORTB = (1<<PB2);
|
66 | PORTB &= ~(1<<PB3);
|
67 | }
|
68 | }
|
69 |
|
70 | return 0;
|
71 | }
|
und hier der des Slave (ATTiny2313):
1 | /*
|
2 | * SPI_Slave.c
|
3 | *
|
4 | * Created: 07.07.2013 10:59:29
|
5 | * Author: Jens
|
6 | */
|
7 |
|
8 | // This program is for an SPI-Slave Tiny device
|
9 |
|
10 | #include <avr/io.h>
|
11 | #include <avr/interrupt.h>
|
12 | #include <util/delay.h>
|
13 |
|
14 | int data;
|
15 |
|
16 | // Initialize SPI-Interface
|
17 | void init_spi()
|
18 | {
|
19 | // Set DO as output and DI, USCK as input
|
20 | DDRB = (1<<DDB6);
|
21 | DDRB &= ~(1<<DDB5) | ~(DDB7);
|
22 |
|
23 | // Set 3-wire-mode and external positive clock
|
24 | USICR = (1<<USIWM0) | (1<<USICS1);
|
25 |
|
26 | // Set the counter-overflow flag
|
27 | USISR = (1<<USIOIF);
|
28 |
|
29 | // Enable interrupt for counter-overflow when data is received
|
30 | USISR = (1<<USISIF);
|
31 |
|
32 | }
|
33 |
|
34 | ISR(USI_OVERFLOW_vect)
|
35 | {
|
36 | // Wait for reception complete
|
37 | while(!(USISR & (1<<USIOIF)));
|
38 |
|
39 | // Return data register
|
40 | data = USIDR;
|
41 |
|
42 | }
|
43 |
|
44 | int main(void)
|
45 | {
|
46 | // Set LED-Ports PB2 (LED1) and PB3 (LED2) as output
|
47 | DDRB = (1<<DDB2) | (1<<DDB3);
|
48 |
|
49 | init_spi();
|
50 |
|
51 | // If data is received via SPI, check if it is an '5' or an '6'.
|
52 | // Then turn on the corresponding LED
|
53 | while(1)
|
54 | {
|
55 | // Check if "data" includes an '5' -> Turn on LED1 PB2
|
56 | if(data == '5')
|
57 | {
|
58 | PORTB = (1<<PB2);
|
59 | PORTB &= ~(1<<PB3);
|
60 | }
|
61 | // Check if "data" includes an '6' -> Turn on LED2 PB3
|
62 | else if(data == '6')
|
63 | {
|
64 | PORTB = (1<<PB3);
|
65 | PORTB &= ~(1<<PB2);
|
66 | }
|
67 | }
|
68 |
|
69 | return 0;
|
70 | }
|