Autor Tema: El TIMER me ignora ...  (Leído 3409 veces)

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

Desconectado blackcat

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 600
El TIMER me ignora ...
« en: 13 de Diciembre de 2008, 04:44:35 »
Hola ... tengo el siguiente problema:

Quiero hacer un disparador de media onda para un tiristor, para generar los pulsos utilizo el ADC, modulo OUTPUT COMPARE (OC1) y el TIMER 2. La idea es simple, uso un transformador y un comparador para generar una onda cuadrada (50% duty cicle) de 5V. El semiciclo positivo de esa onda me sirve para sincronizar el timer, es decir, el timer empieza su cuenta cuando se aplica 5V, cuando la señal de sincronización cae a 0V, se activa la interrupción por timer y la ISR me restablece la interrupción, copio el contenido del timer y luego lo borro, en resumen tengo algo como asi.

Código: C
  1. void __attribute__((__interrupt__, no_auto_psv)) _T2Interrupt(void)
  2. {
  3.  
  4.         IFS0bits.T2IF = 0;      // clear interrupt flag
  5.  
  6.         t_max = TMR2;
  7.  
  8.         TMR2 = 0;
  9.  
  10. }


Ahora, que significado tiene copiar TMR2 ... muy facil ... TMR2 en ese instante representa el valor justo de un semiperiodo de 60Hz; usando el ADC puedo calcular el angulo de disparo adecuadamente para que se ajuste entre el semiciclo positivo de la señal de potencia ... en si ... es una forma vaga de ajustar el angulo de disparo mínimo. Con ese dato coloco de manera precisa el valor en el OC y obtengo un rango de ajuste de 0 a 180° de disparo.

El programa me funcionó perfectamente, sin embargo, luego de un rato de estar funcionando  el angulo de disparo solo se ajusta a 0° ... despues de verificar el programa, noté con el DEBUGER que el valor de t_max siempre es cero y teniendo el TMR2 algún valor, es decir, trato de leer el TMR2 y este me devuelve 0x0000.

Esto ya me ha pasado varias veces con otras aplicaciones y otros micros y no se que hacer ....  este es el programita
Código: C
  1. #include "p33FJ12MC202.h"
  2.  
  3. //#define XTFREQ          7370000         //FRC frequency
  4. #define FCY             39613000        //Instruction Cycle Frequency
  5.  
  6. #define BAUDRATE         9600                
  7. #define BRGVAL          ((FCY/BAUDRATE)/16)-1
  8. #define MILLISEC                8200
  9. #define T_PULSE                 2000
  10.  
  11. void DelayNmSec(unsigned int);
  12. void PLL_Init (void);
  13.  
  14. unsigned int adc_value;
  15. unsigned int t_max;
  16. float ft_max = 38000.0;
  17. float ft_pulse;
  18.  
  19. int main(void)
  20. {
  21.  
  22.  
  23.         PLL_Init( );
  24.  
  25.         AD1PCFGL = 0xFFFE;                      // all PORTB = Digital; RB12 = analog
  26.  
  27.         LATB = 0x0000;
  28.         LATA = 0x0000;
  29.         TRISAbits.TRISA0 = 1;   // RP3  input (analog)
  30.         TRISBbits.TRISB6 = 1;   // RP6  input
  31.         TRISBbits.TRISB15 = 0;  // RP15 output         
  32.  
  33.         RPOR7bits.RP15R = 18;   // OC2 en RP15
  34.         RPINR3bits.T2CKR = 6;   // TCK2 en RP2
  35.  
  36. // TIMER
  37.         TMR2 = 0;                                       // Clear timer 2
  38.         PR2 = 0xFF00;                           // Interrupt every ...
  39.         IFS0bits.T2IF = 0;                      // Clr interrupt flag
  40.         IEC0bits.T2IE = 1;                      // Set interrupt enable bit
  41.         T2CONbits.TCKPS = 1;            // 1:8 prescale, start TMR2
  42.         T2CONbits.TGATE = 1;            // Gated time accumulation enabled
  43.        
  44.  
  45. // Initialize Output Compare Module
  46.         OC1CONbits.OCM = 0;             // Disable Output Compare Module
  47.         OC1CONbits.OCTSEL = 0;          // Select Timer 2 as output compare time base
  48.         OC1R = 0xFFFF;                          // Load the Compare Register Value for rising edge
  49.         OC1RS = 0xFFFF;                                 // Load the Compare Register Value for falling edge
  50.         //IPC0bits.OC1IP = 0x01;                // Set Output Compare 1 Interrupt Priority Level
  51.         IFS0bits.OC1IF = 0;             // Clear Output Compare 1 Interrupt Flag
  52.         IEC0bits.OC1IE = 0;             // Enable Output Compare 1 interrupt
  53.         OC1CONbits.OCM = 5;             // Select the Output Compare mode
  54.  
  55. // ADC
  56.         AD1CON1 = 0x00E0;       // SSRC bit = 111 implies internal
  57.                                                 // counter ends sampling and starts
  58.                                                 // converting.
  59.         AD1CHS0= 0x0000;        // Connect RB12/AN12 as CH0 input ..
  60.                                                 // in this example RB12/AN12 is the input
  61.         AD1CSSL = 0;
  62.         AD1CON3 = 0x1F02;       // Sample time = 31Tad, Tad = internal 2 Tcy
  63.         AD1CON2 = 0;
  64.        
  65.         AD1CON1bits.ADON = 1;   // turn ADC ON
  66.  
  67.         T2CONbits.TON = 1;              // TMR2 start!
  68.  
  69. while(1)
  70. {
  71.  
  72.         DelayNmSec( 50 );
  73.  
  74.         AD1CON1bits.SAMP = 1;           // start sampling then ...
  75.         while (!AD1CON1bits.DONE);      // conversion done?
  76.         adc_value = ADC1BUF0;           // yes then get ADC value
  77.         AD1CON1bits.DONE = 0;           // Asegura reinicio
  78.  
  79.         ft_max = (float) t_max - T_PULSE;
  80.         ft_pulse = ((float)adc_value)*ft_max / 1023.0;
  81.  
  82.         OC1R = (unsigned int)ft_pulse;
  83.         OC1RS = OC1R + T_PULSE;
  84.  
  85. }
  86.  
  87. return 0;
  88. }
  89.  
  90. void PLL_Init (void)
  91. {
  92.  
  93. // Configure Oscillator to operate the device at 40Mhz
  94. // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
  95. // Fosc= 8M*40/(2*2)=80Mhz for 8M input clock
  96.         PLLFBD = 41;            // M=40
  97.         CLKDIVbits.PLLPOST=0;           // N1=2
  98.         CLKDIVbits.PLLPRE=0;            // N2=2
  99.         OSCTUN=0;                                       // Tune FRC oscillator, if FRC is used
  100.  
  101. // Disable Watch Dog Timer
  102.         RCONbits.SWDTEN=0;
  103.  
  104. // clock switching to incorporate PLL
  105.         __builtin_write_OSCCONH(0x03);          // Initiate Clock Switch to Primary
  106.                                                                                                         // Oscillator with PLL (NOSC=0b011)
  107.         __builtin_write_OSCCONL(0x01);          // Start clock switching
  108.         while (OSCCONbits.COSC != 0b011);       // Wait for Clock switch to occur
  109.  
  110. // Wait for PLL to lock
  111.         while( (OSCCON & 0x0020) == 0  ) {};
  112.        
  113. }
  114.  
  115. void DelayNmSec(unsigned int N)
  116. {
  117.  
  118.         unsigned int i;
  119.         while(N--)
  120.         {
  121.                 for(i=0; i < MILLISEC; i++);
  122.         }                              
  123.  
  124. }
  125.  
  126. void __attribute__((__interrupt__, no_auto_psv)) _T2Interrupt(void)
  127. {
  128.  
  129.         IFS0bits.T2IF = 0;      // clear interrupt flag
  130.        
  131.         t_max = TMR2;
  132.  
  133.         TMR2 = 0;
  134.  
  135. }

 

 
