How to set PIC for INT RC Clock Freq

For general Flowcode discussion that does not belong in the other sections.
Post Reply
yuheiwai
Posts: 3
http://meble-kuchenne.info.pl
Joined: Sun Dec 05, 2021 6:13 pm

How to set PIC for INT RC Clock Freq

Post by yuheiwai »

How do you set a PIC, for example a 16F1825 or the PIC Kit Demo 18F45K20 to run from INT RC Clock at 31kHz??

I can't find how to do this with FlowCode (set PICs to run the INT RC clock at 31kHz)?

Thx

medelec35
Matrix Staff
Posts: 1917
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 621 times
Been thanked: 645 times

Re: How to set PIC for INT RC Clock Freq

Post by medelec35 »

Hi yuheiwai.
Welcome to the Flowcode forums.
Within the Component Libraries ribbon, select Search and enter intosc.
That with display the internal oscillator helper component, just select add to 2D panel.
Right-click on the helper and select properties.
Within properties select the frequency you want the internal osc to run at.
The correct code will be displayed for you to copy. e.g.

Code: Select all

OSCCON = 0x00;
Highlight the code by clicking on it and you can copy using the right-click context menu.
Add a C Code icon from the Command Icons section to the very top of Main.
Note: Any new icons/components added must be placed below the OSCCON C code icon.
The last thing to do is to select Project Options from the Build ribbon.
Enter the frequency in MHz e.g 0.031 for 31KHz.
Within Configure.
Change:
Oscillator Selection to INTOSC oscillator
Watchdog Timer Enable to disabled.
MCLR to Disabled (Only If you have not got MCLR line pulled up to the supply voltage)
PLL Enable to Disabled
Low-Voltage Programming Enabled to Disabled (If programming via ICSP)
After selecting Modify you should be good to go.
Just make sure the one-second flasher test is correct, before creating your project.
At such a low speed there may be components that don't run as expected, so be aware of that.
Martin

yuheiwai
Posts: 3
Joined: Sun Dec 05, 2021 6:13 pm

Re: How to set PIC for INT RC Clock Freq

Post by yuheiwai »

I can measure 8kHz at RA6 so the clock is running correctly at 31kHz (same as MPLAB X). BTW, I could not find the PLL disable, it does not show up as an option.

The issue I have now is the delay function is 1.68X higher than expected (eg, 100ms delay in code results in 168ms delay in hardware). Simple code shown below.


Code: Select all

//************************************************************************************
//**  
//**  Source name:   C:\Users\Sunny\Documents\Flowcode V9\Examples\18F45k20-blink led2 w looptime.fcfx
//**  Title:         18F45k20 blink led2 w looptime check
//**  Description:   
//**  Device:        PIC.18F.18F45K20
//**  
//**  Generated by:  Flowcode v9.2.0.33
//**  Date:          Tuesday, December 14, 2021 21:22:11
//**  Users:         1
//**  Registered to: yuheiwai (66428356)
//**  Licence key: 6QNJFN
//**  
//**  
//**  https://www.flowcode.co.uk
//**  
//************************************************************************************


#define MX_PIC

#define MX_CAL_PIC

#define MX_CLK_SPEED 31000

#define FCP_NULL Unconnected_Port


#include <xc.h>
#include <math.h>
//Chip Configuration Settings
__PROG_CONFIG(0x1, 0xF9FF);
__PROG_CONFIG(0x2, 0xFEFF);
__PROG_CONFIG(0x3, 0xFFFF);
__PROG_CONFIG(0x4, 0xFFBB);
__PROG_CONFIG(0x5, 0xFFFF);
__PROG_CONFIG(0x6, 0xFFFF);
__PROG_CONFIG(0x7, 0xFFFF);

/*========================================================================*\
   Use :Include the type definitions
\*========================================================================*/
#include "C:\ProgramData\MatrixTSL\FlowcodeV9\CAL\internals.c"





/*========================================================================*\
   Use :panel
       :Variable declarations
       :Macro function declarations
\*========================================================================*/
#define FCV_FALSE (0)
#define FCV_TRUE (1)
MX_GLOBAL MX_BOOL FCV_X1 = (1);

void FCD_03d92_led_base__TurnOn();
void FCD_03d92_led_base__TurnOff();
void FCD_03d91_led_base__TurnOn();
void FCD_03d91_led_base__TurnOff();

/*========================================================================*\
   Use :Include the chip adaption layer
\*========================================================================*/
#include "C:\ProgramData\MatrixTSL\FlowcodeV9\CAL\includes.c"


/*=----------------------------------------------------------------------=*\
   Use :Supplementary defines
\*=----------------------------------------------------------------------=*/

/*========================================================================*\
   Use :led_base
       :Macro implementations
\*========================================================================*/
/*=----------------------------------------------------------------------=*\
   Use :Turn the LED off.
\*=----------------------------------------------------------------------=*/
void FCD_03d92_led_base__TurnOn()
{

    #if (1)

        // pin = polarity
        SET_PORT_PIN(D, 0, 1);

    // #else

    //Code has been optimised out by the pre-processor
    #endif

}

