Hot!Problems with Programming Oscillator in XC16

Author
rfjhh
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2012/07/26 09:19:36
  • Location: 0
  • Status: offline
2018/11/08 07:08:31 (permalink)
0

Problems with Programming Oscillator in XC16

Hi
I'm programming a simple sequence in a dsPIC33FJ32MC202, but I have some problems with OSCCON setup.
I perform the following code:
#include <p33FJ32MC202.h>
#include<xc.h>
#include<stdint.h>
//FBS
#pragma config BSS = NO_FLASH // No Boot program Flash segment
#pragma config BWRP = WRPROTECT_OFF // Boot segment is write-protected
//FGS
#pragma config GSS = OFF // User program memory is not code-protected
#pragma config GWRP = OFF // User program memory is not write-protected
//**********FOSCSEL
#pragma config IESO = OFF // Start-up device w/user-selected osc source
#pragma config FNOSC = FRC // Internal Fast RC (FRC)
//**********FOSC
#pragma config FCKSM = CSECMD  // Both Clock Switching and Fail-Safe Clock Monitor are enabled
#pragma config IOL1WAY = OFF     // Allow Multiple Re-configurations
#pragma config OSCIOFNC = OFF  // OSC2 pin has clock out function
#pragma config POSCMD = XT      // XT Oscillator Mode
//FWDT
#pragma config FWDTEN = OFF // Watchdog timer enabled/disabled by user software
//FPOR
#pragma config ALTI2C = OFF // I2C mapped to SDA1/SCL1 pins
#pragma config FPWRT = PWR128 // 128ms
//FICD
#pragma config JTAGEN = OFF // JTAG is Disabled
#pragma config ICS = PGD1 // Communicate on PGC1/EMUC1 and PGD1/EMUD1
#define LED _LATA0
#define LED_TRIS _TRISA0
int main()
{
int visor;
LED_TRIS = 0; // Inicializa el LED

_T1IF = 0;
_T1IE = 0;
TMR1 = 0x0000;
PR1 = 0x000A;//0x7A12;
T1CONbits.TCKPS = 3;
T1CONbits.TON = 1;
//__builtin_write_OSCCONH(0x78);
//__builtin_write_OSCCONH(0x9A);
__builtin_write_OSCCONH(0x02); //  HS oscillator
//__builtin_write_OSCCONL(0x46);
//__builtin_write_OSCCONL(0x57);
__builtin_write_OSCCONL(0x01); // switch enable
visor = OSCCONbits.COSC;         // check COSC value
while(OSCCONbits.COSC != 0b010){} // Wait for oscillator switch is completed

enum
{
IDLE,BLINK_LED 
}demoState;

while(1)
{
switch(demoState)
{
case IDLE:
if(_T1IF == 1) // TMR1 = PR1
{
_T1IF = 0; // clear TMR1 interrupt
demoState = BLINK_LED;
}
break;
case BLINK_LED:
LED^= 1; 
demoState = IDLE;
break;
}
}
return 0;
}
#1

16 Replies Related Threads

    Jim Nickerson
    User 452
    • Total Posts : 5578
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/08 07:10:07 (permalink)
    0
    Code tags makes code soo much easier to read, learn how to use them in Ian's post here https://www.microchip.com/forums/FindPost/777101
    #2
    du00000001
    Just Some Member
    • Total Posts : 2238
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: Problems with Programming Oscillator in XC16 2018/11/08 08:12:34 (permalink)
    0
       ... , but I have some problems with OSCCON setup.   

    And what exactly is your problem?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    rfjhh
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/08 11:51:57 (permalink)
    0
    The problem is the program doesn't run beyond the line 
     while(OSCCONbits.COSC != 0b010){}
    It seems the COSC is not set to 0x02
     
     
    #4
    du00000001
    Just Some Member
    • Total Posts : 2238
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: Problems with Programming Oscillator in XC16 2018/11/08 13:25:13 (permalink)
    0
    Do you have an external oscillator attached? Is it operational (oscillating)?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #5
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2768
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/08 14:39:05 (permalink)
    0
    Download this document from Microchip:
    Section 39. Oscillator (Part III)
     
    A lot more detailed than the chip pdf.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #6
    Aussie Susan
    Super Member
    • Total Posts : 3366
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/08 19:19:19 (permalink)
    0
    Not your problem but don't have the first 'include' - the <xc.h> will select the correct MCU for you (based on the chip you set in the IDE for the project).
    I think your comment for the FCKSM config setting is wrong: CSECMD means the clock monitor is disabled.
    I am a little surprised that you are not getting at least a warning about the 'enum' declaration in the middle of your code. If the compiler accepts it, then all must be well.
    I would also set the oscillator up before I tried to set a timer going.
    The output pin you are using, RA0, also has AN0 function on it and so you need to make sure the pin is set to 'digital'mode. See the documentation about the AD1PCFGL register.
    Also not wrong as such, I prefer to write to the OSCCON lower byte with '__builtin_write_OSCCONL(OSCCON ! 0x01)' so that I don't alter the other R/W bits in that byte.
    Susan
    #7
    qhb
    Superb Member
    • Total Posts : 7893
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Problems with Programming Oscillator in XC16 2018/11/08 19:24:51 (permalink)
    0
    Aussie Susan
    ...
    The output pin you are using, RA0, also has AN0 function on it and so you need to make sure the pin is set to 'digital'mode. See the documentation about the AD1PCFGL register.

    That's good practice, but doesn't matter if all writes to PORTA are via the LATA register, as they are here.
     
    #8
    JPortici
    Super Member
    • Total Posts : 453
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 02:11:15 (permalink)
    0
    Besides everything that has been told.. Invest some time in writing understandable comments.
    In my experience if you can't switch between two clock sources the issue is
    1)You are not following the datasheet: To switch between two PLL sources like FRC+PLL to POSC+PLL you must first switch to a non-PLL source. NOT THE CASE HERE
    2)The configuration is wrong: Clock switching is not enabled, the POSC type is not selected correctly (using EC with a crystal, using HS when XT should be selected). CSW here is enabled but maybe the clock type is not correct
    3)The new clock source is not running. See the above.
    #9
    MBedder
    Circuit breaker
    • Total Posts : 6558
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 05:14:19 (permalink)
    0
    I wonder why on Earth they implement the clock switching instead of selecting the appropriate final clock source/mode by the FOSC/FOSCSEL configuration bits?
    #10
    JPortici
    Super Member
    • Total Posts : 453
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 05:25:09 (permalink)
    0
    Gee i don't know..
    In general: Low Power modes and functional safety (backup clocks) come to mind
    If you meant the OP, would be nice to know. The MCU will run from FRC until the new clock is ready anyway
    post edited by JPortici - 2018/11/09 05:27:11
    #11
    rfjhh
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 09:25:22 (permalink)
    0
    Now I'm using Proteus
    #12
    JPortici
    Super Member
    • Total Posts : 453
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 12:17:42 (permalink)
    0
    So you are saying that the SIMULATOR in proteus is can't simulate clock switching?
    #13
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2768
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 14:03:47 (permalink)
    0
    You tell Proteus what speed the simulator runs at.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #14
    rfjhh
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 19:46:21 (permalink)
    0
    Yes, When I simulate and debug, the program does not leave the "while" loop:
    while(OSCCONbits.COSC != 0b010){}
    so, I think it means the switch is not accomplished..
    I use the variable "visor" in order to see what's happening with NOSC bits in the watch window
     
    #15
    dan1138
    Super Member
    • Total Posts : 2897
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/09 19:58:35 (permalink)
    5 (1)
    The Microchip simulator does not simulate clock switching behavior completely for PIC controllers.
     
    You will need to validate any clock switching code on real hardware.
     
    I suspect that the Proteus simulator has the same implementation.
    #16
    JPortici
    Super Member
    • Total Posts : 453
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Problems with Programming Oscillator in XC16 2018/11/11 00:13:13 (permalink)
    5 (2)
    in the future, please say that you are using the simulator whenever it's the case. You would have saved you and everybody else a lot of time :/
    There are things that the simulator does not simulate. Namely all analog functions. In the MPLAB simulator documentation there should be a list of the peripherals that are not fully simulated, i would think that proteus should have such list.
    Me, i use simulators only to test code excecution time, as i always have the physical board,test jig and a scope beside me
    post edited by JPortici - 2018/11/11 00:16:50
    #17
    Jump to:
    © 2018 APG vNext Commercial Version 4.5