Hot!external oscillator issue in PIC24FJ128 seires

Author
yo123
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2017/04/19 09:52:08
  • Location: 0
  • Status: offline
2018/08/11 01:35:48 (permalink)
0

external oscillator issue in PIC24FJ128 seires

hii ,
i am working on uart in PIC24 controller. i initialised the UART on 9600 baud rate and the oscillator i used is external on HS mode. but the problem is that  at 9600 baud rate and 16Mhz freq and FCY=8MHz , i supposed to select BRG value = 207;
but this not going to work.when i select BRG value =103; then only uart work. how could this possible . i ahve selct configuration bit as below
// CONFIG3
#pragma config WPFP = WPFP511 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 85))
#pragma config WPDIS = WPDIS // Segment Write Protection Disable bit (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS // Configuration Word Code Page Protection Select bit (Last page(at the top of program memory) and Flash configuration words are not protected)
#pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select bit (Write Protect from WPFP to the last page of memory)

// CONFIG2
#pragma config POSCMOD = HS // Primary Oscillator Select (HS oscillator mode selected)
#pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable bit (Write RP Registers Once)
#pragma config OSCIOFNC = ON // Primary Oscillator Output Function (OSCO functions as port I/O (RC15))
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-safe Clock Monitor are disabled)
#pragma config FNOSC = PRI // Oscillator Select (Primary oscillator (XT, HS, EC))
#pragma config IESO = OFF // Internal External Switch Over Mode (IESO mode (Two-speed start-up)disabled)

// CONFIG1
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Standard Watchdog Timer is enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled)
#pragma config ICS = PGx1 // Comm Channel Select (Emulator functions are shared with PGEC1/PGED1)
#pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = ON // JTAG Port Enable (JTAG port is enabled)

 and UART initialization
U1BRG=103; 
   U1MODEbits.UARTEN=1;
   U1MODEbits.BRGH=1;
   
   U1STAbits.UTXISEL0=1;
   U1STAbits.UTXISEL1=1; //Reserved
   U1STAbits.UTXEN=1; //UARTx transmitter enabled, UxTX pin controlled by UARTx (if UARTEN = 1)
   U1STAbits.RIDLE=1; //Receiver is active
   U1STAbits.URXISEL=0b00;

please help me to solve this issue
thanking you
 
#1

