// 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<AIC_ICCR = (1<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<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<PIO_OER = (1<PIO_CODR = (1< 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<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<PIO_PER = (1<PIO_OER = (1<PIO_CODR = (1< 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<PIO_IDR = 0xFFFFFFFF; // Timer (0x200), PIOA (0x2000), PIOB (0x4000) ausschalten //pmcbase->PMC_PCDR = (1<