• AVR Freaks

Hot!Creating a new source file in MPLAB X IDE

Author
didierleplae
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
2019/12/09 17:03:02 (permalink)
0

Creating a new source file in MPLAB X IDE

I am writing a code for charlie-plexing 12 LEDs using four pins on a PIC. 
I thought it would be useful to create a source file that I could bring into any future projects . 
I wrote functions that work if included within my main.c file. However, I can't seem to make them work in a separate source file. I've never created my own source file before and must be missing something.
I've noticed that usually there is a corresponding header file for every source file in the MCC generated files, but is that always needed?
 
Here is my source file code (charlieplex_config.c):

#include <xc.h>

void LED1_ON (void){
TRISC = 0b111001;
LATCbits.LATC1 = 1;
LATCbits.LATC2 = 0;
}
void LED2_ON (void){
TRISC = 0b111001;
LATCbits.LATC1 = 0;
LATCbits.LATC2 = 1;
}
void LED3_ON (void){
TRISC = 0b110011;
LATCbits.LATC2 = 1;
LATCbits.LATC3 = 0;
}
void LED4_ON (void){
TRISC = 0b110011;
LATCbits.LATC2 = 0;
LATCbits.LATC3 = 1;
}
void LED5_ON (void){
TRISC = 0b100111;
LATCbits.LATC3 = 1;
LATCbits.LATC4 = 0;
}
void LED6_ON (void){
TRISC = 0b100111;
LATCbits.LATC3 = 0;
LATCbits.LATC4 = 1;
}
void LED7_ON (void){
TRISC = 0b110101;
LATCbits.LATC1 = 1;
LATCbits.LATC3 = 0;
}
void LED8_ON (void){
TRISC = 0b110101;
LATCbits.LATC1 = 0;
LATCbits.LATC3 = 1;
}
void LED9_ON (void){
TRISC = 0b101011;
LATCbits.LATC2 = 1;
LATCbits.LATC4 = 0;
}
void LED10_ON (void){
TRISC = 0b101011;
LATCbits.LATC2 = 0;
LATCbits.LATC4 = 1;
}
void LED11_ON (void){
TRISC = 0b101101;
LATCbits.LATC1 = 1;
LATCbits.LATC4 = 0;
}
void LED12_ON (void){
TRISC = 0b101101;
LATCbits.LATC1 = 0;
LATCbits.LATC4 = 1;
}
/**
End of File
*/
 

 
And my main.c code (just cycles through the LEDs as a test):
 

#include "mcc_generated_files/mcc.h"
#include "charlieplex_config.c"
 
/*
Main application
*/
void main(void)
{

SYSTEM_Initialize();
while (1)
{
__delay_ms(300);
LED1_ON();

__delay_ms(300);
LED2_ON();

__delay_ms(300);
LED3_ON();

__delay_ms(300);
LED4_ON();

__delay_ms(300);
LED5_ON();

__delay_ms(300);
LED6_ON();

__delay_ms(300);
LED7_ON();

__delay_ms(300);
LED8_ON();

__delay_ms(300);
LED9_ON();

__delay_ms(300);
LED10_ON();

__delay_ms(300);
LED11_ON();

__delay_ms(300);
LED12_ON();
}
}
/**
End of File
*/

 
#1

