Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-systema para riego automatico-*/
- /*-en este update se reparo el problema del rebote del relay provocado por la hola que produce el removorbedor-*/
- #include <16f628A.h>
- #fuses NOWDT
- #fuses MCLR
- #fuses NOBROWNOUT
- #fuses NOLVP
- #USE DELAY(INTERNAL=4000000)
- #USE FAST_IO (A)
- #USE FAST_IO (B)
- //-"direccionamiento de ports-//
- #byte port_A = 0x05
- #byte port_B = 0x06
- #byte PCON = 0x8e
- #define Timer1 PIN_A0
- #define Timer2 PIN_A1
- #define SetUp PIN_A2
- #define Save PIN_A3
- #define MinLevel PIN_A4
- #define MaxLevel PIN_A6
- #define LedGrow PIN_A7
- #define IE_Pump PIN_B0
- #define Evalve2 PIN_B1
- #define Evalve1 PIN_B2
- #define Evalve3 PIN_B3
- #define Level_1 PIN_B4
- #define Level_2 PIN_B5
- #define Led_Zona2 PIN_B6
- #define LedFlor PIN_B7
- #define false 0
- #define true 1
- /*-"Declaracion de funciones"-*/
- void Retardo_05seg(void);
- void Retardo_03seg(void);
- void Retardo_40seg(void);
- Void tmpVaciado(void);
- /*-"Declaracion de VARIABLES Globales"-*/
- int l=0;
- VOID MAIN(){
- SET_TRIS_A(0x7f);
- SET_TRIS_b(0x00);
- output_b(0);
- output_low(LedGrow);
- //output_high(Level_1);
- //rutina para ver por a donde va el programa despues de un apagon O un reset
- if(!bit_test(PCON,1) && (bit_test(read_eeprom(0x00),0))){ //testeo PCON para saver si fue apagon o reset, PCON==1 RESET, PCON==0 POWER OFF
- BIT_SET(PCON,1);
- output_low(Level_2);
- //output_low(Level_3);
- output_high(Level_1);
- Delay_ms(50);
- if(!bit_test(read_eeprom(0x03),0)){
- output_High(LedGrow);
- goto Growing;
- }
- if(bit_test(read_eeprom(0x05),0)) {
- output_High(LedFlor);
- goto Floration;
- }
- if(!bit_test(read_eeprom(0x04),0)){
- //output_High(LedFlor);
- goto Growing_room2;
- }
- }else if(!bit_test(PCON,1)&& (bit_test(read_eeprom(0x01),0))){
- BIT_SET(PCON,1);
- output_low(Level_1);
- output_high(Level_2);
- Delay_ms(50);
- if(!bit_test(read_eeprom(0x03),0)){
- output_High(LedGrow);
- goto Growing;
- }
- if(bit_test(read_eeprom(0x05),0)) {
- output_High(LedFlor);
- goto Floration;
- }
- if(!bit_test(read_eeprom(0x04),0)){
- goto Growing_room2;
- }
- }else if(bit_test(PCON,1)){ //Si PCON.1==1 esto es un Reset
- //////////////// RUTINA PARA SELECCIONAR EL NIVEL DEL TANQUE\\\\\\\\\\\\\\\\\\\\\
- while(true){
- while(1){
- write_eeprom(0x00,0xff);// 0x00 para el nivel1 // Esatas son las direcciones de memoria a testiar para saber que nivel
- //estaba seleccionado antes del apagon.
- write_eeprom(0x01,0x00);// SET FLAG ,REGISTRO 0x01=0X00 DE LA EEEPROM para el nivel2
- //write_eeprom(0x02,0x00);// 0x02 para el nivel3
- output_low(Level_2);
- //output_low(Level_3);
- output_high(Level_1);
- delay_ms(500);
- do{
- if(!input(Save)){
- //delay_ms(50); //Level-1
- goto Mode;
- }
- }while(input(SetUp));
- write_eeprom(0x00,0x00);
- write_eeprom(0x01,0xff);
- //write_eeprom(0x02,0x00);
- output_low(Level_1);
- //output_low(Level_3);
- output_high(Level_2);
- delay_ms(500);
- do{
- if(!input(Save)){ //Level-2
- //delay_ms(50);
- goto Mode;
- }
- }while(input(SetUp));
- } //End of Select
- //}
- Mode:
- //delay_ms(250);
- //////////////////SUBRRUTINA PARA SELECCIONAR MODO DE TRABAJO///////////
- while(1){
- write_eeprom(0x03,0x00);
- output_Low(LedFlor);
- output_High(LedGrow);
- delay_ms(250);
- do{
- if(!input(Save))
- {
- output_low(LedGrow);
- delay_ms(250);
- output_High(LedGrow);
- goto Growing;
- }
- }while(input(SetUp));
- write_eeprom(0x05,0xff);
- delay_ms(250);
- output_Low(LedGrow);
- output_High(LedFlor);
- do{
- if(!input(Save))
- {
- output_low(LedFlor);
- delay_ms(250);
- output_High(LedFlor);
- goto Floration;
- }
- }while(input(SetUp));
- } //End del Setup.
- } //End deel while
- } // End Del IF-Else.
- /////////////////////////////////////////////////////
- //Rutina que Atiende el llenado del tanque Mas Las Valvulas de Riego y Pumpa.
- Growing:
- write_eeprom(0x04,0xff); // Clear Flag
- write_eeprom(0x05,0x00); // Clear Flag
- while(1){
- do{
- if(input(pin_A6)){ // max level pin_A6 ////////////////////////////////////////////////////////////////////////////////
- output_Low(Pin_b3); // sierro la valvula de llenado // Rutina para el llenado del tanke mientras espero la entrada del timer //
- }else{ // //
- output_High(Pin_b3); // Pin_b3 valbula de llenado ////////////////////////////////////////////////////////////////////////////////
- }
- //}while(input(PIN_A0)||input(PIN_A1));
- }while(input(PIN_A0)); // Timer_1 Pin_A0 / espero trigger Timer1 Zona_1
- output_Low(Pin_b3); // Cierro la Valbula de llenado
- Retardo_40seg(); // Retardo k garantiza k el programa no entre en un loop
- output_low(PIN_B1); // Evalve2 pin_B1 / valbula de riego de Spot_2 // En este punto el pin B1 se pone a 0 para garantizar que la valvula del Cuarto 2 este serrada
- output_high(PIN_B0); // Arranco Bomba de Riego.
- Retardo_05seg(); // Wait 5sec
- output_high(PIN_B2); // Evalve1 pin_B2 / Abro Valvula de riego de Spot_1 // En este punto se comienza la irrigation.
- do{
- output_toggle(LedGrow); // Hago parpadear el led(200ms) para indicar que esta irrigando
- delay_ms(250);
- }while(input(PIN_A4)); // Mini lavel Pin_A4 // En este punto se monitorea el pin_a4 para saber cuando el tanke este un su nivel minimo.
- //dalay_ms(x) // Descomentar Linea Paravaciar el tanke completo.)
- output_high(LedGrow);
- output_Low(PIN_B2); // Sierro la Valbula de Riego del Cuarto_1
- output_Low(PIN_B1); // Sierro la Valvula De Riego del Cuarto_2
- Retardo_03seg();
- output_Low(PIN_B0); // Apago la Bomba de rego
- Retardo_05seg();
- } //End Del Growing
- Floration:
- while(1){
- output_High(LedFlor);
- write_eeprom(0x05,0xff); // Set flag
- write_eeprom(0x03,0xff); // Clear Flag
- write_eeprom(0x04,0xff); // Clear Flag
- do{
- if(input(pin_A6)){ //llenado del Tanque Para riego de Zona_1//la entrada A6 esta connectada al sensor de maximo nivel
- //delay_ms(x); //tiempo introducido para evitar feedback en el sensor de maximo nivel
- output_Low(Pin_b3); //la valvula de llenado esta connectada a la salida B3
- l=1;
- }else if(l==0){
- output_High(Pin_b3);
- }
- if(!input(PIN_A1)){goto IrriG_zona2;} // Si el timer_2 se sierra ir a la zona_2
- // }while(input(PIN_A0)||input(PIN_A1));
- }while(input(PIN_A0)); //espero trigger Timer1 Zona_1
- IrriG_Zona1:
- output_Low(Pin_b3); // Cierro la valvula de llenado
- Retardo_40seg();
- output_low(PIN_B1); // Sierro valbula de riego de Spot_2
- output_high(PIN_B0); // Arranco Bomba de Riego.
- Retardo_05seg(); // Espero 5 seg
- output_high(PIN_B2); // Abro Valvula de riego de Spot_1
- do{
- if(!input(PIN_A1) && (input(pin_A6))) {
- output_low(LedFlor);
- output_Low(Evalve1);
- output_Low(IE_Pump);
- goto IrriG_zona2;
- }ELSE IF(!input(PIN_A1) && (!input(pin_A6))) {
- output_high(Led_Zona2);
- output_low(LedFlor);
- output_Low(Evalve1);
- output_Low(IE_Pump);
- DO{
- output_High(Pin_b3);
- }WHILE(!input(pin_A6));
- output_Low(Pin_b3);
- goto IrriG_zona2;
- }
- output_toggle(LedFlor);
- delay_ms(250);
- }while(input(PIN_A4)); // Sensor Minimo lavel Pin_A4, codicion para Apagar bomba y cierrar Valvulas.
- //tmpVaciado(); // Descomentar Linea Para vaciar el tanke completo.
- //Growing_room2:
- l=0;
- output_low(LedFlor);
- output_Low(Evalve1); /////////////////////////////////////////////////
- output_Low(Evalve2); // //
- Retardo_03seg(); // //
- output_Low(IE_Pump); //Cierro Las Valvulas Y apago la bomba de riego//
- Retardo_05seg(); // //
- /////////////////////////////////////////////////
- Growing_room2:
- output_high(Led_Zona2);
- write_eeprom(0x04,0x00); // Set Flag
- write_eeprom(0x03,0xff); // Clear Flag
- write_eeprom(0x05,0x00); // Clear Flag
- do{
- if(input(pin_A6)){ // llenado del Tanque para Riego de Zona_2
- //delay_ms(x); //tiempo introducido para evitar feedback en el sensor de maximo nivel
- output_Low(Pin_b3);
- l=1; //clear flag, l es una bandera para evitar revote en el relay conectado a B3 cuando el hagua se
- mueve en el tanke.
- }else if(l==0){
- output_High(Pin_b3);
- }
- if(!input(PIN_A0)){goto IrriG_Zona1;}
- }while(input(PIN_A1)); //espero trigger timer2 Zona_2
- IrriG_Zona2:
- output_Low(Pin_b3); // Cierro la Valvula dellenado
- Retardo_40seg();
- output_low(PIN_B2); // Cierro Valve_1_Zona_1
- output_high(PIN_B0); // Arranco la Bomba.
- Retardo_05seg(); // espero 5 seg.
- output_high(PIN_B1); // Abro la valvula_2 Para regar en Zona_2
- do{
- if(!input(PIN_A0) && (input(pin_A6))) {
- output_low(Led_Zona2);
- output_Low(PIN_B1);
- output_Low(PIN_B0);
- goto IrriG_zona1;
- }ELSE IF(!input(PIN_A0) && (!input(pin_A6))) {
- output_High(LedFlor);
- output_low(Led_Zona2);
- output_Low(PIN_B1);
- output_Low(PIN_B0);
- DO{
- output_High(Pin_b3);
- }WHILE(!input(pin_A6));
- output_Low(Pin_b3);
- goto IrriG_zona1;
- }
- output_toggle(Led_Zona2);
- delay_ms(250);
- }while(input(PIN_A4)); // Termino Riego Zona_2
- //tmpVaciado(); // Descomentar Linea Para(espero un tiempo x para vaciar el tanke completo.)
- l=0; // set flag == 0
- output_low(Led_Zona2);
- write_eeprom(0x04,0x00);
- //bit_clear(eeprom 0x04,0);
- output_Low(PIN_B1); // cierro valvula_2_Zona_2
- Retardo_03seg(); // espero_3seg
- output_Low(PIN_B2); // cierro valvula_1_zona_1
- output_Low(PIN_B0); // Apago_la_bomba_de_riego
- Retardo_05seg(); // Retardo de 5seg.
- }
- }
- //funciones
- Void Retardo_05seg(){
- for(int i=1;i<=5;i++){
- for(long j=0;j<=1000;j++){
- delay_ms(1);
- }
- }
- }
- Void Retardo_03seg(){
- for(int i=1;i<=3;i++){
- for(long j=0;j<=1000;j++){
- delay_ms(1);
- }
- }
- }
- //funcion para evitar timer overLap
- Void Retardo_60seg(){
- for(int i=0;i<=59;i++){
- for(long j=0;j<=1000;j++){
- delay_ms(1);
- }
- output_toggle(LedGrow);
- }
- output_low(LedGrow);
- //i=0
- }
- // Retardo para el vaciado del tanke
- Void tmpVaciado(){
- for(int i=0;i<=59;i++){
- for(long j=0;j<=1000;j++){
- delay_ms(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment