summaryrefslogtreecommitdiffstats
path: root/Bachelor/Mikroprozessorsysteme2/mi2/Termin4/Termin4Aufgabe1.c
blob: 67b220d29e060533d2d0014e3f69e77f0e3da3a1 (plain)
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
// L�sung zur Aufgabe Termin4
// Aufgabe 1
//****************************
// Zeiger auf Peripherie
// Messen der Periodendauer einer angelegten Frequenz
// 
// von: Manfred Pester
// vom: 06. August 2003

#include "../h/pio.h"
#include "../h/tc.h"
#include "../h/pmc.h"

// f�r die Initialisierung des Z�hler TC4

#define TC4_INIT  TC_CLKS_MCK2 | TC_LDBSTOP | TC_CAPT | TC_LDRA_RISING_EDGE | TC_LDRB_RISING_EDGE


int main(void)
{
	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/1.09;
	volatile int	c2=40;
	volatile float	masse;
		
	StructPMC* pmcbase = PMC_BASE;
	StructPIO* piobaseA = PIOA_BASE;
	StructPIO* piobaseB = PIOB_BASE;
	StructTC* tcbase4 = TCB4_BASE;
	StructTC* tcbase5 = TCB5_BASE;
	
	pmcbase->PMC_PCER = 0x06f80;	// Clock PIOA, PIOB, Timer5, Timer4, Timer1 einschalten
	
// 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		=	TC4_INIT;
	tcbase5->TC_CCR		=	TC_CLKEN;
	tcbase5->TC_CCR		=	TC_SWTRG;
	
	piobaseB-> PIO_PER = KEY3;
	
	while(piobaseB->PIO_PDSR & KEY3)
	{
		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) / 12.5;	// Zeit in us
		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) / 12.5;	// Zeit in us
		
		masse = c1 * ((Periodendauer1 / Periodendauer2) -1) -c2;
		
	}
	
	return 0;
}