19 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/09 17:26:06 (permalink)
    0
    Do NOT #include the C file into your main C file.
    Just add the C file to the project, and create an h file containing declarations of the functions in your C file

    //charlieplex_config.h

    //declare public functions in charlieplex_config.c
    void LED1_ON (void);
    void LED2_ON (void);
    void LED3_ON (void);
    void LED4_ON (void);
    void LED5_ON (void);
    void LED6_ON (void);
    void LED7_ON (void);
    void LED8_ON (void);
    void LED9_ON (void);
    void LED10_ON (void);
    void LED11_ON (void);
    void LED12_ON (void);


    then change
    #include "charlieplex_config.c"
    to
    #include "charlieplex_config.h"
    in your main file
    post edited by ric - 2019/12/09 17:27:38

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/09 17:39:54 (permalink)
    0
    So, this is still not working
     
    main.c:

    #include "mcc_generated_files/mcc.h"
    #include "charlieplex_config.h"
     
    /*
    Main application
    */
    void main(void)
    {
    // initialize the device
    SYSTEM_Initialize();
    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:
    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();
    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();
    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();
    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
    while (1)
    {
    __delay_ms(300);
    LED1_ON();

    __delay_ms(300);
    LED2_ON();

    __delay_ms(300);
    LED3_ON();

    __delay_ms(300);
    LED4_ON();

    __delay_ms(300);
    LED5_ON();

    __delay_ms(300);
    LED6_ON();

    __delay_ms(300);
    LED7_ON();

    __delay_ms(300);
    LED8_ON();

    __delay_ms(300);
    LED9_ON();

    __delay_ms(300);
    LED10_ON();

    __delay_ms(300);
    LED11_ON();

    __delay_ms(300);
    LED12_ON();
    }
    }
    /**
    End of File
    */

     
    source file:

    #include "charlieplex_config.h"
    #include <xc.h>

    void LED1_ON (void){
    TRISC = 0b111001;
    LATCbits.LATC1 = 1;
    LATCbits.LATC2 = 0;
    }
    void LED2_ON (void){
    TRISC = 0b111001;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 1;
    }
    void LED3_ON (void){
    TRISC = 0b110011;
    LATCbits.LATC2 = 1;
    LATCbits.LATC3 = 0;
    }
    void LED4_ON (void){
    TRISC = 0b110011;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 1;
    }
    void LED5_ON (void){
    TRISC = 0b100111;
    LATCbits.LATC3 = 1;
    LATCbits.LATC4 = 0;
    }
    void LED6_ON (void){
    TRISC = 0b100111;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 1;
    }
    void LED7_ON (void){
    TRISC = 0b110101;
    LATCbits.LATC1 = 1;
    LATCbits.LATC3 = 0;
    }
    void LED8_ON (void){
    TRISC = 0b110101;
    LATCbits.LATC1 = 0;
    LATCbits.LATC3 = 1;
    }
    void LED9_ON (void){
    TRISC = 0b101011;
    LATCbits.LATC2 = 1;
    LATCbits.LATC4 = 0;
    }
    void LED10_ON (void){
    TRISC = 0b101011;
    LATCbits.LATC2 = 0;
    LATCbits.LATC4 = 1;
    }
    void LED11_ON (void){
    TRISC = 0b101101;
    LATCbits.LATC1 = 1;
    LATCbits.LATC4 = 0;
    }
    void LED12_ON (void){
    TRISC = 0b101101;
    LATCbits.LATC1 = 0;
    LATCbits.LATC4 = 1;
    }
    /**
    End of File
    */

     
    Header file:
     

    #ifndef CHARLIEPLEX_CONFIG_H
    #define CHARLIEPLEX_CONFIG_H
    #ifdef __cplusplus
    extern "C" {
    #endif
     

    #ifdef __cplusplus
    }
    #endif
    void LED1_ON (void);
    void LED2_ON (void);
    void LED3_ON (void);
    void LED4_ON (void);
    void LED5_ON (void);
    void LED6_ON (void);
    void LED7_ON (void);
    void LED8_ON (void);
    void LED9_ON (void);
    void LED10_ON (void);
    void LED11_ON (void);
    void LED12_ON (void);
    #endif /* CHARLIEPLEX_CONFIG_H */
     

    #3
    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/09 17:47:16 (permalink)
    0
    Define "not working"
    Does it compile?
    If it's just red squiggles under the functions, that's just a long standing bug in MPLABX.
    You don't really need all that extra guff you added to the header file.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/09 18:54:04 (permalink)
    0
    It isn’t compiling.
    The extra stuff in the header file was auto generated when I created the file. I tried removing it, but that didn’t help.

    Perhaps I’m making some other error.
    I’ll try again tomorrow
    #5
    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/09 19:01:57 (permalink)
    0
    didierleplae
    It isn’t compiling.

    Do you get an error message during the compile?
    What actually happens? I'm not getting any idea from your answers.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/09 19:54:10 (permalink)
    0
    If you search the forum I show how to make mplab x as nature intended.
    No squiggly lines or highlighting from header files so it is not impossible.
     
    It works 100%, it should be a sticky.

    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.
    #7
    oliverb
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/10 05:14:17 (permalink)
    +1 (1)
    Short explanation:
    When you have multiple C source files the compiler compiles them separately, then the linker combines them into one binary. Within each C source you can only use functions the compiler knows about. So a "quick, dirty" solution is to add a "prototype" for a function when you want to use it. This tells the compiler to just go with it the function will be defined later.
     
    This can quickly get very disorganised, so to get it under control you can put the function prototypes in a header, then use the header in your module and in the code where you use the module.
     
    Now there are some further considerations:
     
    For a small project you can make a "project header" which contains prototypes for all the functions in the project. It doesn't scale well, but means you have just one header file to include.
     
    For a larger project and if you want to make reusable modules you'd have one "h" file per "c" file, and your modules form a heirachy where if one module uses another then it includes its header file.
     
    #8
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/10 07:13:22 (permalink)
    0
    Create a library project with each function in a separate file.

    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.
    #9
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/10 13:13:25 (permalink)
    0
    ric
    didierleplae
    It isn’t compiling.

    Do you get an error message during the compile?
    What actually happens? I'm not getting any idea from your answers.


    Here is the error:

    make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
    make[1]: Entering directory '/Users/noisola/Desktop/Volt-a-tone/PIC_TESTS/Charlieplexing_test_16F18326.X'
    make -f nbproject/Makefile-default.mk dist/default/production/Charlieplexing_test_16F18326.X.production.hex
    make[2]: Entering directory '/Users/noisola/Desktop/Volt-a-tone/PIC_TESTS/Charlieplexing_test_16F18326.X'
    make[2]: *** No rule to make target 'mcc_generated_files/charlieplex_config.c', needed by 'build/default/production/main.p1'. Stop.
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    make[2]: Leaving directory '/Users/noisola/Desktop/Volt-a-tone/PIC_TESTS/Charlieplexing_test_16F18326.X'
    nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
    make[1]: Leaving directory '/Users/noisola/Desktop/Volt-a-tone/PIC_TESTS/Charlieplexing_test_16F18326.X'
    nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
    BUILD FAILED (exit value 2, total time: 342ms)

    #10
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/10 13:14:26 (permalink)
    0
    oliverb
    Short explanation:
    When you have multiple C source files the compiler compiles them separately, then the linker combines them into one binary. Within each C source you can only use functions the compiler knows about. So a "quick, dirty" solution is to add a "prototype" for a function when you want to use it. This tells the compiler to just go with it the function will be defined later.
     
    This can quickly get very disorganised, so to get it under control you can put the function prototypes in a header, then use the header in your module and in the code where you use the module.
     
    Now there are some further considerations:
     
    For a small project you can make a "project header" which contains prototypes for all the functions in the project. It doesn't scale well, but means you have just one header file to include.
     
    For a larger project and if you want to make reusable modules you'd have one "h" file per "c" file, and your modules form a heirachy where if one module uses another then it includes its header file.
     


    Thanks. This is very helpful
    #11
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/10 13:37:38 (permalink)
    0
    They are in the same directory or the path exists ?
    'mcc_generated_files/charlieplex_config.c'.
     
     

    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.
    #12
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/10 17:32:29 (permalink)
    0
    Gort2015
    They are in the same directory or the path exists ?
    'mcc_generated_files/charlieplex_config.c'.

    My charlieplex_config.c and charlieplex_config.h are in the same directory as main.c
    #13
    Mysil
    Super Member
    • Total Posts : 3677
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/10 19:09:11 (permalink)
    0
    Hi,
    When:  'charlieplex_config.c'   and  'charlieplex_config.c' is in the same directory as main.c
    then it doesn't help to look for those files in the subdirectory with files generated by MCC.
     
    You must tell MPLAB X where each .c source file is to be found:
    in 'Projects' window, the upper left part of MPLAB window, point to a suitable folder,
    Right Click to get a menu, and select: 'Add existing Item ...'  Browse until you find the wanted file.
     
    Do the same with the header file, so MPLAB can find it.
    But the Compiler do not care about what MPLAB know about header files. 
    If they all are in the same directory, there is nothing more.
    If it is a complicated project with files in many different directories,
    then create a 'Search List' of directories for the compiler to use.
    It is in 'Project Properties ...' under 'XC8 Compiler'
     
    Then, if there is a mess with some wrong files already compiled, try 'Clean and Build'
     
        Mysil
     
     
    post edited by Mysil - 2019/12/10 19:12:59
    #14
    hudejun
    Electrical engineer
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2019/12/05 08:48:06
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/11 00:30:30 (permalink)
    0
    You must set "Source Folders" and "Common include dirs" from the project properties.
    "Source Folders" includes *.c files of your custom firmware.
    "Common include dirs" must have all *.h directories including Harmony and your custom header files. 
     
    #15
    oliverb
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/11 01:46:08 (permalink)
    0
    FWIW you might need to use the "Clean and build" option once if you get the "no rule to make x" error. This can sometimes happen if you remove or rename source files.
     
    #16
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/11 10:02:24 (permalink)
    0
    I rebuilt this project on another computer last night just by copying and pasting the code from my second post in this thread and it compiled in simulator mode. 
    #17
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/11 12:27:47 (permalink)
    0
    oliverb
    FWIW you might need to use the "Clean and build" option once if you get the "no rule to make x" error. This can sometimes happen if you remove or rename source files.

    Ok, so came back to my original project and using "Clean and Build" seems to have fixed the issue. Thanks!
    #18
    didierleplae
    Senior Member
    • Total Posts : 175
    • Reward points : 0
    • Joined: 2019/03/31 16:57:50
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/11 12:31:45 (permalink)
    0
    hudejun
    You must set "Source Folders" and "Common include dirs" from the project properties.
    "Source Folders" includes *.c files of your custom firmware.
    "Common include dirs" must have all *.h directories including Harmony and your custom header files. 

     
    Thanks, this is helpful.
    Is it best practice to keep custom source and header files outside of MCC generated files?
    I'm thinking MCC might be confused if I go back and generate changes.
    Would it make sense to create a custom files directory?
     
    #19
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Creating a new source file in MPLAB X IDE 2019/12/11 14:07:31 (permalink)
    0
    If you keep all your projects and libraries in the same parent folder, "mplabxprojects" then any project can be accessed just by moving back one into the parent folder ("mplabxprojects") then into the folder containing the include file.
     
    #include "../qprint_Library.X/qprint.h"
    #include "../WS2824B_Library.X/WS2824B.h"
     
    I wouldn't move related files out of the project, you might lose some when backing up.
     
    Re-usable code goes into a compiled library that can be included by the linker.
     
     
     
     
     
     
     
    post edited by Gort2015 - 2019/12/11 14:08:38

    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.
    #20
    Jump to:
    © 2020 APG vNext Commercial Version 4.5