Advertisement
Electgpl

PIC - Control Campana Extractora de Cocina Inteligente

Jun 19th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.74 KB | None | 0 0
  1. //*******************************************************
  2. #include <16F883.h>
  3. #FUSES NOWDT
  4. #FUSES HS
  5. #FUSES MCLR
  6. #use delay(clock=4000000)
  7. #include <LCD.C>
  8. #use rtos(timer=0,minor_cycle=1ms)
  9. //Macros de Salidas
  10. #define SALIDA_LAMPARA  PIN_C1
  11. #define SALIDA_LEDS     PIN_C2
  12. #define SALIDA_BL_LCD   PIN_C3
  13. #define SALIDA_MOTOR_V1 PIN_C4
  14. #define SALIDA_MOTOR_V2 PIN_C5
  15. #define SALIDA_MOTOR_V3 PIN_C6
  16. #define SALIDA_BUZZER   PIN_C7
  17. //Variables globales
  18. int vel_A1=0, vel_A2=0, con_A2=0, con_A21=0, con_A3=0, timer_A1=0,
  19.     con_A31=0, con_A4=0, con_A41=0, valor=0, timer_A4=0, timer_A3=0;
  20. int1 flag_A2=0, flag_A21=0, flag_A3=0, flag_A4=0, flag_motor=0;
  21. //Funcion LCD
  22. #task(rate=10ms,max=1ms)
  23. void f_lcd();
  24. //Funcion Boton1, Enciende o apaga el extractor
  25. #task(rate=10ms,max=1ms)
  26. void f_boton1();
  27. //Funcion Boton2, Maneja 3 velocidades del extractor,
  28. //manteniendolo presionado salta a la mas alta.
  29. #task(rate=10ms,max=1ms)
  30. void f_boton2();
  31. //Funcion Boton3, Enciende o apaga la luz,
  32. //manteniendolo presionado enciende la luz con
  33. //temporizador de apagado automatico.
  34. #task(rate=10ms,max=1ms)
  35. void f_boton3();
  36. //Funcion Boton4, Enciende el extractor con temporizador
  37. //de apagado automatico.
  38. #task(rate=10ms,max=1ms)
  39. void f_boton4();
  40. //Funcion Boton5, Alarma acustia para cocina,
  41. //activa incrementando tiempo.
  42. #task(rate=10ms,max=1ms)
  43. void f_boton5();
  44. //Funcion Boton6, Reset
  45. #task(rate=10ms,max=1ms)
  46. void f_boton6();
  47. //Funcion principal
  48. void main(){
  49.    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  50.    output_low(SALIDA_BUZZER);
  51.    output_high(SALIDA_LEDS);
  52.    output_low(SALIDA_BL_LCD);
  53.    output_low(SALIDA_MOTOR_V1);
  54.    output_low(SALIDA_MOTOR_V2);
  55.    output_low(SALIDA_MOTOR_V3);
  56.    output_low(SALIDA_LAMPARA);
  57.    lcd_init();
  58.    rtos_run();
  59. }
  60. //Funcion LCD
  61. void f_lcd(){
  62.    valor=0;
  63.    if(                    flag_A21) valor=1;
  64.    if(          flag_A3)            valor=2;
  65.    if(          flag_A3 & flag_A21) valor=3;
  66.    if(flag_A4)                      valor=4;
  67.    if(flag_A4 &           flag_A21) valor=5;
  68.    if(flag_A4 & flag_A3)            valor=6;
  69.    if(flag_A4 & flag_A3 & flag_A21) valor=7;
  70.    if((input(PIN_A2)==1)&
  71.       (input(PIN_A3)==1)&
  72.       (input(PIN_A4)==1)){
  73.       switch(valor){
  74.       case 1:
  75.          lcd_gotoxy(1,1);
  76.          printf(lcd_putc," Timer Activado ");
  77.          lcd_gotoxy(1,2);
  78.          printf(lcd_putc,"       Luz      ");
  79.          break;
  80.       case 2:
  81.          lcd_gotoxy(1,1);
  82.          printf(lcd_putc," Timer Activado ");
  83.          lcd_gotoxy(1,2);
  84.          printf(lcd_putc,"   Extractor    ");
  85.          break;
  86.       case 3:
  87.          lcd_gotoxy(1,1);
  88.          printf(lcd_putc," Timer Activado ");
  89.          lcd_gotoxy(1,2);
  90.          printf(lcd_putc,"Luz y Extractor ");
  91.          break;
  92.       case 4:
  93.          lcd_gotoxy(1,1);
  94.          printf(lcd_putc," Timer Activado ");
  95.          lcd_gotoxy(1,2);
  96.          printf(lcd_putc,"     Alarma     ");
  97.          break;
  98.       case 5:
  99.          lcd_gotoxy(1,1);
  100.          printf(lcd_putc," Timer Activado ");
  101.          lcd_gotoxy(1,2);
  102.          printf(lcd_putc,"  Luz y Alarma  ");
  103.          break;
  104.       case 6:
  105.          lcd_gotoxy(1,1);
  106.          printf(lcd_putc," Timer Activado ");
  107.          lcd_gotoxy(1,2);
  108.          printf(lcd_putc,"Extrac. y Alarm.");
  109.          break;
  110.       case 7:
  111.          lcd_gotoxy(1,1);
  112.          printf(lcd_putc," Timer Activado ");
  113.          lcd_gotoxy(1,2);
  114.          printf(lcd_putc,"Extr, Alar y Luz");
  115.          break;
  116.       default:
  117.          break;
  118.       }
  119.    }
  120. }  
  121. //Funcion Boton1, Enciende o apaga el extractor
  122. void f_boton1(){
  123.    if(input(PIN_A0)==0){
  124.       delay_ms(50);
  125.       if(input(PIN_A0)==0){
  126.          output_high(SALIDA_BL_LCD);
  127.          if(flag_motor){
  128.             lcd_gotoxy(1,1);
  129.             printf(lcd_putc,"   Extractor    ");
  130.             lcd_gotoxy(1,2);
  131.             printf(lcd_putc,"    Apagado     ");
  132.             flag_motor=FALSE;
  133.             output_low(SALIDA_MOTOR_V1);
  134.             output_low(SALIDA_MOTOR_V2);
  135.             output_low(SALIDA_MOTOR_V3);
  136.             delay_ms(500);
  137.             con_A31=0;
  138.             flag_A3=0;
  139.             lcd_init();
  140.          }else{
  141.             lcd_gotoxy(1,1);
  142.             printf(lcd_putc,"   Extractor    ");
  143.             lcd_gotoxy(1,2);
  144.             printf(lcd_putc,"   Encendido    ");
  145.             flag_motor=TRUE;
  146.             output_low(SALIDA_MOTOR_V1);
  147.             output_high(SALIDA_MOTOR_V2);
  148.             output_low(SALIDA_MOTOR_V3);
  149.             delay_ms(500);
  150.          }  
  151.       }
  152.    }
  153. }
  154. //Funcion Boton2, Maneja 3 velocidades del extractor,
  155. //manteniendolo presionado salta a la mas alta.
  156. void f_boton2(){
  157.    if(input(PIN_A1)==0){
  158.       delay_ms(100);
  159.       timer_A1++;
  160.       if(timer_A1>5){
  161.          timer_A1=0;
  162.          output_high(SALIDA_BL_LCD);
  163.          output_low(SALIDA_MOTOR_V1);
  164.          output_low(SALIDA_MOTOR_V2);
  165.          output_high(SALIDA_MOTOR_V3);  
  166.          flag_motor=TRUE;
  167.          lcd_gotoxy(1,1);
  168.          printf(lcd_putc,"   Extractor    ");
  169.          lcd_gotoxy(1,2);
  170.          printf(lcd_putc,"Maxima Velocidad");
  171.          delay_ms(500);
  172.          lcd_init();
  173.       }
  174.       delay_ms(400);
  175.       if((input(PIN_A1)==1)&&(timer_A1>0)){
  176.          output_high(SALIDA_BL_LCD);
  177.          ++vel_A1;
  178.          if(vel_A1<4){
  179.             lcd_gotoxy(1,1);
  180.             printf(lcd_putc,"   Extractor    ");
  181.             lcd_gotoxy(1,2);
  182.             printf(lcd_putc,"  Velocidad: %d ",vel_A1);
  183.             delay_ms(450);
  184.             switch(vel_A1){
  185.             case 1:
  186.                flag_motor=TRUE;
  187.                output_high(SALIDA_MOTOR_V1);
  188.                output_low(SALIDA_MOTOR_V2);
  189.                output_low(SALIDA_MOTOR_V3);
  190.                break;
  191.             case 2:
  192.                flag_motor=TRUE;
  193.                output_low(SALIDA_MOTOR_V1);
  194.                output_high(SALIDA_MOTOR_V2);
  195.                output_low(SALIDA_MOTOR_V3);
  196.                break;
  197.             case 3:
  198.                flag_motor=TRUE;
  199.                output_low(SALIDA_MOTOR_V1);
  200.                output_low(SALIDA_MOTOR_V2);
  201.                output_high(SALIDA_MOTOR_V3);
  202.                break;
  203.             default:
  204.                flag_motor=FALSE;
  205.                output_low(SALIDA_MOTOR_V1);
  206.                output_low(SALIDA_MOTOR_V2);
  207.                output_low(SALIDA_MOTOR_V3);
  208.                output_low(SALIDA_BL_LCD);
  209.                break;
  210.             }
  211.          }else{
  212.             output_low(SALIDA_BL_LCD);
  213.             vel_A1=0;
  214.          }
  215.       }
  216.    }
  217. }
  218. //Funcion Boton3, Enciende o apaga la luz,
  219. //manteniendolo presionado enciende la luz con
  220. //temporizador de apagado automatico.
  221. void f_boton3(){
  222.    if(input(PIN_A2)==0){
  223.       delay_ms(50);
  224.       if(input(PIN_A2)==1)
  225.          vel_A2=0;
  226.       vel_A2++;
  227.       output_high(SALIDA_BL_LCD);
  228.       if(input(PIN_A2)==0){
  229.          if(vel_A2<3){
  230.             if(flag_A2==0){
  231.                flag_A2=1;
  232.                flag_A21=0;
  233.                delay_ms(50);
  234.                lcd_gotoxy(1,1);
  235.                printf(lcd_putc,"  Iluminacion   ");
  236.                lcd_gotoxy(1,2);
  237.                printf(lcd_putc,"   Encendida    ");
  238.                output_high(SALIDA_LAMPARA);
  239.                delay_ms(500);
  240.             }else{
  241.                flag_A2=0;
  242.                flag_A21=0;
  243.                delay_ms(50);
  244.                lcd_gotoxy(1,1);
  245.                printf(lcd_putc,"  Iluminacion   ");
  246.                lcd_gotoxy(1,2);
  247.                printf(lcd_putc,"    Apagada     ");
  248.                output_low(SALIDA_LAMPARA);
  249.                delay_ms(500);
  250.                lcd_init();
  251.             }  
  252.          }else{
  253.             if(input(PIN_A2)==0){
  254.                ++con_A2;
  255.                if(con_A2>10){
  256.                   lcd_gotoxy(1,1);
  257.                   printf(lcd_putc,"  Timer de Luz  ");
  258.                   lcd_gotoxy(1,2);
  259.                   printf(lcd_putc," Tiempo: 10 min ");
  260.                   delay_ms(100);
  261.                   flag_A2=1;
  262.                   con_A21=600;
  263.                   lcd_init();
  264.                   flag_A21=1;
  265.                }
  266.             }
  267.          }
  268.       }
  269.    }else{
  270.       if((input(PIN_A2)==1)&
  271.          (input(PIN_A3)==1)&
  272.          (input(PIN_A4)==1)&
  273.          (flag_A21)){
  274.          output_high(SALIDA_LAMPARA);
  275.          flag_A2=1;
  276.          delay_ms(800);
  277.          con_A21--;
  278.          if(con_A21<1){
  279.             flag_A21=0;
  280.             con_A21=0;
  281.             output_low(SALIDA_LAMPARA);
  282.          }
  283.       }
  284.    }  
  285. }
  286. //Funcion Boton4, Enciende el extractor con temporizador
  287. //de apagado automatico.
  288. void f_boton4(){
  289.    if(input(PIN_A3)==0){
  290.       output_high(SALIDA_BL_LCD);
  291.       delay_ms(50);
  292.       timer_A3++;
  293.       if(timer_A3>10){
  294.          flag_A3=0;      
  295.          timer_A3=0;
  296.          con_A3=0;
  297.          con_A31=0;
  298.          output_low(SALIDA_MOTOR_V1);
  299.          output_low(SALIDA_MOTOR_V2);
  300.          output_low(SALIDA_MOTOR_V3);
  301.          flag_motor=FALSE;
  302.          lcd_init();
  303.       }
  304.       delay_ms(400);
  305.       if((input(PIN_A3)==1)&&(timer_A3>0)){
  306.          if(con_A3<60){
  307.             ++con_A3;
  308.             lcd_gotoxy(1,1);
  309.             printf(lcd_putc," Timer Extractor ");
  310.             lcd_gotoxy(1,2);
  311.             printf(lcd_putc," Tiempo: %d min",con_A3*5);
  312.             con_A31=con_A3*300;
  313.             delay_ms(800);
  314.             lcd_init();
  315.             flag_A3=1;
  316.          }else{
  317.             delay_ms(100);
  318.             con_A3=0;
  319.          }
  320.       }
  321.    }else{
  322.       if((input(PIN_A2)==1)&
  323.          (input(PIN_A3)==1)&
  324.          (input(PIN_A4)==1)&
  325.          (flag_A3)){
  326.          flag_motor=TRUE;
  327.          output_low(SALIDA_MOTOR_V1);
  328.          output_high(SALIDA_MOTOR_V2);
  329.          output_low(SALIDA_MOTOR_V3);
  330.          delay_ms(800);
  331.          con_A31--;
  332.          if(con_A31<1 || flag_motor==FALSE){
  333.             flag_A3=0;
  334.             con_A31=0;
  335.             flag_motor=FALSE;
  336.             output_low(SALIDA_MOTOR_V1);
  337.             output_low(SALIDA_MOTOR_V2);
  338.             output_low(SALIDA_MOTOR_V3);
  339.          }          
  340.       }
  341.    }  
  342. }  
  343. //Funcion Boton5, Alarma acustia para cocina,
  344. //activa incrementando tiempo.
  345. void f_boton5(){
  346.    if(input(PIN_A4)==0){
  347.       output_high(SALIDA_BL_LCD);
  348.       delay_ms(50);
  349.       timer_A4++;
  350.       if(timer_A4>5){
  351.          flag_A4=0;      
  352.          timer_A4=0;
  353.          con_A4=0;
  354.          con_A41=0;
  355.          lcd_init();
  356.       }
  357.       delay_ms(400);
  358.       if((input(PIN_A4)==1)&&(timer_A4>0)){
  359.          if(con_A4<60){
  360.             ++con_A4;
  361.             lcd_gotoxy(1,1);
  362.             printf(lcd_putc," Timer de Alarma ");
  363.             lcd_gotoxy(1,2);
  364.             printf(lcd_putc," Tiempo: %d min",con_A4);
  365.             con_A41=con_A4*60;
  366.             delay_ms(800);
  367.             lcd_init();
  368.             flag_A4=1;
  369.          }else{
  370.             delay_ms(100);
  371.             con_A4=0;
  372.          }
  373.       }  
  374.    }else{
  375.       if((input(PIN_A2)==1)&
  376.          (input(PIN_A3)==1)&
  377.          (input(PIN_A4)==1)&
  378.          (flag_A4)){
  379.          delay_ms(800);
  380.          con_A41--;
  381.          if(con_A41<1){
  382.             flag_A4=0;
  383.             con_A41=0;
  384.             output_high(SALIDA_BUZZER);
  385.             delay_ms(500);
  386.             output_low(SALIDA_BUZZER);
  387.             delay_ms(500);
  388.             output_high(SALIDA_BUZZER);
  389.             delay_ms(500);
  390.             output_low(SALIDA_BUZZER);
  391.             delay_ms(500);
  392.             output_high(SALIDA_BUZZER);
  393.             delay_ms(500);
  394.             output_low(SALIDA_BUZZER);
  395.             delay_ms(500);
  396.             output_low(SALIDA_BL_LCD);
  397.             con_A41=0;
  398.             con_A4=0;
  399.          }
  400.       }
  401.    }  
  402. }
  403. //Funcion Boton6, Reset
  404. void f_boton6(){
  405.    if(input(PIN_A5)==0){
  406.       delay_ms(50);
  407.       if(input(PIN_A5)==0){
  408.          reset_cpu();
  409.          output_low(SALIDA_BL_LCD);
  410.       }  
  411.    }      
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement