NECESITA AYUDA: Error del oscilador y PIC24F dejó de funcionar

1

Tengo un oscilador de cristal de 30MHz para sincronizar un controlador PIC24FJ128GC010. Estoy programando en MPLAB X. Mi idea es generar un pulso en uno de los conmutadores de pin de E / S. Pero lamentablemente, no puedo hacerlo. Inicialmente estaba usando un cristal de 8MHz. He realizado los ajustes de configuración de Word, así como los ajustes del registro de control. También he cambiado la velocidad de transmisión.

Por favor, sugiera las posibilidades en este caso.

*****************************************************************************************
CONFIG WORDS
*****************************************************************************************

POSCMD = HS              // Primary Oscillator Select (HS Oscillator Enabled)
OSCIOFCN = OFF           // OSCO Pin Configuration (OSCO/CLKO/RC15 functions as CLKO (FOSC/2))//
FCKSM = CSDCMD           // Clock Switching and Fail-Safe Clock Monitor Configuration bits (Disabled)
FNOSC = PRI              // Initial Oscillator Select (Primary Oscillator (XT, HS, EC))
IESO = OFF               // Internal External Switchover (Disabled)

*****************************************************************************************
#include "timer.h"

unsigned millisCount = 0;

void __attribute__ ((interrupt, no_auto_psv)) _T2Interrupt(void)
{
  IFS0bits.T2IF = 0;
  millisCount++;
}

/*******************************************************************************
 * Returns the number of milliseconds since the device was started as an
 * unsigned long.
 ******************************************************************************/
unsigned long Millis()
{
  return millisCount;
}

void ResetMillis(void)
{
  millisCount = 0u;
}

void Delay(unsigned long delayTime)
{
  volatile int delayStart = Millis();
  while (Millis() - delayStart < delayTime);
}

****************************************************************************************    
INITIALISATION CODE
****************************************************************************************
void Osc_Config(void)
{
    OSCTUN = 0;
    CLKDIV = 0;
    OSCCON = 0;

    OSCCONbits.COSC = 2;
    OSCCONbits.NOSC = 2;
    OSCCONbits.OSWEN = 1;

    while(OSCCONbits.OSWEN);
}

void Timer2_Config(void)
{
    T2CON = 0;
    TMR2 = 0x0000;                      //Turn off Timer2
    PR2 = 0xFFFF;                       //Period register at maximum

    _T2IF = 0;                          //clear Timer2 interupt flag
    _T2IE = 1;                          //Enable Time2 interupt

    //T2CON
    T2CONbits.T32 = 0;                  //Timer2 and T3 or T4 and T5 act as two 16-bit timers
    T2CONbits.TCKPS = 0;                //Timer Prescale 1:1
    T2CONbits.TGATE = 0;                //Gated time accumulation is disabled
    T2CONbits.TSIDL = 0;                //Continues module operation in Idle mode
    T2CONbits.TCS = 0;                  //Internal clock (FOSC/2)
    T2CONbits.TON = 1;                  //Starts Timer2
}

void Io_Config(void)
{
    ****//Start pulse pin**
    PULSE_PIN = 0;
    TRISBbits.TRISB2 = 0;   //set as output pin**

    //Stop pulse pin
    TRISBbits.TRISB15 = 1;  //set as input pin
    TRISCbits.TRISC1 = 1;   //set as input pin for current
    TRISBbits.TRISB13 = 1;  //set as input pin

    //Trip point pin
    TRISBbits.TRISB5 = 0;   //set as output pin

    // Configure start and stop pulse as digital
    ANSBbits.ANSB2 = 0;      //set as digital
    ANSBbits.ANSB15 = 0;    // set as digital
    ANSCbits.ANSC1 = 1;     // set to analog
    ANSBbits.ANSB13 = 0; // set to digital
    ANSBbits.ANSB12 = 0;   //Set to Digital to check the frequency of the Oscillator

    //reference clock output
    ANSBbits.ANSB15 = 0;    //seta s digital
    TRISBbits.TRISB15 = 0;  //SSet as output
} <-- NOTE: was missing trailing brace here.

void Adc_Config(void)
{
    PMD1bits.ADC1MD = 0;    //Clear PMD bit to allow ADC operation

    //ADCON1
    ADCON1bits.ADSIDL = 0;
    ADCON1bits.ADSLP = 0;
    ADCON1bits.FORM = 0;
    ADCON1bits.PUMPEN = 0;
    ADCON1bits.ADCAL = 0;
    ADCON1bits.PWRLVL = 1;

    //ADCON2
    ADCON2bits.PVCFG = 0;
    ADCON2bits.NVCFG0 = 0;
    ADCON2bits.BUFORG = 1;

    //ADCON3
    ADCON3bits.ADRC = 0;
    ADCON3bits.ADCS = 0;

    ADCON3bits.SLEN0 = 1u; //FKo

    ADCON1bits.ADON = 1;
    while(ADSTATHbits.ADREADY == 0); // Wait for ready flag set.

    //Make sure ADC sample lists and Accumulator feature are in known states
    ACCONH = 0x0000;        //Disable accumulation for the moment
    ACRES = 0x00000000;     //Clear previous accumulation count
    ADL0CONLbits.SLSIZE = 1-1; //Sample list length for 1 channel
//Configure Sample List 0 settings
    ADL0CONHbits.ASEN = 0;
    ADL0CONHbits.SLINT = 1;
    ADL0CONHbits.WM = 0;
    ADL0CONHbits.CM = 0;
    ADL0CONHbits.SAMC = 1;
    ADL0CONLbits.SLTSRC = 0;    
    ADTBL0bits.ADCH = 8;    // channel 0 selected
    ADL0PTR = 0;            // Point to start of sample list 0

#if (__TEST_FREQUENCY_SETUP__)
    ADL0CONLbits.SAMP = 1;
    Nop();Nop();
    ADL0CONLbits.SAMP = 0;
    while(!IFS0bits.AD1IF && !ADSTATLbits.SL0IF) { ; }
    IFS0bits.AD1IF = 0u;
    ADSTATLbits.SL0IF = 0u;
    {
        unsigned long res = ADRES0;
        res = 15u;
    }
#endif <-- NOTE: was missing.
} <-- NOTE: missing.

void Uart_Config(void)
{
    RPOR1bits.RP2R = 3;         //Assign UART1 transmit to RP3 Peripheral
    RPOR1bits.RP3R = 4;
    RPINR18bits.U1RXR = 0;      //Assign UART1 RXD to RP0 Peripheral
    RPINR18bits.U1CTSR = 1;
    U1BRG = 50;//12;
    U1STA = 0;
    U1MODE = 0x8000;
    U1STAbits.UTXEN = 1;
}

**#define UART_BUFFER_SIZE  128

void UARTInit(void)
{
    //U1BRG = 312;   //9600 baudrate @ 24MHz osc
    U1BRG = 24;      //19200 baudrate @ 10 MHz osc

    U1MODE = 0;
    U1MODEbits.BRGH = 1;
    U1STA = 0;
    U1STAbits.URXISEL = 0;
    U1MODEbits.UARTEN = 1;
    U1STAbits.UTXEN = 1;
    IFS0bits.U1RXIF = 0;
}

void UARTPutChar(uint8_t ch)
{
    while(U1STAbits.TRMT == 0);
    Nop(); Nop();Nop(); Nop();
     Nop(); Nop();Nop(); Nop();
    U1TXREG = ch;
}

void UARTPutInt(int32_t data)
{
    int j;
    char string[UART_BUFFER_SIZE];

    sprintf(string, "%ld", data);

    j = 0;
    while(string[j] != 0)
    {
        UARTPutChar(string[j]);
        j++;
        if(j >= UART_BUFFER_SIZE)
        {
            break;
        }
    }
}

void UARTPutString(char* string)
{
    while(*string != 0)
    {
        UARTPutChar(*string);
        string++;
    }
}

void UARTPutDouble(double data)
{
    int j;
    char string[UART_BUFFER_SIZE];

    sprintf(string, "%9.3f", data);

    j = 0;
    while(string[j] != 0)
    {
        UARTPutChar(string[j]);
        j++;
        if(j >= UART_BUFFER_SIZE)
        {
            break;
        }
    }
}

char UARTWaitChar(void)
{
    while(IFS0bits.U1RXIF == 0);
    IFS0bits.U1RXIF = 0;
    return U1RXREG;
}

char UARTGetChar(void)
{
    if(IFS0bits.U1RXIF == 0)
    {
        return 0;
    }
    U1STAbits.OERR = 0;
    IFS0bits.U1RXIF = 0;
    return U1RXREG;
}

*************************************************************************************
MAIN CODE
*************************************************************************************
// PIC24FJ128GC010 Configuration Bit Settings

#define STARTUP_DELAY (5)  //Time spent waiting after device start up
#define ZERO_SET_TIME (5)  //Time spent setting the zero level
#define COUNT 5
#define DELAY for(i=0;i<COUNT;i++)
int dispOffset = 0;

void SetDutyCycle(int nDC);                //Set duty cycle for trip point
int  PulseMeasure(void);                   //Single pulse measurement
int  LevelMeasure(int, int);               //Averaged return of multiple measurements
int  adctemp;
void TestMeas(void);                       //Test routine to scan through PWM values
double StdDev(int, double, double, double);//Get sigma for measurements
int i;


/*******************************************************************************
 * Main
 ******************************************************************************/
int main(void)
{
    //Configure all of the devices

    while (Millis() < STARTUP_DELAY);                        //Wait for settling...
    //Wait for sensor to be plugged in
    **while(LevelMeasure(CTMU_BUFFER_POWER, OVER_SAMPLE) < 0);** 

    #if (__TEST_FREQUENCY_SETUP__)
        while(Millis() < 10) { ; }
        ResetMillis();
        LATBbits.LATB2 = 1;
        while(Millis() < 10) { ; }
        ResetMillis();
        LATBbits.LATB2 = 0;
    #endif
} <-- NOTE: missing brace.

int PulseMeasure(void)
{
    volatile unsigned int Vread;
    double Vtot = 0;
    int j;
    //ADL0CONLbits.SAMP = 1;       //Sample
    PULSE_PIN = 0;
    CTMUCON2 = CTMUCON2 & 0xFCFF;
    CTMUCON1bits.IDISSEN = 1;     //discharge cap
    Delay(20);
    CTMUCON1bits.IDISSEN = 0;     //Release cap from discharge
    ADL0CONLbits.SLEN = 1;
    ADL0CONLbits.SAMP = 1;
    for(j=0;j<10;j++)
    {
        Nop();Nop();Nop();Nop();
        Nop();Nop();Nop();Nop();
        Delay(1);
        CTMUCON2 = CTMUCON2 & 0xFCFF;

        PULSE_PIN = 1;
        PULSE_PIN = 0;
        while(!CTMUCON2bits.EDG1STAT);
        while(ADSTATHbits.ADBUSY);//{;}
        Nop(); Nop();
        IFS0bits.AD1IF = 0;
        ADSTATLbits.SL0IF = 0;
        ADL0CONLbits.SAMP = 0;    //Conversion

        while (!ADSTATLbits.SL0IF);

        Vread = ADRES0;
        IFS0bits.AD1IF = 0;
        ADSTATLbits.SL0IF = 0;
        ADL0CONLbits.SAMP = 1;
        UARTPutString("#gy;1;");
        UARTPutInt(Vread);
        UARTPutString("\r\n");
    }
    return (Vread);
}

PULSE_PIN es la función para la generación de pulsos. Disculpe por el largo código y el número de ediciones. ¡Pero realmente no entiendo por qué el programa funciona bien a 8 MHz y no a 30 MHz! Y si; Realmente es un oscilador de cristal de 30MHz.

POR FAVOR AYUDE !!! Soy nuevo en programación pero debo trabajar con este código para ejecutarlo a 30/2 = 16MHz para un trabajo adicional.

    
pregunta Bob

2 respuestas

3

Reduje su código al mínimo requerido para probar el reloj del sistema y el funcionamiento del temporizador, y lo ejecuté en un PIC24FJ64GA104 (cuyo reloj básico y el temporizador son similares a su PIC). Todo funcionó correctamente, incluso cuando se usa un módulo de oscilador externo en lugar de un cristal (en ese caso, POSCMD debería configurarse como EC ).

