1 | #include <p18f46k22.h>
|
2 | #include <delays.h>
|
3 | #include <timers.h>
|
4 | #include <stdio.h>
|
5 | #include <string.h>
|
6 | #include "Lcd.h"
|
7 |
|
8 |
|
9 | #pragma config FOSC = INTIO7 // interner Takt
|
10 | unsigned int uint_period1 = 0;
|
11 | unsigned int uint_period2 = 0;
|
12 | unsigned long int ulint_period = 0; //Differenz
|
13 | unsigned char uchar_i = 0;
|
14 | char char_Zeit[];
|
15 | unsigned char i = 0;
|
16 |
|
17 |
|
18 | /*----------------------------------------------------------------------------
|
19 | ----- -----
|
20 | ----- InterruptHandler -----
|
21 | ----- -----
|
22 | ---------------------------------------------------------------------------*/
|
23 |
|
24 | #pragma interruptlow InterruptHandler
|
25 | void InterruptHandler (void)
|
26 | {
|
27 | // nur um zu sehen ob Timer überläuft
|
28 | LATBbits.LATB1=~LATBbits.LATB1;
|
29 | PIR1bits.TMR1IF=0; //setze Timer3 Interrupt Flag zurück
|
30 | }
|
31 | /*----------------------------------------------------------------------------
|
32 | ----- -----
|
33 | ----- Interruptvektoren setzen -----
|
34 | ----- -----
|
35 | ---------------------------------------------------------------------------*/
|
36 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x000008
|
37 | void _high_ISR (void)
|
38 | {
|
39 | _asm
|
40 | goto InterruptHandler // Sprung zur Interruptroutine
|
41 | _endasm
|
42 | }
|
43 |
|
44 | #pragma code _LOW_INTERRUPT_VECTOR = 0x000018
|
45 | void _low_ISR (void)
|
46 | {
|
47 | _asm
|
48 | goto InterruptHandler // Sprung zur Interruptroutine
|
49 | _endasm
|
50 | }
|
51 | #pragma code
|
52 |
|
53 |
|
54 |
|
55 | /*----------------------------------------------------------------------------
|
56 | ----- -----
|
57 | ----- Initialisierung -----
|
58 | ----- -----
|
59 | ---------------------------------------------------------------------------*/
|
60 | void init (void)
|
61 | {
|
62 | OSCCONbits.IRCF=0b101; //HFINTOSC interner Takt auf 4MHz -> CLKOUT = 1MHz
|
63 | T1CON = 0x00; //Timer 1 mit FOSC/4, kein Prescaler für Capture, 16 Bit in eins lesen und STOP
|
64 | ANSELCbits.ANSC2 = 0; // RC2 als digitaler
|
65 | TRISCbits.TRISC2 = 1; // RC2 als Eingang für CCP1
|
66 | PIE1bits.CCP1IE = 0; // kein Interrupt von CCP1
|
67 | PIR1bits.CCP1IF = 0; // Interrupt Flag löschen
|
68 |
|
69 | //LCD starten PORTD als Ausgang damit LCD funktioniert
|
70 | LATD=0x00;
|
71 | ANSELD=0x00;
|
72 | TRISD=0x00;
|
73 | LCDInit();
|
74 |
|
75 | LATB=0x00;
|
76 | ANSELB=0x00;
|
77 | TRISB=0x00;
|
78 |
|
79 | //Interrupt für TIMER1 um zu sehen das Timer überläuft
|
80 | PIE1bits.TMR1IE = 1; //TIMER1 Interrupt Enable
|
81 |
|
82 | INTCONbits.GIEH=1; //
|
83 | INTCONbits.GIEL=1; //
|
84 | INTCONbits.GIE = 1; //enable unmasked Interrupts Global
|
85 | }
|
86 |
|
87 |
|
88 | /*----------------------------------------------------------------------------
|
89 | ----- -----
|
90 | ----- MAIN -----
|
91 | ----- -----
|
92 | ---------------------------------------------------------------------------*/
|
93 | void main(void)
|
94 | {
|
95 | init();
|
96 | while(1)
|
97 | {
|
98 | TMR1L = 0x00; //setze Timer auf 0
|
99 | TMR1H = 0x00;
|
100 | uint_period1=0;
|
101 | uint_period2=0;
|
102 | ulint_period=0;
|
103 |
|
104 | T1CONbits.TMR1ON = 1; //starte Timer 1
|
105 | CCP1CONbits.CCP1M = 0b111; // Capture bei jeder steigenden Flanke
|
106 |
|
107 | while (!(PIR1bits.CCP1IF)); //Warte auf erste steigende Flanke
|
108 | PIR1bits.CCP1IF = 0;
|
109 | uint_period1 = (unsigned int) CCPR1H; //speichere Zeitpunkt erster Flanke (16Bit-Wert)
|
110 | uint_period1 = uint_period1<<8;
|
111 | uint_period1 = uint_period1 + CCPR1L;
|
112 |
|
113 | while (!(PIR1bits.CCP1IF)); //Warte auf zweite steigende Flanke
|
114 | CCP1CON = 0x00; //schalte Capture aus
|
115 | T1CONbits.TMR1ON = 0; //stoppe Timer 1
|
116 |
|
117 | uint_period2 = (unsigned int) CCPR1H; //speichere Zeitpunkt zweiter Flanke (16Bit-Wert)
|
118 | uint_period2 = uint_period2<<8;
|
119 | uint_period2 = uint_period2 + CCPR1L;
|
120 |
|
121 | ulint_period = (long) uint_period2 - (long) uint_period1; //Zeitdifferenz bilden
|
122 |
|
123 | LCDClear();
|
124 | LCDGoto(0,1);
|
125 | LCDWriteStr("Zeit");
|
126 | LCDGoto(0,0);
|
127 |
|
128 | ultoa(ulint_period, char_Zeit); // unsigend Long to Ascii (String)
|
129 |
|
130 | i=0;
|
131 | while (char_Zeit[i] != 0x00) // Solange nicht NUL
|
132 | {
|
133 | LCDPutChar(char_Zeit[i]);
|
134 | i++;
|
135 | }
|
136 |
|
137 |
|
138 | Delay1KTCYx(100);
|
139 | }
|
140 | }
|