Ich habe ein Porblem mit der SPI Kommunikation!!! ATMega16 -> MCP41010 MOSI -> SI SCK -> SCK SS -> CS(inv.) Mein Code: #include <stdlib.h> #include <avr/io.h> #include <avr/interrupt.h> #include <avr/signal.h> #include <avr/pgmspace.h> #include <util/delay.h> #include "uart.h" #include "ADC.h" /* define CPU frequency in Mhz here if not defined in Makefile */ #ifndef F_CPU #define F_CPU 16000000UL #endif /* 9600 baud */ #define UART_BAUD_RATE 9600 void adc(char mux) { int ad = ADC_Read(mux); char buffer[10]; itoa(ad,buffer,10); uart_puts(buffer); uart_puts("\n\r"); return; } void spi_init(void) { SPCR|=(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //SPI enable, Master, Freq/128; } void spi_write(char data) { PORTB |= (1<<PB4); //CS high SPDR = 0x11; // 11 für write und pot1 SPDR = data; PORTB &= ~(1<<PB4); //CS low } int main(void) { DDRD &=~((1<<PD2)|(1<<PD3)|(1<<PD4)); DDRA = 0x00; DDRD|=(1<<PD7)|(1<<PD6)|(1<<PD5); DDRB |= (1<<PB5)|(1<<PB7); //MOSI u SCK als OUTPUT DDRB |= (1<<PB4); // SS als OUTPUT uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU)); ADC_Init(); spi_init(); sei(); while (1) { int read = uart_getc(); if (read==48) adc(0); if (read==49) adc(1); if (read==50) adc(2); if (read==51) adc(3); if (read==52) adc(4); if (read==53) adc(5); if (read==54) adc(6); if (read==55) adc(7); if (PIND &(1<<PD2)) spi_write(0xFF); //255 else spi_write(0x50); // 80 _delay_ms(1000); } } Kann mir jemand sagen was ich falsch mache??? Der wert des potis verändert sich nicht!!!
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
Das Poti hat ein high-aktives CS? Kann ich kaum glauben.
Gast
#2454452
Ich auch nicht. Reduziert auf die Grundfunktionen:
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
Gast
#2455829
Vielen Dank, jetzt geht es endlich. Ich dachte wenn CS invertiert ist und im Protokoll drin steht low active dann muss ich den eingang high setzten!!! Lg