Cambié un par de cosas que podrían impedir que la prueba de frecuencia funcione correctamente: -

  1. Deshabilitó el temporizador de vigilancia (está habilitado de forma predeterminada, lo que hará que la MCU se reinicie repetidamente si no se reinicia periódicamente).

  2. Incluyó el código de prueba de frecuencia en un bucle while(1) , para que se ejecute continuamente en lugar de salir de main() después de una iteración.

Prueba mi código en tu PIC. Si no funciona, es posible que tenga un problema de hardware (el oscilador no se inicia, el suministro de energía es insuficiente, el cableado incorrecto, etc.).

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <xc.h>               // automatically includes MCU selected in project

#pragma config POSCMOD = HS   // Primary Oscillator Select (HS = crystal, EC = external clock input)
#pragma config OSCIOFNC = OFF // OSCO Pin Configuration (OSCO/CLKO/RC15 functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor Configuration bits (Disabled)
#pragma config FNOSC = PRI    // Initial Oscillator Select (Primary Oscillator (XT, HS, EC))
#pragma config IESO = OFF     // Internal External Switchover (Disabled)
#pragma config FWDTEN = OFF   // watchdog timer (off)

//******************************************************************************
#include "timer.h"

unsigned millisCount = 0;

void __attribute__ ((interrupt, no_auto_psv)) _T2Interrupt(void)
{
  IFS0bits.T2IF = 0;
  millisCount++;
}

/*******************************************************************************
 * Returns the number of milliseconds since the device was started as an
 * unsigned long.
 ******************************************************************************/
unsigned long Millis()
{
  return millisCount;
}

void ResetMillis(void)
{
  millisCount = 0u;
}

void Delay(unsigned long delayTime)
{
  volatile int delayStart = Millis();
  while (Millis() - delayStart < delayTime);
}

//******************************************************************************
// INITIALISATION CODE
//******************************************************************************

void Timer2_Config(void)
{
    T2CON = 0;
    TMR2 = 0x0000;                      //Turn off Timer2
    PR2 = 0xFFFF;                       //Period register at maximum

    _T2IF = 0;                          //clear Timer2 interupt flag
    _T2IE = 1;                          //Enable Time2 interupt

    //T2CON
    T2CONbits.T32 = 0;                  //Timer2 and T3 or T4 and T5 act as two 16-bit timers
    T2CONbits.TCKPS = 0;                //Timer Prescale 1:1
    T2CONbits.TGATE = 0;                //Gated time accumulation is disabled
    T2CONbits.TSIDL = 0;                //Continues module operation in Idle mode
    T2CONbits.TCS = 0;                  //Internal clock (FOSC/2)
    T2CONbits.TON = 1;                  //Starts Timer2
}

#define PULSE_PIN LATBbits.LATB2

void Io_Config(void)
{
 // pulse pin
    PULSE_PIN = 0;
    TRISBbits.TRISB2 = 0;   //set as output
} 

//******************************************************************************
//MAIN CODE
//******************************************************************************

#define STARTUP_DELAY (5)  //Time spent waiting after device start up

int main(void)
{
    //Configure all of the devices
    Io_Config();
    Timer2_Config();
    while (Millis() < STARTUP_DELAY){};   //Wait for settling...
    while (1)
    {
       while(Millis() < 10) { ; }
       ResetMillis();
       LATBbits.LATB2 = 1;
       while(Millis() < 10) { ; }
       ResetMillis();
       LATBbits.LATB2 = 0;
    }
        return 0;
} 
    
respondido por el Bruce Abbott
1

enlace (documento bastante importante) muestra en la sección 35.8 que la frecuencia máxima para un cristal El oscilador es de 25 MHz. Si necesita una frecuencia más alta, necesita un cristal de frecuencia más baja y el PLL interno para generarlo.

También puede tomar una fuente de reloj externa de hasta 32 MHz, pero eso no es lo mismo que un cristal.

    
respondido por el Scott Seidman

Lea otras preguntas en las etiquetas