1 |
|
2 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
3 |
|
4 | // Hauptprogramm für den ClockClock Master
|
5 |
|
6 | // µC :ATmega32
|
7 |
|
8 | // CPU Takt : 16MHz
|
9 |
|
10 | // Funktionsbeschreibung:
|
11 |
|
12 | //------------------------------
|
13 |
|
14 | // Version: V0.1
|
15 | // Datum: 07.08.12
|
16 |
|
17 |
|
18 |
|
19 | //------------------------------
|
20 |
|
21 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
22 |
|
23 | #include <avr/io.h>
|
24 | #include <avr/interrupt.h>
|
25 | #include "uart.h" //Enthällt uart funktionen (Achtung uart.c muss in makefile eingetragen werden, wenn dies nicht von der ide gemacht wird)
|
26 | #include <stdlib.h> // enthällt die Umwandlung von int zu char (itoa)
|
27 | #include <util/delay.h> // Enthällt delay funktionen
|
28 | #include <avr/wdt.h> // Enthält Watchdog Funktionen
|
29 |
|
30 | // Definitionen
|
31 |
|
32 | #ifndef F_CPU
|
33 | #define F_CPU 8000000UL // CPU Takt in Hz
|
34 | #endif
|
35 | #define UART_BAUD_RATE 9600
|
36 |
|
37 |
|
38 | //Deklaration der Variablen
|
39 |
|
40 | volatile unsigned char count_100ms = 0;
|
41 | volatile unsigned char count_500ms = 0;
|
42 | volatile unsigned char count_1s = 0;
|
43 | volatile unsigned char timestamp = 0; // Zeitstempel byte
|
44 | //( Bit3 = 100ms; Bit4 = 500ms; Bit5= 1s)
|
45 |
|
46 |
|
47 | //Deklaration der Funktionsprototypen
|
48 |
|
49 | void send_int_uart(int value); // Deklaration für UART INt SEND Unterfunktion
|
50 |
|
51 | //Interrupts
|
52 |
|
53 |
|
54 | // Aktion bei Timer 1 Overflow (alle 100ms)
|
55 | ISR (TIMER1_OVF_vect)
|
56 | {
|
57 | ++count_100ms; // Intervallzähler inkrementieren
|
58 | TCNT1 = 53036; // Timer1 vorladen damit er nach 100ms überläuft
|
59 | }
|
60 |
|
61 | // Hauptschleife
|
62 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
63 | int main (void) // Start Hauptschleife
|
64 | {
|
65 |
|
66 | // Eingänge definieren
|
67 |
|
68 |
|
69 |
|
70 | // Ausgänge definieren
|
71 | DDRD |=(1<< PD1); //UART Tx
|
72 | DDRD |=(1<< PD5); //Test Led1
|
73 | DDRD |=(1<< PD6); //Test Led2
|
74 |
|
75 | // UART initialisieren
|
76 |
|
77 | uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
|
78 |
|
79 |
|
80 | // Startkonfig Timer 1
|
81 |
|
82 | TCCR1B |= (1<<CS10); // Prescaler /64 = Overflow alle 100ms und Timer starten
|
83 | TCCR1B |= (1<<CS11); //
|
84 |
|
85 | TIMSK |= (1<<TOIE1); // Overlfow ISR erlauben
|
86 |
|
87 |
|
88 | TCNT1 = 53036; // TImer1 vorladen damit er nach 100ms überläuft
|
89 |
|
90 | // Global Interrupts aktivieren
|
91 | sei();
|
92 |
|
93 |
|
94 |
|
95 | while(1) // Start Dauerschleife
|
96 | {
|
97 |
|
98 | //Zeit setzen
|
99 | if ( count_100ms == 1) // 100ms
|
100 | {
|
101 | timestamp |= ((1 << 3));
|
102 | ++count_500ms;
|
103 | count_100ms = 0;
|
104 | }
|
105 |
|
106 | if ( count_500ms == 5) // 500ms
|
107 | {
|
108 | timestamp |= ((1 << 4));
|
109 | ++count_1s;
|
110 | count_500ms = 0;
|
111 | }
|
112 |
|
113 | if ( count_1s ==2) // 1s
|
114 | {
|
115 | timestamp |= ((1 << 5));
|
116 | count_1s = 0;
|
117 | }
|
118 |
|
119 | //Zeitgesteuerte Aktionen
|
120 |
|
121 | //100ms
|
122 | if ((timestamp & (1<<3)))
|
123 | {
|
124 |
|
125 | timestamp &= ~((1 << 3)); // Zetistempelbit 100ms löschen
|
126 | }
|
127 |
|
128 | //500ms
|
129 | if ((timestamp & (1<<4)))
|
130 | {
|
131 |
|
132 | timestamp &= ~((1 << 4)); // Zetistempelbit 500ms löschen
|
133 | }
|
134 | //1s
|
135 | if ((timestamp & (1<<5)))
|
136 | {
|
137 | PORTD ^= (1 << PD5 ); // Test Led 1 toggeln
|
138 | uart_puts("Test");
|
139 | timestamp &= ~((1 << 5)); // Zetistempelbit 1s löschen
|
140 | }
|
141 | }//Ende Dauerschleife
|
142 | }//Ende Hauptschleife
|
143 |
|
144 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
145 |
|
146 | //Unterfunktion Integerwert über uart ausgeben
|
147 | void send_int_uart(int value)
|
148 | {
|
149 | char buffer [33];
|
150 | itoa (value,buffer,10);
|
151 | uart_puts(buffer);
|
152 | }
|