8 Replies Related Threads

    davekw7x
    Entropy++
    • Total Posts : 1537
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: external oscillator issue in PIC24FJ128 seires 2018/08/11 07:44:34 (permalink)
    +2 (2)
    yo123
    ...when i select BRG value =103; then only uart work. how could this possible

    4e6/4/9600 - 1 = 103

    If Fcy is equal to 4 MHz, then, with BRGH = 1, 103 is the correct value for 9600baud.

    So, here's the thing...
    You say that Fcy is 8 MHz, but your UART functionality seems to indicate that Fcy is 4 MHz.  Well...

    If you have a 'scope you can configure the Reference Clock Output to be some value that depends on Fosc, and see if you get what you expect.

    However...
    If your board has an LED, here is a quick way to determine that Fcy is 8 MHz rather than 4 MHz.  Easier than trying to figure out the Reference Clock stuff, and doesn't require a 'scope.

    (If your board doesn't have an LED, add one, in series with an appropriate resistor, to one of your output pins.  I have found that having an LED is always useful when evaluting new CPUs or new functionality, even if the final project doesn't have an LED.)

    Create a project PIC for your PIC.  Add a file named, say, Main.c, containing something like the following


    // Put configuration bits here

    #include <xc.h>

    // To use __delay_ms(), you must define FCY before including <libpic30.h>
    //
    // If your Fcy is actually 8 MHz, __delay_ms(1000) gives 1 second delay.
    // If your Fcy is actually 4 MHz, __delay_ms(1000) gives 2 second delay.
    // If your Fcy is something else, different delays will be generated.
    //
    #define FCY 8000000
    #include <libpic30.h>

    #define LED1 LATAbits.LATA8
    #define LED1_TRIS TRISAbits.TRISA8

    int main()
    {

        // If you have any code that affects the clock, put it here

        LED1_TRIS = 0;
        while (1) {
            LED1 = 1;
            __delay_ms(1000);
            LED1 = 0;
            __delay_ms(1000);
        } // End of main loop
    } // End of main



    If your Fosc is 16 MHz (Fcy = 8 MHz), you will see the LED blink one-second-on, one-second-offf.
    If your Fosc is 8 MHz (Fcy = 4 MHz), you will see the LED blink two-seconds-on, two-seconds-off.

    Bottom line: Verify clock frequency before you implement UART code or any other code that depends on Fosc.  The quick and easy way to eye-ball it is to make an LED blink at an easily observable rate.  Clock errors are almost always off by a factor of 2 or 4 or more, so verification is easy.
     
    Now, if it turns out that your Fcy is not what you expected, then you have to give us some more information about your circuit, your crystal, etc.   For example, you say you have an external oscillator, but the HS mode in your configuration pragma is for a crystal.  What do you have on your board?  Oscillator or Crystal?
    You should also tell us which device you are using.  Sometimes it makes a difference to people who would like to help.
     
    Finally: Did you mean to leave JTAG enabled?  Nothing to do with clock frequency, but that is a common source of problems that may crop up later on, after you get the clock configuration fixed.
    Recommendation:  Turn it off unless you really need it and have appropriate connections to the JTAG pins so that they won't interfere with normal operation with no external device attached to the JTAG pins.

    Regards,

    Dave



    post edited by davekw7x - 2018/08/11 08:50:12

    Sometimes I just can't help myself...
    #2
    yo123
    New Member
    • Total Posts : 19
    • Reward points : 0
    • Joined: 2017/04/19 09:52:08
    • Location: 0
    • Status: offline
    Re: external oscillator issue in PIC24FJ128GA106 seires 2018/08/13 23:34:04 (permalink)
    0
    thanks for your response,
    as i know the __delay_ms(1000) function gives delay of 1sec and it does not depend upon the crystal you used(may be i wrong). whatever the crystal value it gives 1 sec delay only. external crystal i used 4MHzand  now i change the  #pragma value to
     
    #define FCY 2000000
    // CONFIG2
    #pragma config POSCMOD = XT             // Primary Oscillator Select (XT oscillator mode selected)
    #pragma config OSCIOFNC = OFF           // Primary Oscillator Output Function (OSCO functions as CLKO (FOSC/2))
    #pragma config FNOSC = PRI              // Oscillator Select (Primary oscillator (XT, HS, EC))
    #pragma config JTAGEN = OFF             // JTAG Port Enable (JTAG port is disabled)
     
    U1BRG=51;
    BOARD PIC24FJ128GA106
      after this i dint get any output on 103, 51, 25, 207.
    sad: sad
    post edited by yo123 - 2018/08/13 23:36:05
    #3
    davekw7x
    Entropy++
    • Total Posts : 1537
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: external oscillator issue in PIC24FJ128GA106 seires 2018/08/14 06:31:37 (permalink)
    +1 (1)
    yo123
    as i know the __delay_ms(1000) function gives delay of 1sec and it does not depend upon the crystal you used(may be i wrong).

    Well, you are flat wrong.
    __delay_ms(1000) will give a delay of 1 second if and only if your actual instruction frequency (Fosc/2) is equal to the value in your definition of FCY.  Your definition of FCY does not change any aspect of the actual circuit operation; it just tells the compiler what instructions to generate in order to get the given delay.
     
    The amount of delay from __delay_ms() depends on your definition of FCY and it depends on the actual frequency of operation.   Period.  Full Stop.
     
    My suggestion:
     
    1. Please, go back to your original program, where you got satisfactory UART operation for the BRG value you told us about.
    2. DO NOT CHANGE anything in your pragmas.  DO NOT CHANGE anything in your initialization.
    3. Add a definition for LED and LED_TRIS, similar to what I showed. Define FCY and include libpic30.h.
    4. Change the code for the main loop as I showed.  DO NOT do anything with the UART just yet.
    5. Tell us the duration of the LED on-time and off-time.

      If it is not one second, then that means your circuit is not operating with the frequency you think it is.
      If it is one second, then something that affects UART operation is incorrect (something that you did not tell us about).

    6. Bottom line, in either case:  Post a complete program that we can compile and test.
     
    Maybe someone can help you get to the bottom of things.
     
     
    Regards,

    Dave
     
    post edited by davekw7x - 2018/08/14 08:29:00

    Sometimes I just can't help myself...
    #4
    yo123
    New Member
    • Total Posts : 19
    • Reward points : 0
    • Joined: 2017/04/19 09:52:08
    • Location: 0
    • Status: offline
    Re: external oscillator issue in PIC24FJ128GA106 seires 2018/08/15 10:06:09 (permalink)
    0


    // 'C' source line config statements

    // 'C' source line config statements

    // CONFIG3
    #pragma config WPFP = WPFP511 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 85))
    #pragma config WPDIS = WPDIS // Segment Write Protection Disable bit (Segmented code protection disabled)
    #pragma config WPCFG = WPCFGDIS // Configuration Word Code Page Protection Select bit (Last page(at the top of program memory) and Flash configuration words are not protected)
    #pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select bit (Write Protect from WPFP to the last page of memory)

    // CONFIG2
    #pragma config POSCMOD = HS // Primary Oscillator Select (HS oscillator mode selected)
    #pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable bit (Write RP Registers Once)
    #pragma config OSCIOFNC = OFF // Primary Oscillator Output Function (OSCO functions as CLKO (FOSC/2))
    #pragma config FCKSM = CSECMD // Clock Switching and Monitor (Clock switching is enabled, Fail-safe Clock Monitor is disabled)
    #pragma config FNOSC = PRI // Oscillator Select (Primary oscillator (XT, HS, EC))
    #pragma config IESO = ON // Internal External Switch Over Mode (IESO mode (Two-speed start-up) enabled)

    // CONFIG1
    #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
    #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
    #pragma config WINDIS = OFF // Watchdog Timer Window (Standard Watchdog Timer is enabled,(Windowed-mode is disabled))
    #pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled)
    #pragma config ICS = PGx1 // Comm Channel Select (Emulator functions are shared with PGEC1/PGED1)
    #pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed)
    #pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled)
    #pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled)

    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF..

    #define _XTAL_FREQ 16000000UL
    #define FCY 8000000
    #include <xc.h>
    #include <i2c.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <libpic30.h>
    #include "uart.h"
    int main(void)
    {
        AD1PCFG = 0xFFFF; //turn off All Analog pin
        //OSCCONH Unlock Sequence
    __builtin_write_OSCCONH(0x78);
    __builtin_write_OSCCONH(0x9A);

    //Oscillator select
    __builtin_write_OSCCONH(0x02);

    //OSCCONL Unlock Sequence
    __builtin_write_OSCCONL(0x46);
    __builtin_write_OSCCONL(0x57);


    __builtin_write_OSCCONL(OSCCON | 0x01); //turn on OSWEN
    while (OSCCONbits.OSWEN); //wait until OSWEN to low

    // Wait for Clock switch to occur
    while (OSCCONbits.COSC!= 0b010);
    CLKDIVbits.DOZE = 0b000;
    REFOCONbits.RODIV = 0b0000;
    REFOCONbits.ROSEL = 1;
    REFOCONbits.ROSSLP = 1;
        TRISB=0x00;
        PPS_Set();
        Setting_interrupt();
        
        Uart1_Init();
         __delay_ms(1000);
          

        while(1)
        {
            
            PORTB = 0xFFFF;
            __delay_ms(1000);
            PORTB = 0x0000;
            __delay_ms(1000);
            
            UART1_Write_CText("Initialization complete");
        }
        return 0;
    }


    //https://www.microchip.com/forums/m846065.aspx
    //http://microchipdeveloper.com/16bit:osc




    i have using 16MHz crystal (High Speed). still my uart operate at 4Mhz(FCY), please the check  above code and correct me where i am wrong.

    UART setting




       U1BRG=103;
       U1MODEbits.UARTEN=1;
       U1MODEbits.BRGH=1;
       
        U1STAbits.UTXISEL0=1;
        U1STAbits.UTXISEL1=1;   //Reserved
       U1STAbits.UTXEN=1;       //UARTx transmitter enabled, UxTX pin controlled by UARTx (if UARTEN = 1)
        U1STAbits.RIDLE=1;     //Receiver is active
         U1STAbits.URXISEL=0b00;



     

     
    post edited by yo123 - 2018/08/15 10:15:11
    #5
    davekw7x
    Entropy++
    • Total Posts : 1537
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: external oscillator issue in PIC24FJ128GA106 seires 2018/08/15 13:26:27 (permalink)
    +1 (1)
    So, I'm guessing that if you have an LED or a scope on one of the PORTB pins acting as an output that you see two-seconds-on, two-seconds-off.  (But---sigh---why should we have to guess?  Couldn't you have just told us, as I indicated in suggestion 5 of my previous post?)
     
    If this is what you see, then, since you have defined FCY to be 8 MHz and since __delay_ms(1000) gives a two-second delay, for sure the chip is operating with, Fosc = 8 Mhz (Fcy = 4 MHz).
     
    That's what happens if your configuration parameters tell it to use PRI and HS,  and if your crystal is not present or for some other reason the Primary Oscillator is not working.  It falls back on the internal FRC operating at 8 MHz.
     
    Then, as we have previously determined, 103 is the proper setting for BRG to get 9600 Baud.
     
    So, (assuming what that I previously assumed is true), we get down to circuitry.  Maybe you can show us the layout and give us some indication of the pedigree of your 16 MHz crystal.
     
     
    After making sure that Power, Ground, and Reset connections are correct, my methodology for starting development with new hardwere is the following.    Every time.  Really.  Every single time:
    1. Impelemnt a simple loop (using __delay_ms()) to make sure that you know what frequency your chip is operating at.  If it's not what you want, then STOP.  Don't worry about UART or timers or anything else until you get it right.
    2. Then, and only then, calculate BRG based on the correct Fcy and on your required UART serial rate.
     
     
    Regards,

    Dave
    post edited by davekw7x - 2018/08/15 14:51:18

    Sometimes I just can't help myself...
    #6
    yo123
    New Member
    • Total Posts : 19
    • Reward points : 0
    • Joined: 2017/04/19 09:52:08
    • Location: 0
    • Status: offline
    Re: external oscillator issue in PIC24FJ128GA106 seires 2018/08/16 04:54:45 (permalink)
    0
    hii,
     hardware not yet came so i check code in proteus ( earlier i worked on 877A, 1508, 1824 in proteus).
    [image]C:\Users\Yogesh\Desktop\pic24.png[/image]
    as you said if external clock failed then controller will shift into internal oscillator. so for checking the clock failure i enable the fail clock safe monitor (FCSM) and then what i observed after enabling the FCSM(earlier it was disable) that my code stuck at

    // Wait for Clock switch to occur
    while (OSCCONbits.COSC!= 0b010);

    so what i thought the external clock does not work properly. then i checked clock failure bit

    if(OSCCONbits.CF==1)
          {
             PORTB = 0xFFFF;
             __delay_ms(1000);
             PORTB = 0x0000;
             __delay_ms(100);
           }

    but this is also not showing anythingsad: sad.
     
     
     
    post edited by yo123 - 2018/08/16 04:55:58

    Attached Image(s)

    #7
    davekw7x
    Entropy++
    • Total Posts : 1537
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: external oscillator issue in PIC24FJ128GA106 seires 2018/08/16 11:04:51 (permalink)
    0
    yo123
     
     hardware not yet came so i check code in proteus ( earlier i worked on 877A, 1508, 1824 in proteus).
     

    We could have saved some bandwidth if I had known you were simulating, not observing hardware.
     
    I simply never simulate hardware functionality, so I can't say whether your results are valid.  Sorry.
     
    Bottom line: When you get your hardware hooked up, if you can't understand test results, I invite you to post again.  In the meanwhile, maybe others can help you with simulation results.
     
    Regards,

    Dave

    Sometimes I just can't help myself...
    #8
    yo123
    New Member
    • Total Posts : 19
    • Reward points : 0
    • Joined: 2017/04/19 09:52:08
    • Location: 0
    • Status: offline
    Re: external oscillator issue in PIC24FJ128GA106 seires 2018/08/16 22:45:52 (permalink)
    0
    grin: grin

     
    Bottom line: When you get your hardware hooked up, if you can't understand test results, I invite you to post
     



    i should get trail on hardware only,
    thanks,
    Dave
    #9
    Jump to:
    © 2018 APG vNext Commercial Version 4.5