/*=----------------------------------------------------------------------=*\
   Use :Turn the LED on.
\*=----------------------------------------------------------------------=*/
void FCD_03d92_led_base__TurnOff()
{

    #if (1)

        // pin = 1 - polarity
        SET_PORT_PIN(D, 0, 1 - 1);

    // #else

    //Code has been optimised out by the pre-processor
    #endif

}


/*========================================================================*\
   Use :led_base
       :Macro implementations
\*========================================================================*/
/*=----------------------------------------------------------------------=*\
   Use :Turn the LED off.
\*=----------------------------------------------------------------------=*/
void FCD_03d91_led_base__TurnOn()
{

    #if (1)

        // pin = polarity
        SET_PORT_PIN(C, 0, 1);

    // #else

    //Code has been optimised out by the pre-processor
    #endif

}

/*=----------------------------------------------------------------------=*\
   Use :Turn the LED on.
\*=----------------------------------------------------------------------=*/
void FCD_03d91_led_base__TurnOff()
{

    #if (1)

        // pin = 1 - polarity
        SET_PORT_PIN(C, 0, 1 - 1);

    // #else

    //Code has been optimised out by the pre-processor
    #endif

}

/*=----------------------------------------------------------------------=*\
   Use :Supplementary implementations
\*=----------------------------------------------------------------------=*/




/*========================================================================*\
   Use :Main
\*========================================================================*/
void main()
{
    #ifdef INTOSCHELPER
        INTOSCHELPERCODE
    #endif
ANSEL = 0x00;
ANSELH = 0x00;




    // Name: C Code, Type: C Code:
    OSCCON = 0x00;
    OSCTUNE = 0x00;

    //Comment:
    //Clkout/4 = Port RA6
    //Loop Timing check = Port RC0

    // Name: Output, Type: Output: 0 -> C0
    SET_PORT_PIN(C,0,(0));

    // Name: Loop, Type: Loop: While 1
    while (1)
    {

        // Name: Output, Type: Output: X1 -> C0
        SET_PORT_PIN(C,0,(FCV_X1));

        // Name: Output, Type: Output: 0 -> D0
        SET_PORT_PIN(D,0,(0));

        // Name: Delay, Type: Delay: 100 ms
        FCI_DELAYBYTE_MS(100);

        // Name: Output, Type: Output: 1 -> D0
        SET_PORT_PIN(D,0,(1));

        // Name: Delay, Type: Delay: 100 ms
        FCI_DELAYBYTE_MS(100);

        // Name: Calculation, Type: Calculation:
        // X1 = NOT (X1)
        FCV_X1 = ~(FCV_X1);


    }

    mainendloop: goto mainendloop;
}



/*========================================================================*\
   Use :Interrupt
\*========================================================================*/
void MX_INTERRUPT_MACRO(void)
{

}

medelec35
Matrix Staff
Posts: 1917
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 621 times
Been thanked: 645 times

Re: How to set PIC for INT RC Clock Freq

Post by medelec35 »

I have placed the code within a code block so that it takes up much less room.
My take on this is there is nothing wrong with the flowchart as you have proven the clockout frequency is correct
At 31KHz was probably designed to use assembly only due to the overheads after being converted to C
I don't believe C was designed to run on Microcontrollers with such low speeds.
The only thing I can think of is either fudge it or run at a faster speed (in the MHz).
For the fudge. If the actual delay is 168ms instead of 100ms then multiply each delay by 100/168 = 0.59.
So change the delay to 60ms.
Not ideal but the alternative fudge could be to reduce the project options clock speed.
That won't be by 0.59 so you will have to experiment.
For the PLL is was only referring to the 16F1825.
Different microcontrollers have different ways of dealing with PLL.
Yours is fine as it is as the frequency on RA6 is correct.
Martin

yuheiwai
Posts: 3
Joined: Sun Dec 05, 2021 6:13 pm

Re: How to set PIC for INT RC Clock Freq

Post by yuheiwai »

My understanding (last time I used Flowcode was v4.3) that Flowcode compiled into assembly and ran at the same speed as the MPLAB or Hi-Tech compilers. Flowcode just needed more flash space.

Is the XC8 Microchip compiler and option for Flowcode?

medelec35
Matrix Staff
Posts: 1917
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 621 times
Been thanked: 645 times

Re: How to set PIC for INT RC Clock Freq

Post by medelec35 »

With up-to Flowcode v6 the flowchart was converted to C and the BoostC compiler compiled to hex.
From V7 onwards the compiler was changed to the free version of XC8 so many new target devices can be included as BoostC was slow to add new devices.
As the processor is running really slow, it adds to the delay.
Also, XC8 free won't be as fully-optimised as the professional version also adding time to the delay at very slow speeds.
At the more normal speeds, this will not be an issue.
Martin

Post Reply