1 | #include <avr/io.h>
|
2 | #include "avr/interrupt.h"
|
3 | #include <stdint.h>
|
4 | #include <util/delay.h>
|
5 | #include "avr_compiler.h"
|
6 | #include "function.h"
|
7 | #include "UartHandler.h"
|
8 | #include "usart_driver.h"
|
9 |
|
10 |
|
11 | /***********************************/
|
12 |
|
13 | void funct_port_init (void)
|
14 | {
|
15 |
|
16 | /* Port C - digital I/O */
|
17 | DIGI_PORT.DIRSET = DIGI_PIN1; // Port C Pin 1 digital I/O 1 as output PIN7
|
18 | DIGI_PORT.DIRCLR = DIGI_PIN2; // Port C Pin 3 digital I/O 2 as input PIN9
|
19 | DIGI_PORT.DIRSET = OPTO_PIN; // Port C Pin 0 opto-coupler as digital output
|
20 |
|
21 | PORTC.PIN0CTRL= PORT_OPC_WIREDANDPULL_gc;
|
22 | PORTC.INTCTRL = PORT_INT0LVL_LO_gc | PORT_INT0LVL_MED_gc | PORT_INT0LVL_HI_gc;
|
23 | PORTC.PIN3CTRL = PORT_OPC_PULLUP_gc | PORT_ISC_FALLING_gc;
|
24 | PORTC.INT0MASK = 0x03;
|
25 |
|
26 | }
|
27 |
|
28 |
|
29 | /**********************************/
|
30 |
|
31 |
|
32 | void funct_inter_init(void)
|
33 | {
|
34 | PMIC.CTRL = PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm | PMIC_HILVLEN_bm;
|
35 | }
|
36 |
|
37 | /***********************************/
|
38 |
|
39 | void funct_timer2_init(void)
|
40 | {
|
41 | TCD1.CTRLA = TC_CLKSEL_DIV1_gc;
|
42 | TCD1.CTRLB = TC_WGMODE_NORMAL_gc;
|
43 | TCD1.INTCTRLA = TC_OVFINTLVL_HI_gc;
|
44 | TCD1.PER= 0xFFF;
|
45 | }
|
46 |
|
47 |
|
48 | /***********************************/
|
49 |
|
50 |
|
51 | float zaehler_auswerten(void)
|
52 | {
|
53 | float perioden_dauer = 0;
|
54 | if (PORTC_PIN3CTRL==0) // hier soll der Counter anfangen zu zählen bei fallender Flanke
|
55 |
|
56 | {
|
57 |
|
58 | perioden_dauer = TCD1.CNT*zeit_in_us;
|
59 | TCD1.CNT= 0;
|
60 | zaehler=true;
|
61 | }
|
62 | return perioden_dauer;
|
63 | }
|
64 |
|
65 | /***********************************/
|
66 |
|
67 |
|
68 | ISR(PORTC_INT0_vect) // interrupt für I/O Port
|
69 | {
|
70 | uart_send_string("\ninterrupt ausgeloest\n"); // Diese serielle Ausgabe wird nicht an Termite angezeigt
|
71 |
|
72 | perioden_dauer = zaehler_auswerten();
|
73 | interrupt=true;
|
74 |
|
75 |
|
76 | if (perioden_dauer >= sync_min_1) // synchronization and Communication
|
77 | {
|
78 | synchronisiert_b = true;
|
79 | clock_tick = perioden_dauer*ticks;
|
80 |
|
81 | n_nibble = 0;
|
82 | }
|
83 |
|
84 |
|
85 | }
|
86 |
|
87 | /***********************************/
|
88 |
|
89 | ISR(TCD1_OVF_vect) // Counter Interrupt
|
90 | {
|
91 | uart_send_string("\ninterrupt timer ausgeloest\n");
|
92 |
|
93 | if (interrupt)
|
94 | {
|
95 | TCD1.CNT=0 ;
|
96 | }
|
97 |
|
98 |
|
99 | }
|
100 |
|
101 | /***********************************/
|
102 |
|
103 | int main (void)
|
104 | {
|
105 |
|
106 | sei();
|
107 | funct_clock_init_PLL_60M(); // clock initialization 60 MHz
|
108 |
|
109 |
|
110 |
|
111 | funct_DACB_single0_enable(DAC_REFSEL_AVCC_gc); // initialize DAC with internal Vcc
|
112 |
|
113 | uart_InitUsartDriver(); // initialize RS232 communication
|
114 |
|
115 |
|
116 | DIGI_PORT.OUTCLR =DIGI_PIN1; //digital output port pin 1 LOW
|
117 | DIGI_PORT.OUTCLR = OPTO_PIN; // opto-coupler pin LOW
|
118 |
|
119 | IdleValue=DEFAULT_IDLE_VAL; // analog output -> 5 V (max.: 0x0FFF, 12 bit)
|
120 | Port=SER_PORT_AO0; // set analog output as idle port
|
121 | errorOut=true; // turn on error messages by default
|
122 | debugOut=true; // turn on debug messages by default
|
123 |
|
124 |
|
125 |
|
126 | if (debugOut)
|
127 | {
|
128 | uart_send_string(version);
|
129 | // send version number via RS232
|
130 | uart_send_string("\nSignalgenerator laeuft\n");
|
131 | }
|
132 |
|
133 |
|
134 | funct_timer2_init();
|
135 | uart_send_string("\ntimer2 initialisiert\n");
|
136 | funct_port_init();
|
137 | funct_inter_init();
|
138 |
|
139 | uart_send_string("\nSignalgenerator wurde initialisiert\n");
|
140 |
|
141 |
|
142 | uart_send_string("\nENDE INITIALISIERUNGEN\n");
|
143 |
|
144 | /* main loop */
|
145 | while(1)
|
146 | {
|
147 | }
|
148 | }
|