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 | #include <cc_com.h> // Definitionen für die I2C Kommunikation zwischen den Baugruppen
|
30 |
|
31 | #include <avr/pgmspace.h>
|
32 | #include "i2cmaster.h"
|
33 |
|
34 | // Definitionen
|
35 |
|
36 | #ifndef F_CPU
|
37 | #define F_CPU 16000000UL // CPU Takt in Hz
|
38 | #endif
|
39 | #define UART_BAUD_RATE 9600
|
40 |
|
41 |
|
42 |
|
43 | //Deklaration der Variablen
|
44 |
|
45 | volatile unsigned char count_100ms = 0;
|
46 | volatile unsigned char count_500ms = 0;
|
47 | volatile unsigned char count_1s = 0;
|
48 | volatile unsigned char timestamp = 0; // Zeitstempel byte
|
49 | //( Bit3 = 100ms; Bit4 = 500ms; Bit5= 1s)
|
50 | volatile unsigned char ccs_data = 0; // Buffer zur Speicherung der vom Slave emfpangenen Daten
|
51 | int data =0;
|
52 |
|
53 | //Deklaration der Funktionsprototypen
|
54 |
|
55 | void send_int_uart(int value); // Deklaration für UART INt SEND Unterfunktion
|
56 |
|
57 |
|
58 | void I2CWrite_CCS(uint8_t ADDR, uint8_t CMD, uint8_t DATA) // Funktion zum senden von Daten an I2C Slave ( CCS = ClocClock Slave)
|
59 | {
|
60 | i2c_start_wait(ADDR+I2C_WRITE); // set device address and write mode
|
61 | i2c_write(CMD); // write command
|
62 | i2c_write(DATA); // write data
|
63 | i2c_stop(); // set stop conditon = release bus
|
64 | }
|
65 |
|
66 |
|
67 | unsigned char I2CRead_CCS(uint8_t ADDR, uint8_t CMD) // Funktion zum lesen von Daten aus I2C Slave ( CCS = ClocClock Slave)
|
68 | {
|
69 | int data;
|
70 | i2c_start_wait(ADDR+I2C_WRITE); // set device address and write mode
|
71 | i2c_write(CMD); // write command, sendet Startpostion der Leseadresse an Slave
|
72 | i2c_rep_start (ADDR+I2C_READ); // Repeated Start
|
73 | data = i2c_readNak(); // Liest akutelle Bufferposition vom Slave aus und schrebit sie in die variable "ccs_data"
|
74 | i2c_stop(); // set stop conditon = release bus
|
75 | return data; // Gibt das gelesene Byte an funktion zurück
|
76 | }
|
77 |
|
78 |
|
79 | //Interrupts
|
80 |
|
81 |
|
82 | // Aktion bei Timer 1 Overflow (alle 100ms)
|
83 | ISR (TIMER1_OVF_vect)
|
84 | {
|
85 | ++count_100ms; // Intervallzähler inkrementieren
|
86 | TCNT1 = 40536; // Timer1 vorladen damit er nach 100ms überläuft
|
87 | }
|
88 |
|
89 | // Hauptschleife
|
90 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
91 | int main (void) // Start Hauptschleife
|
92 | {
|
93 |
|
94 | // Eingänge definieren
|
95 |
|
96 |
|
97 |
|
98 | // Ausgänge definieren
|
99 | DDRD |=(1<< PD1); //UART Tx
|
100 | DDRD |=(1<< PD5); //Test Led1
|
101 | DDRD |=(1<< PD6); //Test Led2
|
102 |
|
103 | // UART initialisieren
|
104 |
|
105 | uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
|
106 |
|
107 |
|
108 | // Startkonfig Timer 1
|
109 |
|
110 | TCCR1B |= (1<<CS10); // Prescaler /64 = Overflow alle 100ms und Timer starten
|
111 | TCCR1B |= (1<<CS11); //
|
112 |
|
113 | TIMSK |= (1<<TOIE1); // Overlfow ISR erlauben
|
114 |
|
115 |
|
116 | TCNT1 = 40536; // TImer1 vorladen damit er nach 100ms überläuft
|
117 |
|
118 | // I2C initialisieren
|
119 |
|
120 | i2c_init();
|
121 |
|
122 |
|
123 | // Global Interrupts aktivieren
|
124 | sei();
|
125 |
|
126 |
|
127 |
|
128 | while(1) // Start Dauerschleife
|
129 | {
|
130 |
|
131 | //Zeit setzen
|
132 | if ( count_100ms == 1) // 100ms
|
133 | {
|
134 | timestamp |= ((1 << 3));
|
135 | ++count_500ms;
|
136 | count_100ms = 0;
|
137 | }
|
138 |
|
139 | if ( count_500ms == 5) // 500ms
|
140 | {
|
141 | timestamp |= ((1 << 4));
|
142 | ++count_1s;
|
143 | count_500ms = 0;
|
144 | }
|
145 |
|
146 | if ( count_1s ==2) // 1s
|
147 | {
|
148 | timestamp |= ((1 << 5));
|
149 | count_1s = 0;
|
150 | }
|
151 |
|
152 |
|
153 | // Test Taster
|
154 |
|
155 | if((PIND &(1<<PD2)))
|
156 | {
|
157 |
|
158 | for(int address = CCS_ADDRESS_START ; address<= CCS_ADDRESS_END;address++)
|
159 | {
|
160 | uart_puts("Statusabfrage von Slaveadresse:");
|
161 | send_int_uart(address);
|
162 | ccs_data = I2CRead_CCS(address,CCS_STATUS);// Status des akutellen Slaves prüfen
|
163 | uart_puts("Status:");
|
164 | send_int_uart(ccs_data);
|
165 | uart_puts("\n\r");
|
166 |
|
167 | if(ccs_data & CCS_NEW_DATA)
|
168 |
|
169 | {
|
170 | // Wenn noch Daten vom Slave verarbeitet werden passiert nix
|
171 | uart_puts("Slaveadresse:");
|
172 | send_int_uart(address);
|
173 | uart_puts("verarbeit noch alte Daten");
|
174 | uart_puts("\n\r");
|
175 |
|
176 | }
|
177 |
|
178 | else
|
179 |
|
180 | {
|
181 | data = 0;
|
182 |
|
183 | uart_puts("Neue Daten werden an Slave gesendet...");
|
184 | uart_puts("\n\r");
|
185 |
|
186 | for (int i = CC_DATA_START; i<= CC_DATA_END; i++)
|
187 | {
|
188 | ++data; // Testdaten
|
189 | I2CWrite_CCS(address, i, data);
|
190 | uart_puts("Block Nr.");
|
191 | send_int_uart(i);
|
192 | uart_puts(", Inhalt:");
|
193 | send_int_uart(data);
|
194 | uart_puts("\n\r");
|
195 | }
|
196 | I2CRead_CCS(address,CCS_STATUS);// Status des akutellen Slaves prüfen
|
197 | ccs_data |= CCS_NEW_DATA;
|
198 | I2CWrite_CCS(address, CCS_STATUS, ccs_data); // Für Slave markieren das neue Daten da sind
|
199 | }
|
200 | }
|
201 | }
|
202 |
|
203 |
|
204 | //Zeitgesteuerte Aktionen
|
205 |
|
206 | //100ms
|
207 | if ((timestamp & (1<<3)))
|
208 | {
|
209 |
|
210 | timestamp &= ~((1 << 3)); // Zetistempelbit 100ms löschen
|
211 | }
|
212 |
|
213 | //500ms
|
214 | if ((timestamp & (1<<4)))
|
215 | {
|
216 |
|
217 | timestamp &= ~((1 << 4)); // Zetistempelbit 500ms löschen
|
218 | }
|
219 | //1s
|
220 | if ((timestamp & (1<<5)))
|
221 | {
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | timestamp &= ~((1 << 5)); // Zetistempelbit 1s löschen
|
227 | }
|
228 | }//Ende Dauerschleife
|
229 | }//Ende Hauptschleife
|
230 |
|
231 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
232 |
|
233 | //Unterfunktion Integerwert über uart ausgeben
|
234 | void send_int_uart(int value)
|
235 | {
|
236 | char buffer [33];
|
237 | itoa (value,buffer,10);
|
238 | uart_puts(buffer);
|
239 | }
|