Guten Abend zusammen,
ich habe mir einen LED-Cube (4x4x4) zusammengelötet, der per MAX7221
angesteuert wird. Als µC habe ich einen ATMega32 auf einem
AVR-Evaluationsboard von Pollin. Programmiert wird über ein USB-AVR-Lab
von Ullihome mit STK500 Firmware.
Ich hbe den Cube schon mittels Bascom angesteuert und es hat auch alles
so funktioniert, wie es sollte, also ist die elektronische Schaltung
richtig.
Nun möchte ich gerne auf C umsteigen, aber es klappt nicht. Manchmal
leuten alle LEDs und manchmal keine. Ich hab "erweiterte
Grundkenntnisse" in C, hab aber noch nie einen AVR-Programmiert. Bisher
immer nur mit Bascom.
Der MAX7221 wartet auf insgesamt 16 bit. Das erste Bit ist das MSB. Nach
der Initialisierung steht das erste Byte für das Digit und das zweite
Byte für die 8 Segmente. Damit sollte mit diesem Code bei Digit 1 die
erste LED leuchten (SPI_write(0x01,0x01)). Tut sie aber nicht.
Code:
1
#include<avr\io.h>
2
3
4
voidSPI_write(unsignedchar,unsignedchar);
5
voidmax7221_init(void);
6
7
voidmain(){
8
DDRB=(1<<PB4)|(1<<PB5)|(1<<PB6);// MOSI,SCK, SS als Output, Rest als Input
while(!(SPSR&(1<<SPIF)));// Wait for transmission complete
23
SPDR=data;// Start Data transmission
24
while(!(SPSR&(1<<SPIF)));// Wait for transmission complete
25
PORTB|=(1<<PB4);// Set tze CS pin to 1
26
}
27
28
voidmax7221_init(){
29
SPI_write(0x0C,0x01);//normal operation
30
SPI_write(0x09,0x00);//no decode
31
SPI_write(0x0A,0x02);//set intensity
32
SPI_write(0x0B,0x07);//display all digits
33
SPI_write(0x0C,0x01);//normal operation
34
SPI_write(0x0F,0x00);//Displaytest aus
35
SPI_write(0x01,0x00);//clear Digit 1
36
SPI_write(0x02,0x00);//clear Digit 2
37
SPI_write(0x03,0x00);//clear Digit 3
38
SPI_write(0x04,0x00);//clear Digit 4
39
SPI_write(0x05,0x00);//clear Digit 5
40
SPI_write(0x06,0x00);//clear Digit 6
41
SPI_write(0x07,0x00);//clear Digit 7
42
SPI_write(0x08,0x00);//clear Digit 8
43
}
Noch ne kurze ergänzende Frage:
SPI nutzt ja die gleichen Pins wie die ISP. Macht es dann etwas aus,
wenn der Programmer noch am Board hängt?
Ich hoffe ihr könnt mir Helfen!
Viele Grüße
Marius
Ich bins nochmal:
Ich hab noch etwas rumgesucht und bin über Google auf folgende Seite
gestoßen:
http://www.hackchina.com/en/r/19672/Max7221.h__html
Dabei wird nich das Hardware-SPI verwendet, sondern das ganze "per Hand"
gemacht. Daraufhin hab ich den Code folgendermaßen umgestrickt, und nun
Läuft es:
1
#include<avr\io.h>
2
3
#define _nop_() asm volatile("nop")
4
5
typedefunsignedcharbyte;
6
7
structtest{
8
byteb0:1;
9
byteb1:1;
10
byteb2:1;
11
byteb3:1;
12
byteb4:1;
13
byteb5:1;
14
byteb6:1;
15
byteb7:1;
16
}__attribute__((__packed__));
17
18
#define BIT(r,n) (((volatile struct test *)&r)->b##n)
19
20
#define Max7221DIN BIT(PORTD,5)
21
22
23
voidSPI_write(unsignedchar,unsignedchar);
24
voidmax7221_init(void);
25
26
voidmain(){
27
//DDRB |= (1<<PB4)|(1<<PB5)|(1<<PB6); // MOSI,SCK, SS als Output, Rest als Input
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
78
SPDR = data; // Start Data transmission
79
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
80
PORTB |= (1<<PB4); // Set tze CS pin to 1
81
}
82
*/
83
84
voidmax7221_init(){
85
SPI_write(0x0C,0x01);//normal operation
86
SPI_write(0x09,0x00);//no decode
87
SPI_write(0x0A,0x02);//set intensity
88
SPI_write(0x0B,0x07);//display all digits
89
SPI_write(0x0C,0x01);//normal operation
90
SPI_write(0x0F,0x00);//Displaytest aus
91
SPI_write(0x01,0x00);//clear Digit 1
92
SPI_write(0x02,0x00);//clear Digit 2
93
SPI_write(0x03,0x00);//clear Digit 3
94
SPI_write(0x04,0x00);//clear Digit 4
95
SPI_write(0x05,0x00);//clear Digit 5
96
SPI_write(0x06,0x00);//clear Digit 6
97
SPI_write(0x07,0x00);//clear Digit 7
98
SPI_write(0x08,0x00);//clear Digit 8
99
}
Warum funktioniert das ganze dann nicht mit dem Hardware-SPI?
Ich hab nur Clock, MOSI und CS auf andere Pins gelegt, ansonsten ist die
Hardware identisch.
Hat jemand ne Idee?
Viele Grüße
Marius
Marius F. schrieb:> Warum funktioniert das ganze dann nicht mit dem Hardware-SPI?
Ein möglicher Fehler:
Der /SS-Pin muß vor SPI-Init auf Ausgang gesetzt werden.
Peter
Marius F. schrieb:> das hab ich doch getan.
Dann wird wohl was anderes falsch sein.
Probier mal alle 4 Modies aus.
Ich benutze meistens auch nur das Bitschubser-SPI in SW.
Peter
>Ich benutze meistens auch nur das Bitschubser-SPI in SW.
Mein Gott wie armselig. Kriegst du das SPI Modul nicht zum laufen?
Ich geb dir gerne Nachhilfestunden;)
holger schrieb:> Ich geb dir gerne Nachhilfestunden;)
Nicht nötig.
Ich hab an einem ATmega2560 am SPI ADCs und DACs am Laufen und an einer
UART im SPI-Modus einige IO-Expander (74HC165/74HC595).
Welcher Modus für den MAX7219 paßt, müßte ich aber erst im Datenblatt
nachsehen.
Ich benutze auch oft ATtinys und da bevorzuge ich SW-SPI.
Peter