1 | /**
|
2 | * Greift auf eine SD Karte per SPI zu
|
3 | *
|
4 | * Doku, siehe http://www.mikrocontroller.net/articles/AVR_FAT32
|
5 | * Neuste Version: http://www.mikrocontroller.net/svnbrowser/avr-fat32/
|
6 | *
|
7 | *
|
8 | */
|
9 |
|
10 | #define F_CPU 8000000UL
|
11 | #include <avr/io.h>
|
12 | #include <util/delay.h> // definiert _delay_ms()
|
13 |
|
14 |
|
15 | #include <avr/interrupt.h>
|
16 | #include <stdio.h>
|
17 | #include <mmc_config.h> // Hier werden alle noetigen Konfigurationen vorgenommen, umbedingt anschauen !
|
18 | #include <file.h>
|
19 | #include <fat.h>
|
20 | #include <mmc.h> // Hardware abhaengig
|
21 | #include <uart.h> // Hardware abhaengig, es kann auch eine eigene eingebunden werden !
|
22 |
|
23 | #include <avr/BitUtilities.h>
|
24 |
|
25 | #include <util/LCD_display_free_int_pins.h>
|
26 |
|
27 | char strl [5];
|
28 | int xx = 0; // Zähler Datensatz
|
29 |
|
30 |
|
31 | //"""" SD Karte """"""""""""""""""""""""""""""""""""""""""""""""""""
|
32 |
|
33 | // prototypen von funktionen in dieser datei
|
34 | static void timer0_init(void);
|
35 | int main(void);
|
36 |
|
37 | // timer0 einstellungen, werte mit http://www.avrcalc.com/download.html berechnet!
|
38 | // aus diesen 3 werten ergibt sich die tick zeit, hier 10ms.
|
39 | // 4 = prescaler 256, 3 = prescaler 64, 5 = prescaler 1024, 2 = prescaler 8. wenn prescaler 0 = prescaler dann stoppt der timer
|
40 | #if(F_CPU == 4000000) // error 0.16%
|
41 | #define TOP_OCR 0x9B
|
42 | #define START_TCNT 0x64
|
43 | #define PRESCALER 0x04
|
44 | #endif
|
45 |
|
46 | #if(F_CPU == 8000000) // error 0,16%
|
47 | #define TOP_OCR 0x4D
|
48 | #define START_TCNT 0xB2
|
49 | #define PRESCALER 0x05
|
50 | #endif
|
51 |
|
52 | #if(F_CPU == 10000000) // error 0.351%
|
53 | #define TOP_OCR 0x61
|
54 | #define START_TCNT 0x9E
|
55 | #define PRESCALER 0x05
|
56 | #endif
|
57 |
|
58 | #if(F_CPU == 12000000) // error 0.16%
|
59 | #define TOP_OCR 0x74
|
60 | #define START_TCNT 0x8B
|
61 | #define PRESCALER 0x05
|
62 | #endif
|
63 |
|
64 | #if(F_CPU == 16000000) // error 0,16%
|
65 | #define TOP_OCR 0x9B
|
66 | #define START_TCNT 0x64
|
67 | #define PRESCALER 0x05
|
68 | #endif
|
69 |
|
70 | #if(F_CPU == 20000000) // error 0.16%
|
71 | #define TOP_OCR 0x4D
|
72 | #define START_TCNT 0xB2
|
73 | #define PRESCALER 0x04
|
74 | #endif
|
75 |
|
76 | // timer0 variable
|
77 | volatile uint8_t TimingDelay; // fuer mmc.c
|
78 |
|
79 | ISR (TIMER0_COMP_vect)
|
80 | {
|
81 | TimingDelay = (TimingDelay==0) ? 0 : TimingDelay-1;
|
82 | }
|
83 |
|
84 | static void timer0_init()
|
85 | {
|
86 | // timer0 config
|
87 | // initialisierung, auf jeden fall vor mmc_init(),
|
88 | // denn da wird der timer benoetigt!
|
89 |
|
90 | TimingDelay = 0; // initialisierung der zaehl variable
|
91 |
|
92 | TCCR0 = 1<<WGM01; // timer0 im ctc mode
|
93 | TIMSK = 1<<OCIE0; // compare interrupt an
|
94 |
|
95 | TCNT0 = START_TCNT; // ab wo hochgezaehlt wird,
|
96 | OCR0 = TOP_OCR; // maximum bis wo gezaehlt wird bevor compare match
|
97 |
|
98 | TCCR0 = PRESCALER; // wenn prescaler gesetzt wird, lauft timer los
|
99 | sei(); // interrupts anschalten, wegen compare match
|
100 | }
|
101 |
|
102 |
|
103 | // ***** Haupt Programm ************************************************************************************************************
|
104 |
|
105 |
|
106 | int main(void)
|
107 |
|
108 | {
|
109 |
|
110 | lcd_init(); lcd_send(COMMAND, LCD_CLEAR); //LCD
|
111 | lcd_set_cursor (1, 1);lcd_write ("Datensatz"); //LCD
|
112 | sprintf(strl,"%02d",xx); lcd_set_cursor (1, 12); lcd_write (strl); //LCD
|
113 |
|
114 |
|
115 | //""""" SD Karte Aktivierung """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
116 |
|
117 | uint8_t file_name [] = "Temp.txt";
|
118 | char str [20];
|
119 |
|
120 | timer0_init(); // Timer initialisieren bevor FAT Library genutzt wird
|
121 |
|
122 |
|
123 | lcd_set_cursor (2, 1);lcd_write ("Datensatz"); //LCD letze funktionierende Stelle
|
124 | sprintf(strl,"%02d",xx); lcd_set_cursor (2, 12); lcd_write (strl); //LCD letze funktionierende Stelle
|
125 |
|
126 |
|
127 | uinit(); // uart config
|
128 |
|
129 | uputs((uint8_t*)"Start");
|
130 |
|
131 | // sd/mmc config
|
132 | if( FALSE == mmc_init() )
|
133 | {
|
134 | uputs((uint8_t*)" -> Fehler Init. Ende\n");
|
135 | return -1;
|
136 | }
|
137 |
|
138 | // fat config
|
139 | if( FALSE == fat_loadFatData() )
|
140 | {
|
141 | uputs((uint8_t*)" -> Fehler loadFatData. Ende\n");
|
142 | return -1;
|
143 | }
|
144 |
|
145 | uputs((uint8_t*)" OK\n");
|
146 |
|
147 |
|
148 |
|
149 | // ********************** Datei schreiben auf SD **********************
|
150 |
|
151 | for (unsigned char i = 0; i <= 9; i++)
|
152 | {
|
153 | xx++;
|
154 |
|
155 | if( MMC_FILE_OPENED == ffopen(file_name,'r') ) // Oeffne existierende Datei zum anhaengen von Daten. Wenn geklappt
|
156 | {ffseek(file.length);} // Gehe ans Ende der Datei und LED2 an
|
157 |
|
158 | else
|
159 | {ffopen(file_name,'c');}
|
160 |
|
161 | sprintf (str, "Datensatz %2d\r\n",xx); // DOS Zeilenumbruch
|
162 | ffwrites ((uint8_t*)str);
|
163 | ffclose();
|
164 | }
|
165 |
|
166 | lcd_set_cursor (2, 1);lcd_write ("Datensatz"); //LCD
|
167 | sprintf(strl,"%02d",xx); lcd_set_cursor (2, 12); lcd_write (strl); //LCD
|
168 |
|
169 | return(0);
|
170 | }
|