-
Notifications
You must be signed in to change notification settings - Fork 0
/
Water_counter.c
319 lines (297 loc) · 7.35 KB
/
Water_counter.c
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <string.h>
/* RS_0 RW_1 E_2 Bit1_3 Bit2_4 Bit3_5 Bit4_6*/
#define RS_1 PORTC = (PORTC & 0b11111110) | 0b00000001
#define RW_1 PORTC = (PORTC & 0b11111101) | 0b00000010
#define E_1 PORTC = (PORTC & 0b11111011) | 0b00000100
#define RS_0 PORTC &= 0b11111110
#define RW_0 PORTC &= 0b11111101
#define E_0 PORTC &= 0b11111011
#define D_SZ 16 //Display string size
char sreg, time_position,change_c,change_h;
char min = 0,hour = 0, sec = 0,flash=0;
unsigned int time = 0;
double cold=0.0, hot=0.0;
char * string;
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
sreg=SREG;
cli();
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
SREG=sreg;
}
unsigned char EEPROM_read(unsigned int uiAddress)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address register */
EEAR = uiAddress;
sreg=SREG;
cli();
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
SREG=sreg;
return EEDR;
}
void WriteCommand(char data) //запись 1-го байта комманды по текущему адресу
{
RS_0;
RW_0;
_delay_ms(0.5);
PORTC = (PORTC & 0b00000111) | ((data >> 1) & 0b01111000);
_delay_ms(0.1);
E_1;
_delay_ms(0.5);
E_0;
_delay_ms(0.1);
PORTC = (PORTC & 0b00000111) | ((data << 3)& 0b01111000);
_delay_ms(0.1);
E_1;
_delay_ms(0.5);
E_0;
RW_1;
PORTC = (PORTC & 0b00000111);
}
void WriteByte(char data) //запись 1-го байта данных по текущему адресу
{
RS_1;
RW_0;
_delay_ms(0.5);
PORTC = (PORTC & 0b00000111) | ((data & 0b11110000) >> 1);
_delay_ms(0.1);
E_1;
_delay_ms(0.5);
E_0;
PORTC = (PORTC & 0b00000111) | ((data & 0b00001111) << 3);
_delay_ms(0.1);
E_1;
_delay_ms(0.5);
E_0;
RW_1;
PORTC = (PORTC & 0b00000111);
}
void WriteString(char position, char * string)
{
WriteCommand(position);
for (int i=0;i<=D_SZ;i++)
{
WriteByte(*(string + i));
}
}
void LCD_init(void)
{
_delay_ms(30);
PORTC = 0b00011000; //DB5=1 DB4=1
_delay_ms(1);
E_1;
_delay_ms(5);
E_0;
RW_1;
_delay_ms(10);
PORTC = 0b00011000; //DB5=1 DB4=1
_delay_ms(1);
E_1;
_delay_ms(5);
E_0;
RW_1;
_delay_ms(10);
PORTC = 0b00011000; //DB5=1 DB4=1
_delay_ms(1);
E_1;
_delay_ms(5);
E_0;
RW_1;
_delay_ms(10);
PORTC = 0b00010000; //DB5=1
_delay_ms(1);
E_1;
_delay_ms(5);
E_0;
RW_1;
_delay_ms(10);
WriteCommand(0b00101000);
_delay_ms(10);
WriteCommand(0b00001000);
_delay_ms(10);
WriteCommand(0b00000001);
_delay_ms(10);
WriteCommand(0b00000110);
_delay_ms(10);
/* PORTC = 0b00010000; //DB5=1
_delay_ms(20);
PORTC = 0b01000000; //DB7=1
_delay_ms(20);
PORTC = 0b00000000;
_delay_ms(20);
PORTC = 0b01000000; //DB7=1
_delay_ms(20);
PORTC = 0b00000000;
_delay_ms(20);
PORTC = 0b00001000; //DB4=1
_delay_ms(20);
PORTC = 0b00000000;
_delay_ms(20);
PORTC = 0b00110000; //DB6=1 DB5=1
RS_0;RW_0;E_1;
_delay_ms(60);
PORTC = 0b00000000;
_delay_ms(20);
PORTC = 0b01111000;
_delay_ms(20);
RW_1; E_0; RS_1;*/
}
void Time_disp(char position)
{
if (sec >= 60)
{
min+=1;
sec=0;
}
if (min == 60)
{
hour+=1;
min=0;
}
if (hour == 24)
{
hour = 0;
}
WriteCommand(position); // Установка начального адреса
WriteByte(0x30+hour/10);
WriteByte(0x30+hour%10);
WriteByte(0x20); // Пробел
WriteByte(0x30+min/10);
WriteByte(0x30+min%10);
if (flash)
{
WriteCommand(time_position + 2);
WriteByte(0x3A); // Двоеточие
_delay_ms(300);
flash = 0;
}
}
void Water_disp(char position)
{
char hundreds,decades,units,fractional,fractional1,fractional2;
hundreds = (char) ( ((int) cold) / 100);
decades = (char) ((((int) cold) % 100) / 10);
units = (char) ( ((int) cold) % 10);
fractional = (cold - hundreds * 100 - decades * 10 - units)*100;
fractional1 = (char) (fractional / 10);
fractional2 = (char) (((int) fractional) % 10);
WriteCommand(position); // Установка начального адреса
WriteByte(0x58);
WriteByte(0x3A);
// WriteCommand(position+2); // Установка начального адреса
WriteByte(0x30+hundreds);
WriteByte(0x30+decades);
WriteByte(0x30+units);
WriteByte(0x2C);
WriteByte(0x30+fractional1);
WriteByte(0x30+fractional2);
WriteByte(0xA1);
WriteByte(0x3A);
// WriteCommand(position+7); // Установка начального адреса
hundreds = (char) ( ((int) hot) / 100);
decades = (char) ((((int) hot) % 100) / 10);
units = (char) ( ((int) hot) % 10);
fractional = (hot - hundreds * 100 - decades * 10 - units)*100;
fractional1 = (char) (fractional / 10);
fractional2 = (char) (((int) fractional) % 10);
WriteByte(0x30+hundreds);
WriteByte(0x30+decades);
WriteByte(0x30+units);
WriteByte(0x2C);
WriteByte(0x30+fractional1);
WriteByte(0x30+fractional2);
}
void My_disp(void)
{
WriteCommand(0b00001100); // Ничего не мигает
WriteCommand(time_position+5);
WriteByte(0x85);
WriteCommand(time_position-1);
WriteByte(0x84);
Time_disp(time_position);
WriteCommand(0b11000000); // Установка начального адреса на начало второй строки
Water_disp(0b11000000);
}
ISR (SIG_OVERFLOW0) /* Обработка прерывания от переполнения счётчика 0-ого таймера */
{
time++;
if (time == 31250)
{
sec+=1;
flash=1;
time=0;
}
}
ISR (SIG_INTERRUPT0)
{
if (change_c == 1)
{
cold+=0.01;
change_c = 0;
}
else change_c = 1;
if (cold >= 1000.00) cold = 0;
}
ISR (SIG_INTERRUPT1)
{
if (change_h == 1)
{
hot+=0.01;
change_h = 0;
}
else change_h = 1;
if (hot >= 1000.00) hot = 0;
}
int main(void)
{
DDRC = 0xff; /* Порты для управления LCD-дисплеем */
PORTC = 0x00;
DDRD = 0x00;
PORTD = 0xff;
// DDRB = 1; // Порт PD0 как вывод
// PORTB = 0x00;
// SFIOR = (SFIOR & !0x04);
// ACSR = 0x80; // Analog Comparator off
// ADCSRA = 0x00; // Analog-to-Digital Converter off
// GIMSK = 0x00;
TCCR0 = 0b00000001; //Timer0 clk/1
TCNT0 = 0x00; //Timer0 start point
TIMSK = (1 << TOIE0); //Timer0 on
MCUCR = 0b00000101; /* Прерывание от INT0 и INT1 генерируется фронтом */
GICR = 0b11000000;
sei(); //SREG = SREG | 0b10000000;
LCD_init();
WriteCommand(0b00001100); // Включение дисплея без курсора, ничего не мигает
string = (char *)calloc((D_SZ+1),sizeof(char));
time_position = 0b10000000+1;
// strcpy(string," ");
while(1)
{
// WriteCommand(0b00000001);
// strcpy(string,"NRNDDA ");
// WriteString(0b11000000,string);
// _delay_ms(1000);
// strcpy(string,"Progs ");
// WriteString(0b11000000,string);
// _delay_ms(1000);
My_disp();
}
free(string);
return 0;
}