Hallo,
ich benutze von meinem ATMega640 den USART als SPI-Master im MSPIM
Modus.
ich habe bisher folgendes auf die Schnelle ausprobiert:
1 | void MSPIM_Init(void)
|
2 | {
|
3 | DDRD |= (1<<PD6); //CS as Output
|
4 | DDRE |= (1<<PE2); //XCK0 as Output
|
5 | UBRR0H = 0;
|
6 | UBRR0L = 7;
|
7 | /*0,5Mbps at 8MHz Clock*/
|
8 |
|
9 | // enable RX and TX and set interrupts on rx complete
|
10 | UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
|
11 |
|
12 | // MSPIM Mode, MSB first, PHA=1, POL=0
|
13 | UCSR0C = (0 << UCPOL0) | (1 << UMSEL01) | (1 << UMSEL00) | (0 << 2)| (1 << 1);
|
14 | }
|
15 |
|
16 | void MSPIM_Transmit( unsigned char data )
|
17 | {
|
18 | /* Wait for empty transmit buffer */
|
19 | while ( !( UCSR0A & (1<<UDRE0)) );
|
20 | UDR0 = data;
|
21 | }
|
22 |
|
23 | void TestTransmit(void){
|
24 | PORTD &= ~(1<<PD6); //set SS low
|
25 | MSPIM_Transmit(0x01);
|
26 | MSPIM_Transmit(0xaa);
|
27 | MSPIM_Transmit(0x01);
|
28 | while ( !( UCSR0A & (1<<6)) ); //Wait for TX Complete Flag
|
29 | PORTD |= (1<<PD6); // set SS high
|
30 | }
|
Leider verhält sich der SS-Pin nicht so wie ich erwartet hätte, er
schaltet korrekt (?) auf Low, allerdings viel zu früh wieder auf high,
obwohl ich auf das TXC Flag warte (siehe Bild).
Was mache ich falsch, warum ist das TXC-Flag schon da, obwohl das senden
noch nicht abgeschlossen ist?
Ausserdem entnehme ich der AVR317:
> A new byte can be written to UDR as soon as the UDRE flag is set. The
> transmit buffers can therefore be used to achieve continuous
> transmission without any delay between the bytes.
Wenn ich mir die Clock so anschaue, sehe ich da aber schon ein Delay
zwischen den Bytes. Was ist hier falsch?