Autor Tema: Una funcion muy simple para calcular frecuencia no anda en el programa completo  (Leído 2797 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Hola, primero que nada gracias al foro, siempre me dan una mano.
Les arrojo un problemilla::D
Tengo un programa bastante sencillo que calcula la frecuencia en ccp1.
El programa ese pelado funciona bien, calcula frecuencia correctamente, pero cuando lo pongo en uno mas grande, que es en el que estoy trabajando solo mide 20 hz, con o sin señal sobre ccp1.
Este es el programa simple:
Código: C
  1. #include <18F452.h>
  2. #fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
  3. #use delay(clock = 20000000)
  4. #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
  5.  
  6. int16 isr_ccp_delta;
  7.  
  8. #int_ccp1
  9. void ccp1_isr(void)
  10. {
  11. int16 current_ccp;
  12. static int16 old_ccp = 0;
  13.  
  14. current_ccp = CCP_1;    
  15.  
  16. isr_ccp_delta = current_ccp - old_ccp;
  17.  
  18. old_ccp = current_ccp;
  19. }
  20.  
  21.  
  22. //=======================
  23. void main()
  24. {
  25. int16 current_ccp_delta;
  26. int16 frequency;
  27.  
  28. set_timer1(0);            
  29. setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);  
  30. setup_ccp1(CCP_CAPTURE_RE);    
  31.  
  32. clear_interrupt(INT_CCP1);
  33. enable_interrupts(INT_CCP1);
  34. enable_interrupts(GLOBAL);
  35.  
  36. while(1)
  37.   {
  38.    disable_interrupts(GLOBAL);
  39.    current_ccp_delta = isr_ccp_delta;
  40.         enable_interrupts(GLOBAL);
  41.    
  42.    frequency = (int16)((5000000L/8) / current_ccp_delta);
  43.  
  44.    printf("%lu Hz\n\r", frequency);
  45.  
  46.    delay_ms(500);
  47.        
  48.            
  49.   }
  50.  
  51. }
