-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatalogger.cpp
298 lines (266 loc) · 8.84 KB
/
datalogger.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Beschreibung: Zusatzfunktionen f�r Datenlogger-Projekt
Autor: Rolf Rahm
Datum: 01.11.2017
Letzte �nderung: 11.12.2021
*/
#include "controller.h"
#include "datalogger.h"
// Globale Variablen in datalogger.h
// tag,monat,jahr,stunde,minute,sekunde,temperatur
//***************************************************************************
//**** Datenlogger-Funktionen ************************************************
//***************************************************************************
void rs232_print_record(uint8_t i2c_address, uint16_t record)
{
uint8_t dezimale;
eeprom_get_record(EEPROM_1,record);
rs232_printdd(tag); rs232_put('.');
rs232_printdd(monat); rs232_put('.');
rs232_printdd(jahr); rs232_put(' ');
rs232_printdd(stunde); rs232_put(':');
rs232_printdd(minute); rs232_put(':');
rs232_printdd(sekunde); rs232_put(';');
if ((temperatur & 0x80) == 0x80) dezimale = '5';
else dezimale = '0';
if ( temperatur < 0 ) // Temperatur negativ?
{
temperatur = ~temperatur;
temperatur++; // 2er-Komplement
rs232_put('-');
}
rs232_printdd((uint8_t)(temperatur>>8)&0xff); rs232_put(',');
rs232_put(dezimale);
rs232_put('\n');
}
//---------------------------------------------------------------------------
void rs232_set_time(void)
{
uint8_t temp,i;
rs232_print("Uhr stellen [j/n] ?: ");
temp = 'n';
for (i=6;i>0;i--)
{
rs232_put(i-1+'0');
rs232_put('\b');
delay_ms(1000);
temp = rs232_get();
if (temp!='\0') break;
}
rs232_put(temp); // Echo
if ((temp == 'j') || (temp=='J'))
{
rs232_print("\n***** Aktuelles Datum *****");
rs232_print("\nTag [01..31]: ");
tag = rs232_inputdd();
rs232_print("\nMonat [01..12]: ");
monat = rs232_inputdd();
rs232_print("\nJahr [00..99]: ");
jahr = rs232_inputdd();
rs232_print("\nAktuelle Uhrzeit:");
rs232_print("\nStunde [01..24]: ");
stunde = rs232_inputdd();
rs232_print("\nMinuten [00..59]: ");
minute = rs232_inputdd();
sekunde = 00;
rtc_set();
}
}
//---------------------------------------------------------------------------
void eeprom_get_record(uint8_t i2c_address, uint16_t record)
{
volatile uint16_t msb,lsb;
jahr = eeprom_read(i2c_address,record*_REC_SIZE_);
monat = eeprom_read(i2c_address,record*_REC_SIZE_+1);
tag = eeprom_read(i2c_address,record*_REC_SIZE_+2);
stunde = eeprom_read(i2c_address,record*_REC_SIZE_+3);
minute = eeprom_read(i2c_address,record*_REC_SIZE_+4);
sekunde = eeprom_read(i2c_address,record*_REC_SIZE_+5);
msb = eeprom_read(i2c_address,record*_REC_SIZE_+6);
lsb = eeprom_read(i2c_address,record*_REC_SIZE_+7);
temperatur = (msb<<8) | lsb ;
}
//---------------------------------------------------------------------------
void eeprom_set_record(uint8_t i2c_address, uint16_t record)
{
volatile uint8_t test;
eeprom_write(i2c_address,record*_REC_SIZE_ ,jahr);
eeprom_write(i2c_address,record*_REC_SIZE_+1,monat);
eeprom_write(i2c_address,record*_REC_SIZE_+2,tag);
eeprom_write(i2c_address,record*_REC_SIZE_+3,stunde);
eeprom_write(i2c_address,record*_REC_SIZE_+4,minute);
eeprom_write(i2c_address,record*_REC_SIZE_+5,sekunde);
test = (uint8_t)(temperatur >> 8);
eeprom_write(i2c_address,record*_REC_SIZE_+6,test); //msb
test = (uint8_t)(temperatur & 0xff);
eeprom_write(i2c_address,record*_REC_SIZE_+7,test); //lsb
}
//---------------------------------------------------------------------------
void rtc_set()
{
rtc_write(_YEAR_,jahr);
rtc_write(_MONTH_,monat);
rtc_write(_DATE_,tag);
rtc_write(_HR_,stunde);
rtc_write(_MIN_,minute);
rtc_write(_SEC_,sekunde);
}
//---------------------------------------------------------------------------
void rtc_get()
{
jahr = rtc_read(_YEAR_);
monat = rtc_read(_MONTH_);
tag = rtc_read(_DATE_);
stunde = rtc_read(_HR_);
minute = rtc_read(_MIN_);
sekunde = rtc_read(_SEC_);
}
//----------------------------------------------------------------------------
uint16_t read_current_recordnumber_from_rtc(void)
{ uint16_t temp;
temp = rtc_lowlevel_read(RECORD_MSB); //msb
temp <<=8;
temp |= rtc_lowlevel_read(RECORD_LSB); //lsb
return temp;
}
//-----------------------------------------------------------------------------
void write_current_recordnumber_to_rtc(uint16_t record_number)
{
rtc_lowlevel_write(RECORD_MSB,(uint8_t)(record_number>>8) & 0xff);
rtc_lowlevel_write(RECORD_LSB,(uint8_t) record_number & 0xff);
}
//----------------------------------------------------------------------------
uint16_t read_current_sampletime_from_rtc(void)
{ uint16_t temp;
temp = rtc_lowlevel_read(SAMPLETIME_MSB); //msb
temp <<=8;
temp |= rtc_lowlevel_read(SAMPLETIME_LSB); //lsb
return temp;
}
//-----------------------------------------------------------------------------
void write_current_sampletime_to_rtc(uint16_t sample_time)
{
rtc_lowlevel_write(SAMPLETIME_MSB,(uint8_t)(sample_time>>8) & 0xff);
rtc_lowlevel_write(SAMPLETIME_LSB,(uint8_t) sample_time & 0xff);
}
//-----------------------------------------------------------------------------
void serial_print_all_records(void)
{
uint16_t i;
uint8_t dezimale;
int8_t ganzzahl;
uint8_t buffer[6];
delay_ms(20);
for (i=0;i<aktueller_Datensatz;i++)
{
eeprom_get_record(EEPROM_1,i);
rs232_printdd(tag); rs232_put('.');
rs232_printdd(monat); rs232_put('.');
rs232_printdd(jahr); rs232_put(' ');
rs232_printdd(stunde); rs232_put(':');
rs232_printdd(minute); rs232_put(':');
rs232_printdd(sekunde); rs232_put(';');
// Nachkommastelle. Aufl�sung 0,5�C
dezimale = temperatur;
if (dezimale!=0) dezimale = 5; else dezimale = 0;
ganzzahl = temperatur >>8;
sprintf(buffer,"%3d,%1u\n",ganzzahl,dezimale);
rs232_print(buffer);
//rs232_put('\n');
}
}
//-----------------------------------------------------------------------------
void eeprom_speichertest(void)
{
int8_t test = 0;
lcd_setcursor(1,1);
lcd_print("EEPROM #1 test");
test = eeprom_memtest(EEPROM_1);
if (test == -1)
{
lcd_setcursor(1,1);
lcd_print("!Speicherfehler!");
}
//
//lcd_setcursor(1,1);
//lcd_print("EEPROM #2 test");
//test = eeprom_memtest(EEPROM_2);
//lcd_setcursor(1,1);
//lcd_print("EEPROM #3 test");
//test = eeprom_memtest(EEPROM_3);
}
//---------------------------------------------------------------------------
void lcd_print_temperatur(int16_t degree)
{
uint8_t buffer[LCD_LEN+1];
uint8_t dezimale,ganzzahl;
// Nachkommastelle. Aufl�sung 0,5�C
dezimale = degree & 0x80; //nur Bit lsb: 0000 0000 1000 0000
if (dezimale!=0) dezimale = 5; else dezimale = 0;
ganzzahl = degree >>8;
// Zusammenbauen der Zeichenkette mit der Bibliotheksfunktion sprintf()
sprintf(buffer,"%3d,%1u�C",ganzzahl,dezimale);
// Ausgabe auf LC-Display
lcd_print(buffer);
}
//---------------------------------------------------------------------------
void rs232_print_temperatur(int16_t degree, uint8_t mode)
{
uint8_t buffer[LCD_LEN+1];
uint8_t dezimale,ganzzahl;
// Nachkommastelle. Aufl�sung 0,5�C
dezimale = degree & 0x80; //nur Bit lsb: 0000 0000 1000 0000
if (dezimale!=0) dezimale = 5; else dezimale = 0;
ganzzahl = degree >>8;
// Zusammenbauen der Zeichenkette mit der Bibliotheksfunktion sprintf()
//sprintf(buffer,"%3d,%1u�C",ganzzahl,dezimale);
switch (mode)
{
case 0: sprintf(buffer,"T1=%3d.%1u\r\n",ganzzahl,dezimale); break;
case 1: sprintf(buffer,"%3d.%1u\r",ganzzahl,dezimale); break;
case 2: sprintf(buffer,"%3d.%1u�C\r",ganzzahl,dezimale); break;
default: break;
}
// Ausgabe auf LC-Display
rs232_print(buffer); //rs232_put('\n');
}
//---------------------------------------------------------------------------
int16_t lm75_read(void)
{
uint16_t msb,lsb,data16;
i2c_start(); // Startbedingung
i2c_write(LM75_ADDR_W); // Schreibwunsch an LM75 senden
i2c_write(0x00); // Pointer auf 0 setzen
i2c_stop();
i2c_start();
i2c_write(LM75_ADDR_R); // Lesewunsch an LM75 senden
msb = i2c_read(ACK); // MSB lesen und weiteren Wert anfordern.
lsb = i2c_read(NACK); // LSB lesen
i2c_stop(); // Stoppbedingung
msb <<= 8;
data16 = msb | lsb;
data16 &= 0xff80; // niederwertige Bits ausmaskieren!
return data16; // Genauigkeit: +/- 0,5�C
}
//---------------------------------------------------------------------------
uint32_t rs232_get_sampletime(void)
{
volatile uint8_t c,i;
volatile uint32_t buf;
buf = 0, i = 0;
do
{
while((c = rs232_get()) == 0);
if(c == '\n' || c == '\r')
break;
else if ((c >= '0') && (c <= '9'))
{
buf *= 10;
buf += (c-'0');
i++;
rs232_put(c); // Echo
}
} while (i<5);
rs232_put('\n');
return buf;
}