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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// L�sung zu Termin6&7
// Aufgabe 1...
// Namen: Andreas Spirka; Sven Eisenhauer
// Matr.: 702789; 707173
// vom : 25.01.2006
#include "../h/pmc.h"
#include "../h/tc.h"
#include "../h/pio.h"
#include "../h/aic.h"
void pmcinit(void);
void pioinit(void);
void Timer_init(void);
int massemessen(void);
void pumpestart(void);
void pumpestop(void);
void greeting(void);
void ausgabe(int);
int genaumessen(void);
void Int2Str(int);
void taste_irq_handler (void) __attribute__ ((interrupt));
volatile int aktion=0;
char lf[2];
char cr[2];
// f�r die Initialisierung der Z�hler TC4 und TC5
#define TC4_INIT TC_CLKS_MCK2 | TC_LDBSTOP | TC_CAPT | TC_LDRA_RISING_EDGE | TC_LDRB_RISING_EDGE
#define TC5_INIT TC_CLKS_MCK2 | TC_LDBSTOP | TC_CAPT | TC_LDRA_RISING_EDGE | TC_LDRB_RISING_EDGE
// Interruptserviceroutine f�r die Tasten SW1 und SW2
void taste_irq_handler (void)
{
StructPIO* piobaseB = PIOB_BASE; // Basisadresse PIO B
StructAIC* aicbase = AIC_BASE; // Basisadresse Advanced Interrupt Controller
int taste = piobaseB->PIO_PDSR;
if (!(taste & KEY1)) // falls Schalter 1 gedr�ckt
aktion=1;
if (!(taste & KEY2)) // falls Schalter 2 gedr�ckt
aktion=2;
aicbase->AIC_EOICR = piobaseB->PIO_ISR; // AIC End of Interrupt Command Register = 1
}
void pmcinit(void)
{
StructPMC* pmcbase = PMC_BASE;
pmcbase->PMC_PCER = 0x06f84; // Clock PIOA, PIOB, Timer5, Timer4, Timer3,US0 einschalten
}
void pioinit(void)
{
StructPIO* piobaseB = PIOB_BASE; // Basisadresse PIO B
StructAIC* aicbase = AIC_BASE; // Basisadresse Advanced Interrupt Controller
// disable all interrupt sources of pio
piobaseB->PIO_IDR = 0xFFFFFFFF;
piobaseB->PIO_PER = KEY1|KEY2; // Schalter 1-2 enabeled
// Interrupt Initialisierung f�r PIOB (0x4000)
aicbase->AIC_IDCR = (1<<PIOB_ID); // Interrupt disabled
aicbase->AIC_ICCR = (1<<PIOB_ID); // Interrupt clear
aicbase->AIC_SMR[PIOB_ID] = 0x1; // An Stelle 14 Level Sensitive / H�chste Priorit�t
aicbase->AIC_SVR[PIOB_ID] = (unsigned int)taste_irq_handler; // Adresse der Interrupt Service Routine in Vektor-Tabelle
aicbase->AIC_IECR = (1<<PIOB_ID); // enable PIOB Interrupt in AIC
piobaseB->PIO_IER = KEY1|KEY2; // Schalter 1+2 l�sen Interrupts aus
}
void Timer_init(void)
{
StructTC* tcbase3 = TCB3_BASE; // Basisadressse TC Block 3 Pumpe
StructTC* tcbase4 = TCB4_BASE; // Basisadressse TC Block 4 Waage
StructTC* tcbase5 = TCB5_BASE; // Basisadressse TC Block 5 Waage
StructPIO* piobaseA = PIOA_BASE; // Basisadresse PIO A
tcbase3->TC_CCR = TC_CLKDIS; // Disable Clock
// Initialize the mode of the timer 3
tcbase3->TC_CMR =
TC_ACPC_CLEAR_OUTPUT | //ACPC : Register C clear TIOA
TC_ACPA_SET_OUTPUT | //ACPA : Register A set TIOA
TC_WAVE | //WAVE : Waveform mode
TC_CPCTRG | //CPCTRG : Register C compare trigger enable
TC_CLKS_MCK1024; //TCCLKS : MCKI / 1024
// Initialize the counter:
tcbase3->TC_RA = 244;
tcbase3->TC_RC = 488; // RA = RC/2 f�r symmetrisches Signal
// Start the timer :
tcbase3->TC_CCR = TC_CLKEN ; //__
tcbase3->TC_CCR = TC_SWTRG ; //__
piobaseA->PIO_PER = (1<<PIOTIOA3) ; // Pio herrscht �ber pin
piobaseA->PIO_OER = (1<<PIOTIOA3) ; // Output an pin
piobaseA->PIO_CODR = (1<<PIOTIOA3) ; // clear output -> low signal, pumpe aus
// Periodendauer der Waagensignale messen
// Signal aud TIOA4 ca. 16kHz entspricht ca. einer Periodendauer von 62,5us
// durch den Teiler von 32 ergeben sich ca. 2ms
// Z�hler mit positiver Flanke starten
//piobaseA->PIO_PDR = 0x090;
piobaseA->PIO_PDR = (1<<PIOTIOA4)|(1<<PIOTIOA5);
tcbase4->TC_CCR = TC_CLKDIS;
tcbase4->TC_CMR = TC4_INIT;
tcbase4->TC_CCR = TC_CLKEN;
tcbase4->TC_CCR = TC_SWTRG;
tcbase5->TC_CCR = TC_CLKDIS;
tcbase5->TC_CMR = TC5_INIT;
tcbase5->TC_CCR = TC_CLKEN;
tcbase5->TC_CCR = TC_SWTRG;
}
int massemessen(void)
{
StructTC* tcbase4 = TCB4_BASE;
StructTC* tcbase5 = TCB5_BASE;
volatile int captureRA1;
volatile int captureRB1;
volatile int capturediff1;
volatile float Periodendauer1;
volatile int captureRA2;
volatile int captureRB2;
volatile int capturediff2;
volatile float Periodendauer2;
volatile int c1=18030;
volatile int c2=40;
volatile int masse;
tcbase4->TC_CCR = TC_SWTRG;
tcbase5->TC_CCR = TC_SWTRG;
while (!( tcbase4->TC_SR & TC_LDBSTOP)); // Capture Register B wurde geladen Messung abgeschlossen
captureRA1 = tcbase4->TC_RA; //
captureRB1 = tcbase4->TC_RB;
capturediff1 = abs(captureRB1) - abs(captureRA1);
Periodendauer1 = abs(capturediff1);
while (!( tcbase5->TC_SR & TC_LDBSTOP)); // Capture Register B wurde geladen Messung abgeschlossen
captureRA2 = tcbase5->TC_RA; //
captureRB2 = tcbase5->TC_RB;
capturediff2 = abs(captureRB2) - abs(captureRA2);
Periodendauer2 = abs(capturediff2);
masse = c1 * ((Periodendauer1 / Periodendauer2) -1) -c2;
return masse;
}
void pumpestart(void)
{
StructPIO* piobaseA = PIOA_BASE;
piobaseA->PIO_PDR = (1<<PIOTIOA3); // Timer herrscht �ber Bit (Taktsignal)
}
void pumpestop(void)
{
StructPIO* piobaseA = PIOA_BASE;
// Vermeiden, da� dauerhaftes High Signal (PIO herrscht �ber das BIT)
piobaseA->PIO_PER = (1<<PIOTIOA3) ; // PIN der PIO zuweisen
piobaseA->PIO_OER = (1<<PIOTIOA3) ; // wird auf Output gesetzt
piobaseA->PIO_CODR = (1<<PIOTIOA3) ; // Clear Output (low Signal)
}
void greeting(void)
{
cr[0]=0x0d;
lf[0]=0x0a;
lf[1]='\0';
cr[1]='\0';
puts("AUSSCHANKSTATION\0");
puts(&cr[0]);
puts(&lf[0]);
puts("Bitte Becher auf die Waage stellen\0");
puts(&cr[0]);
puts(&lf[0]);
puts("SW1 zum tarieren dr�cken\0");
puts(&cr[0]);
puts(&lf[0]);
}
void ausgabe(int m)
{
cr[0]=0x0d;
lf[0]=0x0a;
lf[1]='\0';
cr[1]='\0';
puts("Gemessene Gesamtmasse: \0");
Int2Str(m);
puts(&cr[0]);
puts(&lf[0]);
puts("Becher kann entfernt werden\0");
puts(&cr[0]);
puts(&lf[0]);
}
void Int2Str(int m)
{
int a,i=7;
char string[9];
string[8] = '\0';
while(m != 0)
{
a = m%10;
string[i] = a + '0';
m = m/10;
i--;
}
puts(&string[i+1]);
}
int genaumessen(void)
{
volatile int durchlaeufe=10;
volatile int summe=0;
volatile int retVal=0;
volatile int i=0;
for (i=0;i<durchlaeufe;i++)
{
summe+=massemessen();
}
retVal= summe/durchlaeufe;
return retVal;
}
int main(void)
{
volatile int masse=0;
volatile int tara=0;
volatile int netto=0;
cr[0]=0x0d;
lf[0]=0x0a;
lf[1]='\0';
cr[1]='\0';
inits();
greeting();
pmcinit();
Timer_init();
pioinit();
for(;;)
{
if(aktion == 1)
{
puts("Tara: \0");
tara=genaumessen();
netto=0;
Int2Str(tara);
puts(" SW2 startet die Pumpe");
puts(&cr[0]);
aktion=0;
}
if (aktion == 2 && tara != 0)
{
puts(&cr[0]);
puts(&lf[0]);
pumpestart();
while(netto <= 50)
{
masse=genaumessen();
netto=masse-tara;
puts("Fuellgewicht: \0");
Int2Str(netto);
puts(&cr[0]);
}
pumpestop();
ausgabe(masse);
do {
masse=genaumessen();
}
while (masse > 10);
// Gewicht hat sich ge�ndert, d. h. Becher wurde entfernt
// D. h. von vorne starten
greeting();
aktion=0;
}
}
// disable piob command register in AIC
//aicbase->AIC_IDCR = (1<<PIOB_ID);
// disable all interrupt sources of pio
//piobaseB->PIO_IDR = 0xFFFFFFFF;
// Timer (0x200), PIOA (0x2000), PIOB (0x4000) ausschalten
//pmcbase->PMC_PCDR = (1<<PIOA_ID)|(1<<PIOB_ID)|(1<<TC3_ID);
return 0;
}
|