-
Notifications
You must be signed in to change notification settings - Fork 0
/
Irrigador Automatizado.c
597 lines (540 loc) · 12.1 KB
/
Irrigador Automatizado.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
sistema automatizado de Irrigação
TCC Automação Indistrial 4º Modulo 2015
*/
#include <16f877a.h>
#use delay(clock=4000000)
#fuses HS,NOWDT,PUT,NOLVP,NOCPD
//#include <regs_16f87x.h>
//#include <timer0.h>
#include<Flex_LCD420.c>
//********ENTRADAS BOTOES********************************
#define BT_START_STOP pin_c0 //botao liga/desliga
#define BT_SENSOR1 pin_c1 //botao SENSOR1
#define BT_SENSOR2 pin_c2 //botao SENSOR2
#define BT_SENSOR3 pin_c3 //botao SENSOR3
#define BT_SENSOR4 pin_c4 //botao SENSOR4
#define BT_INCREMENTA pin_c5 //botao INCREMENTA +
#define BT_DECREMENTA pin_c6 //botao DECREMENTA -
//*******************************************************
//*****************ENTRADA SENSORES**********************
#define SENSOR_BATERIA pin_a0 //SENSOR TENSÃO BATERIA
#define SENSOR1 pin_a1 //ENTRADA SENSOR1
#define SENSOR2 pin_a2 //ENTRADA SENSOR2
#define SENSOR3 pin_a3 //ENTRADA SENSOR3
#define SENSOR4 pin_a4 //ENTRADA SENSOR4
//*******************************************************
//***************SAIDAS DRIVERS**************************
#define RELE_BAT pin_b0//SAIDA RELE BATERIA
#define M1 pin_b1//SAIDA MOTOR1
#define M2 pin_b2//SAIDA MOTOR2
#define M3 pin_b3//SAIDA MOTOR3
#define M4 pin_b4//SAIDA MOTOR4
#define LED_BOTOES pin_b6//SAIDA LED BOTOES
#define LED_TIMER pin_b7//SAIDA LED TIMER
//*******************************************************
static boolean led;
static boolean flag_start_stop;
static boolean flag_ligado;
static boolean flag_M1;
static boolean flag_M2;
static boolean flag_M3;
static boolean flag_M4;
int hor=0;
int min=0;
int seg=0;
int seg2=0;
int conta=0;
int Kp; //ganho proporcional
int Ki; //ganho integral
int32 Sp,Dv,Mv,Pv;
int32 set_sensor1, set_sensor2, set_sensor3, set_sensor4;
float ler_sensor1, ler_sensor2, ler_sensor3, ler_sensor4;
float ler_bat;
//*******************************************************
//tratamento do timer contagem de tempo
#int_timer0
void trata_t0()
{
//*************INICIO CONTAGEM DE SEGUNDOS***************
static int conta;
set_timer0(131+get_timer0());//contagem de tempo 1 segundo
conta++;
if(conta==125)
{
conta=0;
led=!led;
output_bit(LED_TIMER,led);
}
if(conta==0)
{
seg=seg+1;
}
if(seg>59)
{
seg=0;
min=min+1;
}
if(min>59)
{
min=0;
hor=hor+1;
}
}
//************FIM INCREMENTO DE TEMPO********************
//*************MENSAGEM DE APRESENTACAO******************
void mensagem_apresenta()
{
int pos;
pos=20;
lcd_putc('\f');
while(pos>=1 && pos<=20 || pos>=20 && pos<=40)
{
lcd_gotoxy(pos,1);
printf(lcd_putc,"*** IRRIGADOR ****\r");
delay_ms(20);
pos=pos-1;
}
pos=20;
while(pos>=1 && pos<=20 || pos>=235 && pos<=255)
{
lcd_gotoxy(pos,2);
printf(lcd_putc,"** AUTOMATIZADO **\r");
delay_ms(20);
pos=pos-1;
}
return;
}
//***************************************************************************
//*******************MENSAGEM CONTAGEM DE TEMPO***************************
void mensagem_tempo()
{
//int pos=0;
// pos=1;
lcd_gotoxy(1,3);
printf(lcd_putc,"Time=%u:%u:%u\r",hor,min,seg);
delay_ms(50);
return;
}
//***************************************************************************
void Menssagem_Display()
{
if(seg2!=seg)
{
seg2=seg;
if(flag_start_stop)//Testa flag start/stop se ligado
{
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"S1=%2.0u%%\b",set_sensor1);
lcd_gotoxy(1,2);
printf(lcd_putc,"S2=%2.0u%%\b",set_sensor2);
lcd_gotoxy(1,3);
printf(lcd_putc,"S3=%2.0u%%\b",set_sensor3);
lcd_gotoxy(1,4);
printf(lcd_putc,"S4=%2.0u%%\b",set_sensor4);
lcd_gotoxy(7,1);
printf(lcd_putc,"V1=%2.0f%%\b",ler_sensor1);
lcd_gotoxy(7,2);
printf(lcd_putc,"V2=%2.0f%%\b",ler_sensor2);
lcd_gotoxy(7,3);
printf(lcd_putc,"V3=%2.0f%%\b",ler_sensor3);
lcd_gotoxy(7,4);
printf(lcd_putc,"V4=%2.0f%%\b",ler_sensor4);
lcd_gotoxy(13,1);
printf(lcd_putc,"M1=%1.0X\b",flag_M1);
lcd_gotoxy(13,2);
printf(lcd_putc,"M2=%1.0X\b",flag_M2);
lcd_gotoxy(13,3);
printf(lcd_putc,"M3=%1.0X\b",flag_M3);
lcd_gotoxy(13,4);
printf(lcd_putc,"M4=%1.0X\b",flag_M4);
lcd_gotoxy(18,1);
printf(lcd_putc,"BAT\b");
lcd_gotoxy(17,2);
printf(lcd_putc,"%2.1f",ler_bat);
lcd_gotoxy(17,3);
printf(lcd_putc,"SIST\b");
lcd_gotoxy(18,4);
printf(lcd_putc,"LIG\b");
delay_ms(50);
}
else
{
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"S1=%2.0u%%\b",set_sensor1);
lcd_gotoxy(1,2);
printf(lcd_putc,"S2=%2.0u%%\b",set_sensor2);
lcd_gotoxy(1,3);
printf(lcd_putc,"S3=%2.0u%%\b",set_sensor3);
lcd_gotoxy(1,4);
printf(lcd_putc,"S4=%2.0u%%\b",set_sensor4);
lcd_gotoxy(7,1);
printf(lcd_putc,"V1=%2.0f%%\b",ler_sensor1);
lcd_gotoxy(7,2);
printf(lcd_putc,"V2=%2.0f%%\b",ler_sensor2);
lcd_gotoxy(7,3);
printf(lcd_putc,"V3=%2.0f%%\b",ler_sensor3);
lcd_gotoxy(7,4);
printf(lcd_putc,"V4=%2.0f%%\b",ler_sensor4);
lcd_gotoxy(13,1);
printf(lcd_putc,"M1=%1.0X\b",flag_M1);
lcd_gotoxy(13,2);
printf(lcd_putc,"M2=%1.0X\b",flag_M2);
lcd_gotoxy(13,3);
printf(lcd_putc,"M3=%1.0X\b",flag_M3);
lcd_gotoxy(13,4);
printf(lcd_putc,"M4=%1.0X\b",flag_M4);
lcd_gotoxy(18,1);
printf(lcd_putc,"BAT\b");
lcd_gotoxy(17,2);
printf(lcd_putc,"%2.1f",ler_bat);
lcd_gotoxy(17,3);
printf(lcd_putc,"SIST\b");
lcd_gotoxy(18,4);
printf(lcd_putc,"DES\b");
delay_ms(50);
}
}
return;
}
//===buzzer====================
void buzzer()
{
int i;
for (i=0; i<=100; i++)
{
output_high(pin_a5);//sinal Alto buzzer
delay_us(400);
output_low(pin_a5);//sinal baixo buzzer
delay_us(400);
output_high(pin_a5);//sinal Alto buzzer
delay_us(400);
output_low(pin_a5);//sinal baixo buzzer
}
return;
}
//*******************************************************
//********************Trata Sensores*********************
void trata_sensores()//leitura dos sensores e comparação
{ //com set-poit
set_adc_channel(0);
delay_ms(10);
ler_bat=read_adc();
ler_bat=(ler_bat/256)*18;
delay_ms(10);
set_adc_channel(1);
delay_ms(10);
ler_sensor1=read_adc();
ler_sensor1=(ler_sensor1/256)*100;
delay_ms(10);
set_adc_channel(2);
delay_ms(10);
ler_sensor2=read_adc();
ler_sensor2=(ler_sensor2/256)*100;
delay_ms(10);
set_adc_channel(3);
delay_ms(10);
ler_sensor3=read_adc();
ler_sensor3=(ler_sensor3/256)*100;
delay_ms(10);
set_adc_channel(4);
delay_ms(10);
ler_sensor4=read_adc();
ler_sensor4=(ler_sensor4/256)*100;
delay_ms(10);
if(set_sensor1>ler_sensor1)//SENSOR1
{
flag_M1=1;//Liga motor M1
}
else
{
flag_M1=0;//Desliga motor M1
}
if(set_sensor2>ler_sensor2)//SENSOR2
{
flag_M2=1;//Liga motor M2
}
else
{
flag_M2=0;//Desliga motor M2
}
if(set_sensor3>ler_sensor3)//SENSOR3
{
flag_M3=1;//Liga motor M3
}
else
{
flag_M3=0;//Desliga motor M3
}
if(set_sensor4>ler_sensor4)//SENSOR4
{
flag_M4=1;//Liga motor M4
}
else
{
flag_M4=0;//Desliga motor M4
}
return;
}
//*******************************************************
//*************Trata saidas Motores**********************
void trata_motores()
{
if(flag_ligado)//Sidas para motores**********************
{
if(flag_M1)//MOTOR1
{
output_high(M1);
delay_ms(Mv);
output_low(M1);
Mv=(Mv/Ki);
output_high(M1);
delay_ms(Mv);
output_low(M1);
}
else
{
output_low(M1);
}
if(flag_M2)//MOTOR2
{
output_high(M2);
delay_ms(Mv);
output_low(M2);
Mv=(Mv/Ki);
output_high(M2);
delay_ms(Mv);
output_low(M2);
}
else
{
output_low(M2);
}
if(flag_M3)//MOTOR3
{
output_high(M3);
delay_ms(Mv);
output_low(M3);
Mv=(Mv/Ki);
output_high(M3);
delay_ms(Mv);
output_low(M3);
}
else
{
output_low(M3);
}
if(flag_M4)//MOTOR4
{
output_high(M4);
delay_ms(Mv);
output_low(M4);
Mv=(Mv/Ki);
output_high(M4);
delay_ms(Mv);
output_low(M4);
}
else
{
output_low(M4);
}
}
return;
}
//*******************************************************
//******************Trata botões*************************
void trata_booes()
{
//*******Entrada Botao Stop/Start********************
if(!input(BT_START_STOP))// Testa botao start/stop se pressionados
{
flag_start_stop=!flag_start_stop;// Altera estado Liga/Desliga sistema
delay_ms(500);
loop1:
output_high(LED_BOTOES);// Liga Led botoes
delay_ms(500);
if(!input(BT_START_STOP))// Testa botao start/stop se presionado
{
if(flag_start_stop)// Testa flag start/stop se ativado
{
flag_start_stop=1;
}
if(!flag_start_stop) // Testa flag start/stop se Desativado
{
flag_start_stop=0;
}
delay_ms(500);
goto loop1;
}
output_low(RELE_BAT);//RELE BATERIA
output_low(M1);//MOTOR1
output_low(M2);//MOTOR2
output_low(M3);//MOTOR3
output_low(M4);//MOTOR4
delay_ms(10);
}
//*******************************************************
output_low(LED_BOTOES);// Desliga Led botoes
//********BOTAO SENSOR1**********************************
if(!input(BT_SENSOR1))
{
output_high(LED_BOTOES);// Liga Led botoes
if(!input(BT_INCREMENTA))
{
if(set_sensor1<=99)
{
set_sensor1=set_sensor1+1;
}
if(set_sensor1>99)
{
set_sensor1=99;
}
delay_ms(300);
}
if(!input(BT_DECREMENTA))
{
if(set_sensor1>1)
{
set_sensor1=set_sensor1-1;
}
if(set_sensor1<1)set_sensor1=1;
delay_ms(300);
}
lcd_gotoxy(1,1);
printf(lcd_putc,"S1=%2.0u%%\b",set_sensor1);
delay_ms(300);
}
//*******************************************************
//********BOTAO SENSOR2**********************************
if(!input(BT_SENSOR2))
{
output_high(LED_BOTOES);// Liga Led botoes
if(!input(BT_INCREMENTA))
{
if(set_sensor2<=99)
{
set_sensor2=set_sensor2+1;
}
if(set_sensor2>99)set_sensor2=99;
delay_ms(300);
}
if(!input(BT_DECREMENTA))
{
if(set_sensor2>1)
{
set_sensor2=set_sensor2-1;
}
if(set_sensor2<1)set_sensor2=1;
delay_ms(300);
}
lcd_gotoxy(1,2);
printf(lcd_putc,"S2=%2.0u%%\b",set_sensor2);
delay_ms(300);
}
//*******************************************************
//********BOTAO SENSOR3**********************************
if(!input(BT_SENSOR3))
{
output_high(LED_BOTOES);// Liga Led botoes
if(!input(BT_INCREMENTA))
{
if(set_sensor3<=99)
{
set_sensor3=set_sensor3+1;
}
if(set_sensor3>99)set_sensor3=99;
delay_ms(300);
}
if(!input(BT_DECREMENTA))
{
if(set_sensor3>1)
{
set_sensor3=set_sensor3-1;
}
if(set_sensor3<1)set_sensor3=1;
delay_ms(300);
}
lcd_gotoxy(1,3);
printf(lcd_putc,"S3=%2.0u%%\b",set_sensor3);
delay_ms(300);
}
//*******************************************************
//********BOTAO SENSOR4**********************************
if(!input(BT_SENSOR4))
{
output_high(LED_BOTOES);// Liga Led botoes
if(!input(BT_INCREMENTA))
{
if(set_sensor4<=99)
{
set_sensor4=set_sensor4+1;
}
if(set_sensor4>99)set_sensor4=99;
delay_ms(300);
}
if(!input(BT_DECREMENTA))
{
if(set_sensor4>1)
{
set_sensor4=set_sensor4-1;
}
if(set_sensor4<1)set_sensor4=1;
delay_ms(300);
}
lcd_gotoxy(1,4);
printf(lcd_putc,"S4=%2.0u%%\b",set_sensor4);
delay_ms(300);
}
return;
}
//*******************************************************
//********************INICIO PROGRAMA********************
//*******************************************************
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
enable_interrupts(GLOBAL|int_timer0);
set_timer0(131);
delay_ms(10);
set_tris_a(0x3F);//Entradas Analogicas
set_tris_b(0x00);//Saidas Drivers
set_tris_c(0x7F);//Entradas Botoes
set_tris_d(0x00);//Saidas Dados Display
set_tris_e(0x07);//Entradas Analogicas
output_b(0x00);//Zeras saida
setup_adc(ADC_CLOCK_INTERNAL);
delay_ms(10);
SETUP_ADC_PORTS(ALL_ANALOG);
set_sensor1=50;
set_sensor2=50;
set_sensor3=50;
set_sensor4=50;
delay_ms(100);
lcd_init();
delay_ms(100);
mensagem_apresenta();
delay_ms(500);
Kp=10;
Ki=10;
while(true)
{
//*****Verifica status de para movimentação dos motores**
if(flag_start_stop)//Testa flag start/stop se ligado
{
flag_ligado=1;
delay_ms(10);
//*******************************************************
}
else
{
flag_ligado=0;
delay_ms(10);
}
trata_booes();
trata_sensores();
trata_motores();
Menssagem_Display();
}
}