Autor Tema: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta  (Leído 3409 veces)

0 Usuarios y 2 Visitantes están viendo este tema.

Desconectado lopb

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

Desconectado bmfranky

  • PIC16
  • ***
  • Mensajes: 165
    • La Tienda De Fran
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #1 en: 09 de Diciembre de 2010, 14:18:52 »
Hola, lopb, ¿porque defines 2 veces la misma librería?

Citar
#
#include <stdlib.h>/// declarada 1
#
// fin de conteo uptime
#
///para la eeprom interna del pic
#
#include "stdlib.h"/// declarada 2???
Visiten La Tienda De Fran ;-) Aqui. y mi nueva Web Aqui.

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #2 en: 09 de Diciembre de 2010, 14:54:02 »
Hola, lopb, ¿porque defines 2 veces la misma librería?

Citar
#
#include <stdlib.h>/// declarada 1
#
// fin de conteo uptime
#
///para la eeprom interna del pic
#
#include "stdlib.h"/// declarada 2???
ahi lo corregi, gracias

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #3 en: 09 de Diciembre de 2010, 16:24:42 »
puede ser el prescaler?
como verifico si es eso o no?

Desconectado bmfranky

  • PIC16
  • ***
  • Mensajes: 165
    • La Tienda De Fran
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #4 en: 09 de Diciembre de 2010, 18:57:42 »
¿Tienes una simulación, para probarla?
Visiten La Tienda De Fran ;-) Aqui. y mi nueva Web Aqui.

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #5 en: 10 de Diciembre de 2010, 11:45:04 »
¿Tienes una simulación, para probarla?
No, no tengo simulacion, que puedo usar proteus?
La frecuencia me esta midiendo 30hz cuando deberia ser 50 y el segundero va por minutero mas o menos. menos que mas.  :D

Desconectado bmfranky

  • PIC16
  • ***
  • Mensajes: 165
    • La Tienda De Fran
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #6 en: 10 de Diciembre de 2010, 22:12:03 »
Que cristal estas usando físicamente para hallar el reloj de trabajo.
Visiten La Tienda De Fran ;-) Aqui. y mi nueva Web Aqui.

Desconectado LABmouse

  • Moderadores
  • DsPIC30
  • *****
  • Mensajes: 3575
    • Juntos es mejor
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #7 en: 11 de Diciembre de 2010, 13:53:35 »
Pues..Se me ocurre que tal vez sea la versión del compilador...Con CCS nada se sabe, ya me ha pasado que la version tiene un BUG entonces horas y horas perdidas pensando que el problema era mi código.  :5]

Prueba con 3.249 o 4.068 que para mi son las mas estables hasta la fecha.

SALUDOS!

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #8 en: 12 de Diciembre de 2010, 17:57:41 »
Pues..Se me ocurre que tal vez sea la versión del compilador...Con CCS nada se sabe, ya me ha pasado que la version tiene un BUG entonces horas y horas perdidas pensando que el problema era mi código.  :5]

Prueba con 3.249 o 4.068 que para mi son las mas estables hasta la fecha.

SALUDOS!
tengo 4.104
jajaja, estoy perdidoooo :D:D

Desconectado migsantiago

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8257
    • Sitio de MigSantiago
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #9 en: 12 de Diciembre de 2010, 18:43:40 »
Pues..Se me ocurre que tal vez sea la versión del compilador...Con CCS nada se sabe, ya me ha pasado que la version tiene un BUG entonces horas y horas perdidas pensando que el problema era mi código.  :5]

Prueba con 3.249 o 4.068 que para mi son las mas estables hasta la fecha.

SALUDOS!

Puede ser el compilador, pero también puede ser que Lopb no esté modificando el código correctamente. Migrar de un PIC16 a un PIC18 tiene sus implicaciones y no solo basta con cambiar el #include del código fuente o con copiar y pegar código.

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #10 en: 13 de Diciembre de 2010, 11:26:21 »
Pues..Se me ocurre que tal vez sea la versión del compilador...Con CCS nada se sabe, ya me ha pasado que la version tiene un BUG entonces horas y horas perdidas pensando que el problema era mi código.  :5]

Prueba con 3.249 o 4.068 que para mi son las mas estables hasta la fecha.

SALUDOS!

Puede ser el compilador, pero también puede ser que Lopb no esté modificando el código correctamente. Migrar de un PIC16 a un PIC18 tiene sus implicaciones y no solo basta con cambiar el #include del código fuente o con copiar y pegar código.
eso puede ser, pero no se nada de el 18f use este programa en el 16f y andaba

#include <16F628.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#use rs232 (baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)

#priority CCP1, TIMER1

#define BytePtr(var, offset) (char *)((char*)&var + offset)

#byte PIR1 = 0x0C
#bit  TMR1IF = PIR1.0

int8  gc_timer1_extension = 0;
int8  gc_capture_flag = FALSE;
int32 g32_ccp_delta;

//------------------------------------------------------
#int_timer1
void timer1_isr(void)
{
gc_timer1_extension++;
}

//------------------------------------------------------

#int_ccp1
void ccp1_isr(void)
{
char timer_ext_copy;
int32 current_ccp;
static int32 old_ccp = 0;

gc_capture_flag = TRUE;       

current_ccp = (int32)CCP_1;   

// Get local copy of the timer ext.
timer_ext_copy = gc_timer1_extension;


if(TMR1IF)
  {
   if(*BytePtr(current_ccp, 1) < 2)  // Was CCP captured after Timer1 wrapped?
      timer_ext_copy++;  // If so, inc the copy of the timer ext.

   // Since we know a timer interrupt is pending, let's just
   // handle it here and now.  That saves a little load off
   // the processor.
   gc_timer1_extension++;  // Increment the real timer extension
   TMR1IF = 0;     // Then clear the Timer1 interrupt
  }

// Insert the timer extension into the proper place in the 32-bit
// CCP value.
// ie.,  Insert it into location "EE" as follows: 0x00EEnnnn
// (nnnn = the CCP).
*BytePtr(current_ccp, 2) = timer_ext_copy;

g32_ccp_delta = (current_ccp > old_ccp) ? current_ccp - old_ccp : current_ccp + (0x1000000 - old_ccp);

// Save the current ccp value for next time.
old_ccp = current_ccp;

}

//=======================
void main()
{
float frequency;

int32 current_ccp_delta;

set_timer1(0);           
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);   

setup_ccp1(CCP_CAPTURE_RE);   

// Enable interrupts.
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);

clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);

enable_interrupts(GLOBAL);


while(1)
 {
  if(gc_capture_flag == TRUE)
     {
      disable_interrupts(GLOBAL);
      current_ccp_delta = g32_ccp_delta;;
      enable_interrupts(GLOBAL);

      frequency =  (5000000L / (float)current_ccp_delta);
      printf("%4.2f\n\r", frequency);

      gc_capture_flag = FALSE;
     }
  else
    {
     printf("No signal\n\r");
    }

  delay_ms(500);
 }

}

Desconectado migsantiago

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8257
    • Sitio de MigSantiago
Re: Migre de un 16f877 a un 18f452 y algunas funciones dejaron de funcionar correcta
« Respuesta #11 en: 13 de Diciembre de 2010, 11:35:30 »
Entonces lo recomendable es que leas la hoja de características del PIC18.


 

anything