-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controlador Bomba Diafragma.c
447 lines (403 loc) · 10.6 KB
/
Controlador Bomba Diafragma.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
/*
sistema automatizado de Controle de Irrigação
TCC Automação Indistrial 4º Modulo
*/
#include <16f877a.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,PUT,NOLVP,NOCPD
//#include <regs_16f87x.h>
//#include <timer0.h>
//#include <mod_lcd_877a.c>
#include<Flex_LCD420.c>
//#include <mod_lcd_4x20.c>
/*
#define BT_STOP_START pin_C0 //botao liga/desliga
#define BT_BOBINA pin_C1 //botao bobina
#define BT_FIADOR pin_C2 //botao fiador
#define BT_INC pin_C3 //botao incrementa
#define BT_DEC pin_C4 //botao decrementa
#define BT_ESPIRAS pin_C5 //botao decrementa
#define FIM_CURSO_M2 pin_B0 //Chave fim de curso motor M2
#define ENCODER_M1 pin_B1 //Entrada encoder motor M1
#define ENA pin_B2 //Pino Enable L298 M1
#define ENB pin_B3 //Pino Enable L298 M2
#define IN1 pin_B4 //Pino IN1 L298 M1
#define IN2 pin_B5 //Pino IN2 L298 M1
#define IN3 pin_B6 //Pino IN3 L298 M2
#define IN4 pin_B7 //Pino IN4 L298 M2
#define LED_FIM_CURSO pin_D0 //Led fim de curso sinalizacao
#define LED_BOTOES pin_D1 //Led Botoes presionados
*/
//********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_c5 //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
//*******************************************************
unsigned long int hor=0;
int min=0;
int seg=0;
int seg2=0;
int min2=0;
int conta=0;
long int num_voltas_cont,num_espiras;
long int temp_bobina,temp_fiador;
long int valor;
static boolean led;
static boolean flag_desligado;
static boolean flag_ligado;
static boolean flag_sentido_rotacao_M1;
static boolean flag_stop_start;
static boolean flag_encoder_M1,flag_encoder_M1_ativo;
float resultado1;
//***************************************************************************
//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==158)
{
conta=0;
led=!led;
output_bit(LED_FIM_CURSO,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=16;
//lcd_escreve('\f');
// while(pos>=1 && pos<=16 || pos>=239 && pos<=255)
// {
lcd_gotoxy(1,1);
printf(lcd_putc,"\f*** IRRIGADOR ****");
printf(lcd_putc,"\n** AUTOMATIZADO **");
delay_ms(700);
// pos=pos-1;
// }
return;
}
//***************************************************************************
//***************************************************************************
//MENSAGEM Tempeatura
void mensagem_temperatura()
{
float resultado1;
lcd_gotoxy(1,4);
printf(lcd_putc,"Amp.=%2.3f\n",resultado1);
delay_ms(50);
return;
}
//***************************************************************************
//*******************MENSAGEM CONTAGEM DE TEMPO***************************
void mensagem_tempo()
{
//int pos=0;
// pos=1;
lcd_gotoxy(1,3);
printf(lcd_putc,"Time=%lu:%u:%u\r",hor,min,seg);
delay_ms(50);
return;
}
//***************************************************************************
void Menssagem_Display()
{
if(seg2!=seg)
{
seg2=seg;
if(flag_stop_start)//Testa flag start/stop se ligado
{
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"*** IRRIGADOR ****");
lcd_gotoxy(1,2);
printf(lcd_putc,"** AUTOMATIZADO **");
lcd_gotoxy(1,3);
printf(lcd_putc,"Time=%lu:%u:%u",hor,min,seg);
lcd_gotoxy(1,4);
printf(lcd_putc,"mA=%lu",(valor*17));
//lcd_gotoxy(12,3);
//printf(lcd_putc,"mA=%f",resultado1);
lcd_gotoxy(12,4);
printf(lcd_putc,"M1Rodando");
delay_ms(50);
if((valor*17)>100)
{
output_low(ENA);//sinal Alto enable1 RB2
}
}
else
{
lcd_putc('\f');
//delay_ms(10);
lcd_gotoxy(1,1);
lcd_putc("*** IRRIGADOR ****");
//delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc,"** AUTOMATIZADO **");
//delay_ms(500);
lcd_gotoxy(1,3);
printf(lcd_putc,"Time=%lu:%u:%u",hor,min,seg);
//delay_ms(500);
//lcd_gotoxy(12,3);
//printf(lcd_putc,"mA=%f",resultado1);
lcd_gotoxy(1,4);
printf(lcd_putc,"mA=%lu",(valor*16));
//delay_ms(500);
lcd_gotoxy(12,4);
printf(lcd_putc,"M1Parado ");
delay_ms(500);
}
}
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;
}
//inicio do programa
void main()
{
int32 val32;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
enable_interrupts(GLOBAL|int_timer0);
setup_adc(ADC_CLOCK_INTERNAL);
set_timer0(131);
delay_ms(10);
SETUP_ADC_PORTS(AN0_AN1_AN3);
delay_ms(10);
set_adc_channel(0);
delay_ms(10);
set_tris_a(0x07);//Entrada sensor ampraragem do CI L268
set_tris_b(0x00);//Saida para o CI L198
set_tris_c(0x3F);//entradas Botoes
set_tris_e(0x00);
set_tris_d(0x00);
num_espiras=0;//zera numeros de espira
output_b(0x00);//zera saida port B
output_c(0x00);//zera saida port C
output_d(0x00);//zera saida port D
output_e(0x00);//zera saida port E
flag_encoder_M1=0;//desativa encoder M1
flag_encoder_M1_ativo=1;//seta encoder em ativo
num_voltas_cont=0;//zera o contador de espiras
num_espiras=200;// inicia numero minimo de espiras
flag_sentido_rotacao_M1=1; //inicia rotação do motor M1 sentido horario
flag_stop_start=0; //inicia sistema com stop ativado
temp_bobina=60;// seta minimo de tempo para partida do motor M1
temp_fiador=80;// seta mnimo de tempo para partida do motor M2
flag_desligado=0;
flag_ligado=0;
seg2=59;
min2=1;
//lcd_ini();
delay_ms(100);
lcd_init();
delay_ms(200);
mensagem_apresenta();
//lcd_putc("\f");
// loop enquanto true
output_low(ENA);//sinal Alto enable1 RB2
output_low(ENB);//sinal Alto enable2 RB3
output_low(IN1);//sinal baixo M1 RB4
output_low(IN2);//sinal baixo M1 RB5
output_low(IN3);//sinal baixo M2 RB6
output_low(IN4);//sinal baixo M2 RB7
delay_ms(300);
set_adc_channel(0);
delay_ms(10);
valor=0;
while(true)
{
valor=read_adc();
//if(valor)valor+=1;
val32=valor * 5 +((int32)valor * 113)/128; //converte tensao em temperatura
//val32=valor;
resultado1=val32;
//mensagem_tempo();
//mensagem_temperatura();
//mensagem_apresenta();
//*******Entrada Botao Stop/Start********************
if(!input(BT_STOP_START))// Testa botao start/stop se pressionados
{
flag_stop_start=!flag_stop_start;// Altera estado Liga/Desliga sistema
delay_ms(500);
loop1:
output_high(LED_BOTOES);// Liga Led botoes
delay_ms(500);
if(!input(BT_STOP_START))// Testa botao start/stop se presionado
{
if(flag_stop_start)// Testa flag start/stop se ativado
{
flag_stop_start=1;
}
if(!flag_stop_start) // Testa flag start/stop se Desativado
{
flag_stop_start=0;
}
delay_ms(500);
goto loop1;
}
output_low(ENA);//sinal Alto enable1 RB2
output_low(ENB);//sinal Alto enable2 RB3
output_low(IN1);//sinal baixo M1 RB4
output_low(IN2);//sinal baixo M1 RB5
output_low(IN3);//sinal baixo M2 RB6
output_low(IN4);//sinal baixo M2 RB7
output_low(LED_BOTOES);
delay_ms(50);
}
//***************************************************************
/*
if(num_voltas_cont==1000)
{
num_voltas_cont=0;
flag_sentido_rotacao_M1=!flag_sentido_rotacao_M1;
}*/
if(flag_sentido_rotacao_M1)//Testa flag sentido do motor
{
//sentido horario
output_high(IN1);//sinal Alto IN1 RB4
output_low(IN2);//sinal baixo IN2 RB5
}
else
{
//sentido anti horario
output_high(IN2);//sinal Alto IN2 RB5
output_low(IN1);//sinal baixo IN1 RB4
}
//*****Verifica status de para movimentação dos motores***********
if(flag_stop_start)//Testa flag start/stop se ligado
{
//da pulso no enable motor M1 Roda o motor******************
output_high(ENA);//sinal Alto ENA L298 RB2
//delay_ms(temp_bobina);
//output_low(ENA);//sinal baixo ENA L298 RB2
//flag_ligado=1;
//num_voltas_cont=num_voltas_cont+1;
if((valor*17)>100)
{
output_low(ENA);//sinal Alto enable1 RB2
}
//*************************************************************
}
else
{
//********** travar motores *******************************************
output_low(ENA);//sinal Alto enable1
output_low(ENB);//sinal Alto enable2
output_low(pin_B4);//sinal baixo M1
output_low(pin_B5);//sinal baixo M1
output_low(pin_B6);//sinal baixo M2
output_low(pin_B7);//sinal baixo M2
//delay_ms(30);
}
//********botao seta tempo bobina
if(!input(BT_BOBINA))
{
output_high(LED_BOTOES);// Liga Led botoes
if(!input(BT_INC))
{
temp_bobina=temp_bobina+1;
delay_ms(300);
}
if(!input(BT_DEC))
{
temp_bobina=temp_bobina-1;
delay_ms(300);
}
lcd_gotoxy(1,3);
printf(lcd_putc,"Temp M1=%lu \b",temp_bobina);
delay_ms(500);
}
//********botao seta tempo fiador
if(!input(BT_FIADOR))
{
output_high(LED_BOTOES);// Liga Led botoes
if(!input(BT_INC))
{
temp_fiador=temp_fiador+1;
delay_ms(300);
}
if(!input(BT_DEC))
{
temp_fiador=temp_fiador-1;
delay_ms(300);
}
lcd_gotoxy(1,3);
printf(lcd_putc,"Temp Fia.=%lu \b",temp_fiador);
delay_ms(500);
}
//********botao seta quantidade de espiras
if(!input(BT_ESPIRAS))
{
output_high(LED_BOTOES);// Liga Led botoes
if(!input(BT_INC))
{
num_espiras=num_espiras+1;
delay_ms(300);
}
if(!input(BT_DEC))
{
num_espiras=num_espiras-1;
delay_ms(300);
}
lcd_gotoxy(1,3);
printf(lcd_putc,"NUM ESP.=%lu \r",num_espiras);
delay_ms(500);
}
if(BT_ESPIRAS & BT_FIADOR & BT_BOBINA)output_LOW(LED_BOTOES);// Desliga Led botoes
Menssagem_Display();
}
}