« Última modificación: 13 de Diciembre de 2008, 04:52:22 por blackcat »
Control Automático, DSP & Microcontroladores

Desconectado Nocturno

  • Administrador
  • DsPIC33
  • *******
  • Mensajes: 18286
    • MicroPIC
Re: El TIMER me ignora ...
« Respuesta #1 en: 13 de Diciembre de 2008, 04:50:12 »
Blackcat, si lees el TMR2 dentro de la interrupción del Timer2, ¿no es lógico que esté siempre a 0 puesto que acaba de desbordar y por eso ha saltado la interrupción?

Desconectado blackcat

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 600
Re: El TIMER me ignora ...
« Respuesta #2 en: 13 de Diciembre de 2008, 17:54:47 »
El timer no salta por desborde ... como está configurado el TGATE el timer debe saltar con el flanco de bajada de la señal de sincronización ...

Parte del detalle es que el programa funcionó, luego agregue unos detalles y no funcionó ... despues ... retorne al programa anterior (el que si funcionaba) y no funciono ... probe luego con otro micro igual y no sirve tampoco.

Saludos!

Control Automático, DSP & Microcontroladores

Desconectado Nocturno

  • Administrador
  • DsPIC33
  • *******
  • Mensajes: 18286
    • MicroPIC
Re: El TIMER me ignora ...
« Respuesta #3 en: 13 de Diciembre de 2008, 18:00:13 »
¿Será entonces problema de la señal que llega?, a ver si te van a llegar dos pulsos de sincronización seguidos.

Podrías probar quitando el TMR2=0 y mirar los valores que lee el timer por si te sirve de pista.

Desconectado Renatox_

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 541
    • máquinas cnc
Re: El TIMER me ignora ...
« Respuesta #4 en: 14 de Diciembre de 2008, 05:18:59 »
que tal blackat, en la señal de interrupción del timer debes actualizar el prescaler el TCKPS=1 debes volverlo a poner, es una recomendación que lei en el Family Reference Manual, y antes el timer no me funcionaba bien por ese motivo.

saludos.
control de movimiento

Desconectado blackcat

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 600
Re: El TIMER me ignora ...
« Respuesta #5 en: 15 de Diciembre de 2008, 23:24:46 »
Ya se cual fue el problema ... yo tomaba la señal de sincronizacion desde un transformador rectificado media onda y luego la pasaba por un detector de cruce por cero, parece que ese circuito volvia loco el timer, pero no asi la ejecucion del programa ... extraño pero cierto  :? ...

La solución fue eliminar el detector de cruce por cero y usar un optoacoplador ... se coloca el diodo en serie con una resistencia, cuando la señal de 60Hz está en el semiciclo positivo el diodo excita el transistor y se genera la señal rectangular.

Control Automático, DSP & Microcontroladores

Desconectado Nocturno

  • Administrador
  • DsPIC33
  • *******
  • Mensajes: 18286
    • MicroPIC
Re: El TIMER me ignora ...
« Respuesta #6 en: 16 de Diciembre de 2008, 03:14:17 »
A saber cómo estaba interpretando el timer esos pulsos. Me alegro que lo resolvieras.


 

anything