Autor Tema: Midiendo frecuencia de linea, EX_CCPMP.C  (Leído 1513 veces)

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

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Midiendo frecuencia de linea, EX_CCPMP.C
« en: 19 de Noviembre de 2010, 10:38:12 »
Hola, estaba viendo el ejemplo de EX_CCPMP.C   de CCS
Lo que necesito hacer es medir los 50 hertz de la linea de tension corriente.
Tengo un diodo para filtrar los semi ciclos y un zener para que este mas o menos una cuadrada.
Ahora solo necesito conectarlo y medirlo.
Este ejemplo usa dos patitas... no se puede hacer en una sola? calculo que si, alguien tiene data?
 :g)

Desconectado lopb

  • PIC12
  • **
  • Mensajes: 77
    • contactos lopb
Re: Midiendo frecuencia de linea, EX_CCPMP.C
« Respuesta #1 en: 19 de Noviembre de 2010, 16:48:20 »
hola,encontre un codigo que mide entre flancos ascendentes de ccp1
aca se los dejo:
Código: C
  1. #include <16F877A.h>
  2. #fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
  3. #use delay(clock = 20000000)
  4. #use rs232 (baud=9600,xmit=PIN_C6,rcv=PIN_C7)
  5.  
  6. #priority CCP1, TIMER1
  7.  
  8. #define BytePtr(var, offset) (char *)((char*)&var + offset)
  9.  
  10. #byte PIR1 = 0x0C
  11. #bit  TMR1IF = PIR1.0
  12.  
  13. int8  gc_timer1_extension = 0;
  14. int8  gc_capture_flag = FALSE;
  15. int32 g32_ccp_delta;
  16.  
  17. //------------------------------------------------------
  18. #int_timer1
  19. void timer1_isr(void)
  20. {
  21. gc_timer1_extension++;
  22. }
  23.  
  24. //------------------------------------------------------
  25.  
  26. #int_ccp1
  27. void ccp1_isr(void)
  28. {
  29. char timer_ext_copy;
  30. int32 current_ccp;
  31. static int32 old_ccp = 0;
  32.  
  33. gc_capture_flag = TRUE;        
  34.  
  35. current_ccp = (int32)CCP_1;    
  36.  
  37. // Get local copy of the timer ext.
  38. timer_ext_copy = gc_timer1_extension;
  39.  
  40.  
  41. if(TMR1IF)
  42.   {
  43.    if(*BytePtr(current_ccp, 1) < 2)  // Was CCP captured after Timer1 wrapped?
  44.       timer_ext_copy++;  // If so, inc the copy of the timer ext.
  45.  
  46.    // Since we know a timer interrupt is pending, let's just
  47.    // handle it here and now.  That saves a little load off
  48.    // the processor.
  49.    gc_timer1_extension++;  // Increment the real timer extension
  50.    TMR1IF = 0;     // Then clear the Timer1 interrupt
  51.   }
  52.  
  53. // Insert the timer extension into the proper place in the 32-bit
  54. // CCP value.
  55. // ie.,  Insert it into location "EE" as follows: 0x00EEnnnn
  56. // (nnnn = the CCP).
  57. *BytePtr(current_ccp, 2) = timer_ext_copy;
  58.  
  59. g32_ccp_delta = (current_ccp > old_ccp) ? current_ccp - old_ccp : current_ccp + (0x1000000 - old_ccp);
  60.  
  61. // Save the current ccp value for next time.
  62. old_ccp = current_ccp;
  63.  
  64. }
  65.  
  66. //=======================
  67. void main()
  68. {
  69. float frequency;
  70.  
  71. int32 current_ccp_delta;
  72.  
  73. set_timer1(0);            
  74. setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);    
  75.  
  76. setup_ccp1(CCP_CAPTURE_RE);    
  77.  
  78. // Enable interrupts.
  79. clear_interrupt(INT_TIMER1);
  80. enable_interrupts(INT_TIMER1);
  81.  
  82. clear_interrupt(INT_CCP1);
  83. enable_interrupts(INT_CCP1);
  84.  
  85. enable_interrupts(GLOBAL);
  86.  
  87.  
  88. while(1) {
  89.  
  90.   if(gc_capture_flag == TRUE)
  91.      {
  92.       //disable_interrupts(GLOBAL);
  93.       current_ccp_delta = g32_ccp_delta;;
  94.       enable_interrupts(GLOBAL);
  95.  
  96.       frequency =  (5000000L / (float)current_ccp_delta);
  97.       printf("%4.2f\n\r", frequency);
  98.  
  99.       gc_capture_flag = FALSE;
  100.      }
  101.   else
  102.     {
  103.      printf("No signal\n\r");
  104.     }
  105.  
  106.   delay_ms(500);
  107.  }
  108.  
  109. }