y este otro es un programa mas grande, que tiene adentro el programa que puse anteriormente. Sin modificaciones, solo el main del pequeño esta metido en parte en una funcion en el mas grande. jaja, complicado.
Aca va el grande (que no mide frecuencia):
Código: C
  1. #if defined(__PCH__)
  2. #include <18F452.h>
  3. //////// Fuses: LP,XT,HS,RC,EC,EC_IO,H4,RC_IO,PROTECT,NOPROTECT,OSCSEN
  4. //////// Fuses: NOOSCSEN,NOBROWNOUT,BROWNOUT,WDT1,WDT2,WDT4,WDT8,WDT16,WDT32
  5. //////// Fuses: WDT64,WDT128,WDT,NOWDT,BORV20,BORV27,BORV42,BORV45,PUT,NOPUT
  6. //////// Fuses: CCP2C1,CCP2B3,NOSTVREN,STVREN,NODEBUG,DEBUG,NOLVP,LVP,WRT
  7. //////// Fuses: NOWRT,NOWRTD,WRTD,NOWRTB,WRTB,WRTC,NOWRTC,CPD,NOCPD,CPB
  8. //////// Fuses: NOCPB,EBTR,NOEBTR,EBTRB,NOEBTRB
  9. #fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
  10. /*
  11. #FUSES NOWDT                    //No Watch Dog Timer
  12. #FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
  13. #FUSES NOPUT                    //No Power Up Timer
  14. #FUSES NOPROTECT                //Code not protected from reading
  15. #FUSES NODEBUG                  //No Debug mode for ICD
  16. #FUSES NOBROWNOUT               //No brownout reset
  17. #FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
  18. #FUSES NOCPD                    //No EE protection
  19. #FUSES NOWRT                    //Program memory not write protected
  20. #FUSES NOOSCSEN
  21. #FUSES NOPUT
  22. #FUSES CCP2C1
  23. #FUSES NOSTVREN
  24. #FUSES NODEBUG
  25. #FUSES NOWRTD
  26. #FUSES NOWRTB
  27. #FUSES NOWRTC
  28. #FUSES NOCPB
  29. #FUSES NOEBTR
  30. #FUSES NOEBTRB
  31. */
  32.  
  33. #use delay(clock=20000000)
  34.  
  35. #include "HDM64GS12_scanbang.c"
  36. #include "graphics.c"
  37. #include "math.h"
  38. #include "24256.c"
  39. #include <string.h>
  40. /// para conteo de uptime
  41. #define CLOCKS_PER_SECOND  1000 ///contar cada tantos milisegundos (uptime)
  42. #define CLOCKS_PER_SECOND_2  1000 ///contar cada tantos milisegundos (grupo_start)
  43. #include <stdlib.h>
  44. // fin de conteo uptime
  45. ///para la eeprom interna del pic
  46. //#include "stdlib.h"
  47. #include "limits.h"
  48. typedef int8 INTEE;
  49. ///fin eeprom interna del pic
  50. //////para medir frecuencia
  51. //#priority CCP1, TIMER1
  52. #define BytePtr(var, offset) (char *)((char*)&var + offset)
  53. //#byte PIR1 = 0x0C
  54. //#bit  TMR1IF = PIR1.0
  55. //int8  gc_timer1_extension = 0;
  56. ///////fin para medir frecuencia
  57.  
  58. int m, p, l, h; //m se utiliza para el menu, p para la pantalla, l para longitud de texto, y c aun no se, h es para mantener el estado ultimo del encendido grupo
  59. int lo = 0; //para el seteo de temperatura ultima seleccion marcada
  60. int set_ = 0; //para el seteo de temperaturas, boton set tratar de meterlo en short
  61. int16 t = 0; //para el uptime variable de comparacion
  62. unsigned int16 c_state = 0; //para almacenar estado de switch start
  63. unsigned int16 minutos5 = 0; //para el uptime variable de valor en fraciones de 5 minutos
  64. unsigned int16 min_g = 0; // para conteo de tiempo entre encendidos de grupo
  65. unsigned int16 time_g = 0;
  66. //unsigned int vref = 0; // se mueve dentro de la pantalla 3
  67. //int up_down_vref = 0;
  68. int16 isr_ccp_delta; //para la frecuencia
  69. float Values; //para el adc
  70. char Texto[30];
  71. short int dbutton4, dbutton5, dbutton6, dbutton7, start_hours, eg;
  72.  
  73. ////prototipos
  74. float read_analog(int a);
  75. int menu(int p);
  76. int16 read_frequency(void);
  77. void write_eeprom16(int address1, int address2, int16 value);
  78. int16 read_eeprom16(int address1, int address2);
  79. void iniciar_eeprom(int e);
  80. void Pantalla_3(int o);
  81. int check_temp(int sen);
  82. void buzzer();
  83. void alertas(int tip);
  84. ///////fin de los prototipos
  85.  
  86. //_______________________________________________________________________________________
  87. void welcome(void){
  88.  
  89.         delay_ms(50);
  90.         Texto = "Empresa";
  91.         glcd_text57(0, 0, Texto, 2, ON);
  92.         Texto = "ELECTRONIC";
  93.         glcd_text57(0, 20, Texto, 2, ON);
  94. }
  95. //_______________________________________________________________________________________
  96. void Pantalla_0(){
  97.  
  98.         if (p == 1 || p == 2 || p == 3) {
  99.                 glcd_fillScreen(OFF);
  100.                 m = menu(0);
  101.         }
  102.         extern float Values;
  103.         extern float read_analog(int a);
  104.         extern char Texto[];
  105.  
  106.         Texto = "----TEMPERATURAS----";
  107.         glcd_text57(0, 0, Texto, 1, ON);
  108.         Texto = "Externa:";
  109.         glcd_text57(0, 10, Texto, 1, ON);
  110.         Texto = "Interna:";
  111.         glcd_text57(0, 20, Texto, 1, ON);
  112.         Texto = "Motor";
  113.         glcd_text57(0, 30, Texto, 1, ON);
  114.         Texto = "Alternador:";
  115.         glcd_text57(0, 40, Texto, 1, ON);
  116.  
  117.         Values =        read_analog(0) * (5.0 * 100.0 / 1024.0);
  118.         sprintf(Texto,"%3.1fC",Values);
  119.         l = strlen(Texto);
  120.         glcd_rect(80, 17, 127, 10, YES, OFF);
  121.         if (l == 4){glcd_text57(92, 10, Texto, 1, ON);}
  122.         if (l == 5){glcd_text57(86, 10, Texto, 1, ON);}
  123.         if (l == 6){glcd_text57(80, 10, Texto, 1, ON);}
  124.  
  125.         Values =        read_analog(1) * (5.0 * 100.0 / 1024.0);
  126.         sprintf(Texto,"%3.1fC",Values);
  127.         //      c_state = read_analog(8);
  128.         //sprintf(Texto ,"%3.1fC", Values);
  129.         l = strlen(Texto);
  130.         glcd_rect(80, 27, 127, 20, YES, OFF);
  131.         if (l == 4){glcd_text57(92, 20, Texto, 1, ON);}
  132.         if (l == 5){glcd_text57(86, 20, Texto, 1, ON);}
  133.         if (l == 6){glcd_text57(80, 20, Texto, 1, ON);}
  134.  
  135.         Values =        read_analog(2) * (5.0 * 100.0 / 1024.0);
  136.         sprintf(Texto,"%3.1fC",Values);
  137.         l = strlen(Texto);
  138.         glcd_rect(80, 37, 127, 30, YES, OFF);
  139.         if (l == 4){glcd_text57(92, 30, Texto, 1, ON);}
  140.         if (l == 5){glcd_text57(86, 30, Texto, 1, ON);}
  141.         if (l == 6){glcd_text57(80, 30, Texto, 1, ON);}
  142.  
  143.         Values =        read_analog(3) * (5.0 * 100.0 / 1024.0);
  144.         sprintf(Texto,"%3.1fC",Values);
  145.         l = strlen(Texto);
  146.         glcd_rect(80, 47, 127, 40, YES, OFF);
  147.         if (l == 4){glcd_text57(92, 40, Texto, 1, ON);}
  148.         if (l == 5){glcd_text57(86, 40, Texto, 1, ON);}
  149.         if (l == 6){glcd_text57(80, 40, Texto, 1, ON);}
  150.  
  151.         p = 0;
  152. }
  153. //_______________________________________________________________________________________
  154. void Pantalla_1(){
  155.  
  156.         if (p == 0 || p == 2 || p == 3) {
  157.                 glcd_fillScreen(OFF);
  158.                 m = menu(1);
  159.         }
  160.         extern float Values;
  161.         extern float read_analog(int a);
  162.         extern char Texto[];
  163. //      extern int16 read_frequency(void);
  164.         unsigned int16 frecuencia;
  165.  
  166.         Texto = "----ESTADO GRUPO----";
  167.         glcd_text57(0, 0, Texto, 1, ON);
  168.         Texto = "VCA:";
  169.         glcd_text57(0, 10, Texto, 1, ON);
  170.         Texto = "Frecuencia:";
  171.         glcd_text57(0, 20, Texto, 1, ON);
  172.         Texto = "Monoxido:";
  173.         glcd_text57(0, 30, Texto, 1, ON);
  174.         Texto = "Horas uso:";
  175.         glcd_text57(0, 40, Texto, 1, ON);
  176.  
  177.         Values =        read_analog(4) * (260.0 / 1024.0);
  178.         sprintf(Texto,"%3.1fV",Values);
  179.         l = strlen(Texto);
  180.         glcd_rect(80, 17, 127, 10, YES, OFF);
  181.         if (l == 4){glcd_text57(92, 10, Texto, 1, ON);}
  182.         if (l == 5){glcd_text57(86, 10, Texto, 1, ON);}
  183.         if (l == 6){glcd_text57(80, 10, Texto, 1, ON);}
  184.         ///adicional para saber si empezar a contar despues veo donde lo meto
  185. //      if (Values > 100) {start_hours = 1; eg = 1;} else {start_hours = 0; eg = 0;} // lo saque de aca porque solo funcionaba en esta pantalla !!
  186.  
  187.         frecuencia = read_frequency();
  188.         sprintf(Texto,"%3luHz",frecuencia);
  189.         l = strlen(Texto);
  190.         glcd_rect(80, 27, 127, 20, YES, OFF);
  191.         if (l == 4){glcd_text57(98, 20, Texto, 1, ON);}
  192.         if (l == 5){glcd_text57(92, 20, Texto, 1, ON);}
  193.         if (l == 6){glcd_text57(86, 20, Texto, 1, ON);}
  194.  
  195.         Values =        read_analog(5) * (5.0 * 100.0 / 1024.0);
  196.         sprintf(Texto,"%3.1fPPM",Values);
  197.         l = strlen(Texto);
  198.         glcd_rect(80, 37, 127, 30, YES, OFF);
  199.         if (l == 6){glcd_text57(92, 30, Texto, 1, ON);}
  200.         if (l == 7){glcd_text57(86, 30, Texto, 1, ON);}
  201.         if (l == 8){glcd_text57(80, 30, Texto, 1, ON);}
  202.  
  203.         sprintf(Texto,"%5LuHs",minutos5); //cambiar variable a horas if minutos5 >= 60 horas++ dentro de timer
  204.         l = strlen(Texto);
  205.         glcd_rect(80, 47, 127, 40, YES, OFF);
  206.         if (l == 5){glcd_text57(92, 40, Texto, 1, ON);}
  207.         if (l == 6){glcd_text57(86, 40, Texto, 1, ON);}
  208.         if (l == 7){glcd_text57(80, 40, Texto, 1, ON);}
  209.  
  210.         p = 1;
  211. }
  212. //_______________________________________________________________________________________
  213. void Pantalla_2(){
  214.  
  215.         if (p == 1 || p == 0 || p == 3) {
  216.         glcd_fillScreen(OFF);
  217.         m = menu(2);
  218.         }
  219.         extern float Values;
  220.         extern float read_analog(int a);
  221.         extern char Texto[];
  222.  
  223.         Texto = "-------MONITOR-------";
  224.         glcd_text57(0, 0, Texto, 1, ON);
  225.         Texto = "VCA Linea:";
  226.         glcd_text57(0, 10, Texto, 1, ON);
  227.         Texto = "Bateria:";
  228.         glcd_text57(0, 20, Texto, 1, ON);
  229.  
  230.         Values =        read_analog(6) * (260.0 / 1024.0);
  231.         sprintf(Texto,"%3.1fV",Values);
  232.         l = strlen(Texto);
  233.         glcd_rect(80, 17, 127, 10, YES, OFF);
  234.         if (l == 4){glcd_text57(92, 10, Texto, 1, ON);}
  235.         if (l == 5){glcd_text57(86, 10, Texto, 1, ON);}
  236.         if (l == 6){glcd_text57(80, 10, Texto, 1, ON);}
  237.        
  238.         Values =        read_analog(7) * (5.0 * 100.0 / 1024.0);
  239.         sprintf(Texto,"%3.1fV",Values);
  240.         l = strlen(Texto);
  241.         glcd_rect(80, 27, 127, 20, YES, OFF);
  242.         if (l == 4){glcd_text57(92, 20, Texto, 1, ON);}
  243.         if (l == 5){glcd_text57(86, 20, Texto, 1, ON);}
  244.         if (l == 6){glcd_text57(80, 20, Texto, 1, ON);}
  245.        
  246.         p = 2;
  247. }
  248. //_______________________________________________________________________________________
  249. void alertas(int tip){
  250.         glcd_fillScreen(OFF);
  251.  
  252.         if (tip == 1){
  253.         Texto = "TEMPERATURAS ELEVADAS";
  254.         buzzer();
  255.         glcd_text57(0,0, Texto, 1, ON);
  256.         }
  257.         if (tip == 2){
  258.         Texto = "ENCENDIENDO GRUPO...";
  259.         buzzer();
  260.         glcd_text57(0,0, Texto, 1, ON);
  261.         }
  262.         if (tip == 3){
  263.         Texto = "APAGANDO GRUPO...";
  264.         buzzer();
  265.         glcd_text57(0,0, Texto, 1, ON);
  266.         }
  267.         if (tip == 4){
  268.         Texto = "LISTO PARA ENCENDER GRUPO";
  269.         buzzer();
  270.         glcd_text57(0,0, Texto, 1, ON);
  271.         }
  272.         if (tip == 5){
  273.         Texto = "MONOXIDO EN NIVELES CRITICOS";
  274.         buzzer();
  275.         glcd_text57(0,0, Texto, 1, ON);
  276.         }
  277.         delay_ms(1500); // ver que el delay no sea muy alto o apagar grupo antes, no se
  278.         glcd_fillScreen(OFF);
  279.         m = menu(0);
  280. }
  281. //_______________________________________________________________________________________
  282.  
  283. void buzzer(){
  284.         int tim_;
  285.         output_high(PIN_C1);
  286.         delay_ms(2000);
  287.         output_low(PIN_C1);
  288.         while (tim_ <= 30){
  289.                 output_high(PIN_C1);
  290.                 delay_ms(23);
  291.                 output_low(PIN_C1);
  292.                 delay_ms(66);
  293.                 tim_++;
  294.         }
  295.         tim_ = 0;
  296.  
  297. }
  298.  
  299. //_______________________________________________________________________________________
  300. void Pantalla_3(int o){
  301.  
  302.         unsigned int vref = 0;
  303.        
  304.         if (p == 1 || p == 0 || p == 2) {
  305.         glcd_fillScreen(OFF);
  306.         m = menu(3);
  307.         }
  308.         if (o == 0){o = lo;}
  309.         if (o == 1 || o == 2){lo = o;}
  310.         //si o es 1 entonces marcar motor, si o es 2 marcar alternador 
  311.         Texto = "----CONFIGURACION----";
  312.         glcd_text57(0, 0, Texto, 1, ON);
  313.         Texto = "Temp.R. Motor:";
  314.         glcd_text57(0, 10, Texto, 1, ON);
  315.         Texto = "Temp.R. Alt.:";
  316.         glcd_text57(0, 20, Texto, 1, ON);
  317.  
  318.         vref = read_eeprom(7); //probar un cast de unsigned int y elinar variable vref
  319.         if (o == 1){sprintf(Texto,"%3u<",vref);} //motor
  320.         else {sprintf(Texto,"%3u.",vref);}
  321.         l = strlen(Texto);
  322.         glcd_rect(80, 17, 127, 10, YES, OFF);
  323.         if (l == 2){glcd_text57(92, 10, Texto, 1, ON);} //92
  324.         if (l == 3){glcd_text57(98, 10, Texto, 1, ON);} //86
  325.         if (l == 4){glcd_text57(103, 10, Texto, 1, ON);} //80
  326.        
  327.         vref = read_eeprom(8); //probar un cast de unsigned int y elimnar variable vref
  328.         if (o == 2){sprintf(Texto,"%3u<",vref);} //alternador
  329.         else {sprintf(Texto,"%3u.",vref);}
  330.         l = strlen(Texto);
  331.         glcd_rect(80, 27, 127, 20, YES, OFF);
  332.         if (l == 2){glcd_text57(92, 20, Texto, 1, ON);}
  333.         if (l == 3){glcd_text57(98, 20, Texto, 1, ON);}
  334.         if (l == 4){glcd_text57(103, 20, Texto, 1, ON);}
  335.        
  336.         p = 3;
  337.         //ultimo estado seleccionado:
  338.        
  339. }
  340. //_______________________________________________________________________________________
  341. int check_temp(int sen){ //sen1 motor, sen2 alternador
  342.         int temp1, temp2, value;
  343.         delay_ms(20);
  344.         temp1 =         read_analog(2) * (5.0 * 100.0 / 1024.0); //temperatura motor
  345.         delay_ms(20);
  346.         temp2 =         read_analog(3) * (5.0 * 100.0 / 1024.0); //temperatura alternador
  347.  
  348.         if (sen == 1){
  349.                 value = read_eeprom(7);
  350.                 if (temp1 >= value){
  351.                         //apagar grupo
  352.                         return 1;
  353.                 }
  354.                 else {
  355.                         return 0;
  356.                 }
  357.         }
  358.         if (sen == 2){
  359.                 value = read_eeprom(8);
  360.                 if (temp2 >= value){
  361.                         //apagar grupo
  362.                         return 1;
  363.                 }
  364.                 else {
  365.                         return 0;
  366.                 }
  367.         }
  368. }
  369.  
  370. //_______________________________________________________________________________________
  371. void grupo(int act_){ //si act es 1 envia 1 al rele del cebador, 0 contrario.
  372.  
  373.         if ((act_ == 1) && (h == 1) && (min_g >= (time_g + 7))){
  374.                 h = 0;
  375.                 alertas(2);
  376.                 output_high(PIN_C7);
  377.                 delay_ms(5000);
  378.                 output_low(PIN_C7);
  379.                 time_g = min_g;
  380.         }
  381.                 //pantalla encendiendo grupo, menu empty meterlo al principio para que luego lea main y vuelva a la normalidad
  382.        
  383.         if (act_ == 0){
  384.                 alertas(3);
  385.                 output_low(PIN_C5);
  386.                 //stop grupo
  387.         }
  388.         if (act_ == 2){
  389.                 Values =        read_analog(4) * (260.0 / 1024.0);
  390.                 if (Values > 100) {
  391.                         start_hours = 1;
  392.                         eg = 1;
  393.                 }
  394.                 else {
  395.                         start_hours = 0;
  396.                         eg = 0;
  397.                 }
  398.         }
  399.  
  400. }
  401. //_______________________________________________________________________________________
  402. void set_vref(int up_down){ //up_down = 1 incrementa, 2 decrementa
  403.         int value;
  404.         if (up_down == 0){up_down = 0;}
  405.         if (up_down == 1){
  406.                 if (lo == 1){
  407.                         delay_ms(500);
  408.                         value = read_eeprom(7); //motor
  409.                         ++value; //meter value como variable interna
  410.                         write_eeprom(7,value);
  411.                 }
  412.                 if (lo == 2){
  413.                         delay_ms(500);
  414.                         value = read_eeprom(8); //alternador
  415.                         ++value;
  416.                         write_eeprom(8,value);
  417.                 }
  418.         }
  419.         if (up_down == 2){
  420.                 if (lo == 1){
  421.                         delay_ms(500);
  422.                         value = read_eeprom(7); //motor
  423.                         --value;
  424.                         write_eeprom(7,value);
  425.                 }
  426.                 if (lo == 2){
  427.                         delay_ms(500);
  428.                         value = read_eeprom(8); //alternador
  429.                         --value;
  430.                         write_eeprom(8,value);
  431.                 }
  432.         }
  433. }
  434. //_______________________________________________________________________________________
  435. int menu(int p){
  436. //extern char texto[];
  437. if (p == 0){
  438.         Texto = "TMP | GRP | MON | -- ";
  439.         glcd_rect(0, 54, 127, 63, YES, ON);
  440.         glcd_text57(1, 55, Texto, 1, OFF);
  441.         Texto = "TMP";
  442.         glcd_rect(1, 55, 26, 62, YES, OFF);
  443.         glcd_text57(1, 55, Texto, 1, ON);
  444.         return(0);
  445.         }
  446. if (p == 1){
  447.         Texto = "TMP | GRP | MON | -- ";
  448.         glcd_rect(0, 54, 127, 63, YES, ON);
  449.         glcd_text57(1, 55, Texto, 1, OFF);
  450.         Texto = "      GRP ";
  451.         glcd_rect(28, 55, 62, 62, YES, OFF);
  452.         glcd_text57(1, 55, Texto, 1, ON);
  453.         return(1);
  454.         }
  455. if (p == 2){
  456.         Texto = "TMP | GRP | MON | -- ";
  457.         glcd_rect(0, 54, 127, 63, YES, ON);
  458.         glcd_text57(1, 55, Texto, 1, OFF);
  459.         Texto = "            MON";
  460.         glcd_rect(64, 55, 98, 62, YES, OFF);
  461.         glcd_text57(1, 55, Texto, 1, ON);
  462.         return(2);
  463.         }      
  464. if (p == 3){
  465.         Texto = "UP  | DN  | SET | EX ";
  466.         glcd_rect(0, 54, 127, 63, YES, ON);
  467.         glcd_text57(1, 55, Texto, 1, OFF);
  468.         return(3);
  469.         }      
  470. if (p == 4){
  471.         Texto = "UP  | DN  | SET | EX ";
  472.         glcd_rect(0, 54, 127, 63, YES, ON);
  473.         glcd_text57(1, 55, Texto, 1, OFF);
  474.         Texto = "            SET";
  475.         glcd_rect(64, 55, 98, 62, YES, OFF);
  476.         glcd_text57(1, 55, Texto, 1, ON);
  477.         return(3);
  478.         }      
  479. }
  480. //_______________________________________________________________________________________
  481. float read_analog(int a) ///ingresar switch
  482. {
  483.         float ADC_Value;
  484.  
  485. if (a == 0){
  486.         output_low(PIN_E2);
  487.         output_low(PIN_E1);
  488.         output_low(PIN_A2);
  489.         }
  490. if (a == 1){
  491.         output_high(PIN_E2);
  492.         output_low(PIN_E1);
  493.         output_low(PIN_A2);
  494.  
  495.         }
  496. if (a == 2){
  497.         output_low(PIN_E2);
  498.         output_high(PIN_E1);
  499.         output_low(PIN_A2);
  500.        
  501.         }
  502. if (a == 3){
  503.         output_high(PIN_E2);
  504.         output_high(PIN_E1);
  505.         output_low(PIN_A2);
  506.      
  507.         }
  508. if (a == 4){
  509.         output_low(PIN_E2);
  510.         output_low(PIN_E1);
  511.         output_high(PIN_A2);
  512.      
  513.         }
  514. if (a == 5){
  515.         output_high(PIN_E2);
  516.         output_low(PIN_E1);
  517.         output_high(PIN_A2);
  518.      
  519.         }
  520. if (a == 6){
  521.         output_low(PIN_E2);
  522.         output_high(PIN_E1);
  523.         output_high(PIN_A2);
  524.      
  525.         }
  526. if (a == 7){
  527.         output_high(PIN_E2);
  528.         output_high(PIN_E1);
  529.         output_high(PIN_A2);
  530.         }
  531. if (a == 8){   
  532.         delay_ms(100);
  533.         set_adc_channel (3); //Elige canal a medir RA3
  534.         delay_us (20);
  535.         ADC_Value=read_adc(); //Capturamos señal analógica que entra por el canal previamente seleccionado (RA3)
  536.         }
  537. else { 
  538.         delay_ms(100);
  539.         set_adc_channel (0); //Elige canal a medir RA0
  540.         delay_us (20);
  541.         ADC_Value=read_adc(); //Capturamos señal analógica que entra por el canal previamente seleccionado (RA0)
  542.         }      
  543.              
  544. return(ADC_Value);
  545. }
  546. //_______________________________________________________________________________________
  547. void iniciar_eeprom(int e){ // funcion para limpiar la eeprom si es la primera vez que se graba el pic  
  548. #rom int 0xf00000={1,2,3,4} //eeprom graba en la posicion 0123 los valores 1234
  549.         if (read_eeprom(4) != 1 || e == 1){
  550.                 write_eeprom(4,1);
  551.                 write_eeprom(5,0);
  552.                 write_eeprom(6,0);
  553.                 write_eeprom(7,135);
  554.                 write_eeprom(8,100);
  555.         }
  556. }
  557.  
  558. //_______________________________________________________________________________________
  559. void write_eeprom16(int address1, int address2, int16 value) //escribe sobre la eeprom ints de 16 bits
  560. {
  561.    int8 lowerhalf = 0;
  562.    int8 upperhalf = 0;
  563.  
  564.    lowerhalf = make8(value,0);
  565.    write_eeprom(address1,lowerhalf);
  566.    upperhalf = make8(value,1);
  567.    write_eeprom(address2,upperhalf);
  568. }
  569.  
  570. //_______________________________________________________________________________________
  571. int16 read_eeprom16(int address1, int address2) //lee sobre la eeprom ints de 16 bits
  572. {
  573.    int16 finalvalue = 0;
  574.    int8 lowerhalf = 0;
  575.    int8 upperhalf = 0;
  576.  
  577.    upperhalf = read_eeprom(address2);
  578.    lowerhalf = read_eeprom(address1);
  579.  
  580.    finalvalue = make16(upperhalf,lowerhalf);
  581.  
  582.    return finalvalue;
  583. }
  584.  
  585. //_______________________________________________________________________________________
  586. //funcion para medir frecuencia:
  587. int16 read_frequency(){
  588.  
  589. int16 current_ccp_delta;
  590. int16 frequency;
  591.  
  592.         disable_interrupts(GLOBAL);
  593.         current_ccp_delta = isr_ccp_delta;
  594.         enable_interrupts(GLOBAL);
  595.    
  596.    frequency = (int16)((5000000L/8) / current_ccp_delta);
  597.  
  598. return (frequency);
  599.    //printf("%lu Hz\n\r", frequency);
  600.  
  601.   delay_ms(500);
  602. }
  603. //_______________________________________________________________________________________
  604. /*
  605. #int_timer1
  606. void timer1_isr(void)
  607. {
  608. gc_timer1_extension++;
  609. }
  610. */
  611. //------------------------------------------------------
  612. ////codigo para contar segundos o segun se defina en clocks_per_second obtenido de ccs
  613. //_______________________________________________________________________________________
  614. #int_timer2
  615. void updateClock()
  616. {
  617.  #if CLOCKS_PER_SECOND >= 0x10000
  618.    static int32 t;
  619.  #elif CLOCKS_PER_SECOND >= 0x100
  620.    static int16 t;
  621.  #else
  622.    static int8 t;
  623.  #endif
  624. /*
  625.  #if CLOCKS_PER_SECOND_2 >= 0x10000
  626.    static int32 t_2;
  627.  #elif CLOCKS_PER_SECOND_2 >= 0x100
  628.    static int16 t_2;
  629.  #else
  630.    static int8 t_2;
  631.  #endif
  632. */
  633.    t++;
  634. //      t_2++;
  635. /*     
  636.    if(t_2 >= CLOCKS_PER_SECOND_2){
  637.                 t = 0;
  638.                 min_g++;
  639.                 if (min_g >= 65530){min_g = 0;}
  640.                 //incremento segundero para start
  641.         }
  642. */
  643.        
  644.    if(t >= CLOCKS_PER_SECOND)
  645.    {
  646.       t = 0;
  647.                 if (start_hours){minutos5++;}
  648.         }
  649.  
  650. }
  651. //////////// fin de codigo ccs
  652. //________________________________________________________
  653. #int_ccp1
  654. void ccp1_isr(void)
  655. {
  656. int16 current_ccp;
  657. static int16 old_ccp = 0;
  658.  
  659. current_ccp = CCP_1;    
  660.  
  661. isr_ccp_delta = current_ccp - old_ccp;
  662.  
  663. old_ccp = current_ccp;
  664. }
  665. //_______________________________________________________________________________________
  666. #int_rb
  667. void test_detect_rb_change() {
  668.    set_tris_b(0xF0);
  669.         if (input(PIN_B4)) {dbutton4=1;}
  670.         if (input(PIN_B5)) {dbutton5=1;}
  671.         if (input(PIN_B6)) {dbutton6=1;}
  672.         if (input(PIN_B7)) {dbutton7=1;}
  673.         if (!input(PIN_B7))     {dbutton7=0;} //hay que mantener apretado dbutton 7 + 4 para entrar al menu 3
  674.   }
  675.  
  676. void clear_delta() {
  677.    dbutton4=0;
  678.    dbutton5=0;
  679.    dbutton6=0;
  680.    dbutton7=0;
  681. }
  682. //_______________________________________________________________________________________
  683. void main(void)
  684. {
  685.         clear_delta();
  686.         iniciar_eeprom(0);
  687.         setup_timer_2(T2_DIV_BY_4,250,5);
  688.         enable_interrupts(INT_TIMER2);
  689.         enable_interrupts(INT_RB);
  690.         //enable_interrupts(GLOBAL);                                   
  691.  
  692.         glcd_init(ON);   // iniciamos la lcd
  693.         glcd_fillScreen(OFF); //LIMPIAMOS LA PANTALLA
  694.         delay_ms(200);
  695.         welcome();
  696.         delay_ms(2000);
  697.         glcd_fillScreen(OFF); //LIMPIAMOS LA PANTALLA
  698.  
  699.         setup_adc_ports(AN0_AN1_AN3);
  700.         setup_adc(ADC_CLOCK_INTERNAL); 
  701. //      setup_spi(SPI_SS_DISABLED);
  702.         setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT);
  703.         // setup_timer_1(T1_DISABLED);
  704.         //setup_timer_2(T2_DISABLED,0,1);
  705.         // setup_ccp1(CCP_OFF);
  706.         // setup_comparator(NC_NC_NC_NC); //removido para 18f452
  707.         // setup_vref(FALSE);   //removido para 18f452
  708.         ////////////codigo para medir frecuencia, depurar de ccs forums
  709.         set_timer1(0);            
  710.         setup_timer_1(T1_INTERNAL| T1_DIV_BY_8);    
  711.         setup_ccp1(CCP_CAPTURE_RE);    
  712.         // Enable interrupts.
  713.         //clear_interrupt(INT_TIMER1);
  714.         //enable_interrupts(INT_TIMER1);
  715.         clear_interrupt(INT_CCP1);
  716.         enable_interrupts(INT_CCP1);
  717.         enable_interrupts(GLOBAL);
  718.         /////////////fin para medir frecuencia de ccs  
  719.         minutos5 = read_eeprom16(5,6); //asigna al intervalo de 5 minutos el ultimo valor que se guardo en la eeprom
  720.         set_tris_a(0xEF);
  721.         set_tris_c(0x01);
  722.         set_tris_e(0x00);
  723.         m = menu(0);
  724.         eg = 0;
  725.         c_state = 0;
  726.         output_low(PIN_C1);
  727.         while (TRUE){
  728.         if(dbutton4) {
  729.                 if (!dbutton7){
  730.                         if (m != 3){
  731.                                 m = menu(0); dbutton4=0;
  732.                         }
  733.                 }
  734.         }
  735.         if(dbutton5) {
  736.                 if (m != 3){
  737.                         m = menu(1); dbutton5=0;
  738.                 }
  739.         }
  740.         if(dbutton6) {
  741.                 if (m != 3){
  742.                         m = menu(2); dbutton6=0;
  743.                 }
  744.         }
  745.         if(dbutton7) {
  746.                 if (m == 3){m = menu(0);}
  747.                 if (dbutton4){
  748.                         m = menu(3);
  749.                         dbutton4=0;
  750.                 }                              
  751.         }
  752.                                
  753.         delay_ms(500);
  754.         if (m == 0){
  755.                 Pantalla_0();
  756.         }
  757.         if (m == 1){
  758.                 Pantalla_1();
  759.         }
  760.         if (m == 2){
  761.                 Pantalla_2();
  762.         }
  763.         if (m == 3){
  764.                 if (dbutton4){ //UP
  765.                         if (set_ == 1){
  766.                                 set_vref(1);
  767.                                 dbutton4=0;
  768.                         }
  769.                         else{
  770.                                 Pantalla_3(1);
  771.                                 dbutton4=0;
  772.                         }
  773.                 }
  774.                 if (dbutton5){ //DOWN
  775.                         if (set_ == 1){
  776.                                 set_vref(2);
  777.                                 dbutton5=0;
  778.                         }
  779.                         else{
  780.                                 Pantalla_3(2);
  781.                                 dbutton5=0;
  782.                         }
  783.                 }
  784.                 if (dbutton6){ //SET  
  785.                         set_ = 1;
  786.                         if (set_ == 1){
  787.                                 set_ = 0;
  788.                         }
  789.                         dbutton6=0;
  790.                 }      
  791.                 if (dbutton7){ //EXIT
  792.                         m = menu(0);
  793.                         Pantalla_0();
  794.                         dbutton7=0;
  795.                 }      
  796.                 Pantalla_3(0);
  797.         }
  798.  
  799.        
  800.         if ((minutos5 - t ) == 5){ /// este if debe estar dentro del rango de refresco de el while superior. debido a que tiene que coincidir con el minuto x (ver diferencia minutos5-m) [graba cada 5 minutos]
  801.                 write_eeprom16(5,6,minutos5);
  802.                 delay_ms(1000);
  803.                 if (read_eeprom16(5,6) != minutos5){
  804.                         iniciar_eeprom(1);
  805.                         write_eeprom16(5,6,minutos5);
  806.                 }
  807.                 t = minutos5;
  808.         }
  809.        
  810.         grupo(2);
  811.         if (check_temp(1)) { //motor
  812.                 alertas(1);
  813.                 if (eg == 1) {grupo(0);}
  814.         } //motor--
  815.         if (check_temp(2)) { //alternador
  816.                 alertas(1);
  817.                 if (eg == 1){grupo(0);}
  818.         } //alternador--
  819.         c_state = read_analog(8);
  820.         if (c_state < 250 ){ //llave en 0
  821.                 grupo(0);
  822.         }
  823.         if ((c_state > 250) && (c_state < 850)){ //llave en contacto
  824.         output_high(PIN_C5);
  825.         if (!eg){
  826.                 alertas(4); //listo para encender grupo
  827.         }
  828.         h = 1; //vuelve el estado h 1 para que se pueda intentar encender el grupo nuevamente.
  829.         }
  830.         if (c_state > 850)  { //llave en uno
  831.                 grupo(1);
  832.         }
  833.  
  834.         }      
  835. }
Si a alguien se le ocurre que puede ser estoy a la escucha.
gracias, saludos

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Re: Una funcion muy simple para calcular frecuencia no anda en el programa completo
« Respuesta #1 en: 20 de Diciembre de 2010, 12:48:43 »
 :rolleyes: